-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCEnemy.cpp
More file actions
204 lines (170 loc) · 4.79 KB
/
CEnemy.cpp
File metadata and controls
204 lines (170 loc) · 4.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
//=============================================================================
#include "CEnemy.h"
// CEnemy::EnemyList[i]->OnLoad("enemy.png", 34, 47, 3);
//=============================================================================
CEnemy::CEnemy(float startX, float startY, char* File, int Width, int Height, int MaxFrames)
{
armed = false;
canAttack = true;
hitTimer = 1300;
onHitTime = 0;
X = startX;
Y = startY;
health = 100;
currentItem = NULL;
OnLoad(File, Width, Height, MaxFrames);
Type = ENTITY_TYPE_ENEMY;
Flags = ENTITY_FLAG_GRAVITY | ENTITY_FLAG_MAPONLY;
}
CEnemy::~CEnemy()
{
OnCleanup();
}
//=============================================================================
bool CEnemy::OnLoad(char* File, int Width, int Height, int MaxFrames)
{
if(CEntity::OnLoad(File, Width, Height, MaxFrames) == false)
{
return false;
}
Anim_Control.SetFrameRate(100);
Anim_Control.Oscillate = true;
MaxSpeedX = 2;
return true;
}
//-----------------------------------------------------------------------------
void CEnemy::OnLoop(float playerX, float playerY)
{
if (health < 1)
{
Dead = true;
if(armed)
{
currentItem->OnDrop();
currentItem = NULL;
armed = false;
}
}
if(SDL_GetTicks() - onHitTime > hitTimer)
{
canAttack = true;
}
// Give the coordinates to the equiped items; weapon in this case
if(armed)
{
//if facing right, face the item right
if(CurrentFrameCol == 0)
{
currentItem->X = X + Width;
currentItem->SetCurrentFrameCol(0);
}
//if facing left, face the item left
else if(CurrentFrameCol == 1)
{
currentItem->X = X - currentItem->Width;
currentItem->SetCurrentFrameCol(1);
}
currentItem->Y = (Y + Height) - (Height / 2);
}
// Have the enemy start attacking if close enough
if((armed) && (canAttack) && (abs(X - playerX) < 75) && (abs(Y - playerY) < 25))
{
//Jump(); // I had them jump just to test this if statement, since they were not Attack()ing
Attack();
}
// Have the enemy chase the player within a certain range
// Enemy attempts to jump if chasing and can't move
CanJump = false;
MaxSpeedY = 10;
if((playerX - X < 500) && (X < playerX) && (abs(Y - playerY) < 250))
{
if(MoveRight)
{
if(oldX == X && oldY >= Y)
{
CanJump = true;
MaxSpeedY = 5;
Jump();
}
}
MoveRight = true;
MoveLeft = false;
oldX = X;
Anim_Control.Oscillate = true;
}
else if((X - playerX < 500) && (X > playerX) && (abs(Y) - playerY) < 250)
{
if(MoveLeft)
{
if(oldX == X && oldY >= Y)
{
CanJump = true;
MaxSpeedY = 5;
Jump();
}
}
MoveLeft = true;
MoveRight = false;
oldX = X;
oldY = Y;
Anim_Control.Oscillate = true;
}
if((playerX - X) > 250 || (X - playerX) > 250)
{
MoveLeft = false;
MoveRight = false;
Anim_Control.Oscillate = false;
}
CEntity::OnLoop(playerX, playerY);
}
//-----------------------------------------------------------------------------
void CEnemy::OnRender(SDL_Surface* Surf_Display)
{
if(Surf_Entity == NULL || Surf_Display == NULL) return;
CSurface::OnDraw(Surf_Display, Surf_Entity, X - CCamera::CameraControl.GetX(), Y - CCamera::CameraControl.GetY() + 1, CurrentFrameCol * Width, (CurrentFrameRow + Anim_Control.GetCurrentFrame()) * Height, Width, Height);
}
//------------------------------------------------------------------------------
void CEnemy::OnCleanup()
{
CEntity::OnCleanup();
}
//------------------------------------------------------------------------------
void CEnemy::OnAnimate()
{
if(SpeedX != 0)
{
Anim_Control.MaxFrames = 3;
}
else
{
Anim_Control.MaxFrames = 0;
}
CEntity::OnAnimate();
}
//------------------------------------------------------------------------------
bool CEnemy::OnCollision(CEntity* Entity)
{
return true;
}
//=============================================================================
void CEnemy::Attack()
{
if(armed)
{
if(canAttack == false)
{
return;
}
canAttack = false;
onHitTime = SDL_GetTicks();
currentItem->DoDamage();
}
}
//=============================================================================
void CEnemy::Wield(CEntity* itemToWield)
{
if(armed) return;
armed = true;
itemToWield->SetOwner(this);
currentItem = itemToWield;
}