-
Notifications
You must be signed in to change notification settings - Fork 7
/
Enemy.cpp
279 lines (247 loc) · 9.83 KB
/
Enemy.cpp
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/******************************************************************************
* @file Enemy.cpp
* @author Andrés Gavín Murillo, 716358
* @author Rubén Rodríguez Esteban, 737215
* @date Mayo 2020
* @coms Videojuegos - OutRun
******************************************************************************/
#include "Enemy.hpp"
#include "Random.hpp"
#include <cmath>
using namespace std;
using namespace sf;
#define ENEMY_TEXTURES 16
#define MAX_AUTO_DIRECTION_COUNTER 1000
Enemy::Enemy(float maxSpeed, float speedMul, float scale, int maxCounterToChange, const string &vehicle, float pY) :
Vehicle(maxSpeed / speedMul, scale, maxCounterToChange, 0, random_float(-0.5f, 0.5f),
pY, pY, 0, 0, vehicle, ENEMY_TEXTURES, 1, 0), oriX(this->posX),
currentDirection(RIGHT), calculatedPath(RIGHT), current_direction_counter(0), max_direction_counter(0),
probAI(0), typeAI(OBSTACLE) {}
Vehicle::Direction randomDirection() {
const float p = random_zero_one();
if (p < 0.6f)
return Vehicle::Direction::RIGHT;
else if (p < 0.8f)
return Vehicle::Direction::TURNRIGHT;
else
return Vehicle::Direction::TURNLEFT;
}
void Enemy::autoControl(const Config &c, float playerPosX, float playerPosY) {
if (abs(playerPosY - posY) > float(c.renderLen) || random_zero_one() >= probAI) {
// Original
if (current_direction_counter < max_direction_counter) {
current_direction_counter++;
} else {
max_direction_counter = random_zero_n(MAX_AUTO_DIRECTION_COUNTER);
current_direction_counter = 0;
calculatedPath = randomDirection();
}
float newX = posX;
if (calculatedPath == TURNRIGHT)
newX += XINC * random_zero_one() * speed / maxSpeed;
else if (calculatedPath == TURNLEFT)
newX -= XINC * random_zero_one() * speed / maxSpeed;
if (newX < oriX - 0.15f || newX > oriX + 0.15f)
calculatedPath = RIGHT;
else
posX = newX;
currentDirection = calculatedPath;
} else {
// AI
if (typeAI == OBSTACLE) {
const float acc = getAcceleration();
if (abs(playerPosX - posX) > XINC) { // Rotation control
// The vehicle is not in the player's path
if (speed < halfMaxSpeed)
speed = sqrt(acc + ACC_INC);
if (posX > playerPosX && posX > -0.9f) {
posX -= XINC * speed / maxSpeed;
currentDirection = TURNLEFT;
} else if (posX < 0.9f) {
posX += XINC * speed / maxSpeed;
currentDirection = TURNRIGHT;
}
} else { // Acceleration control
// The vehicle is in the player's path
if (posY <= playerPosY)
speed = sqrt(acc + ACC_INC);
else if (acc > ACC_INC)
speed = sqrt(acc - ACC_INC);
else
speed = 0.0f;
currentDirection = RIGHT;
}
} else if (typeAI == EVASIVE) {
if (playerPosX <= -0.5) {
posX += XINC * speed / maxSpeed;
currentDirection = TURNRIGHT;
} else if (playerPosX >= 0.5) {
posX -= XINC * speed / maxSpeed;
currentDirection = TURNLEFT;
} else {
if (posX > playerPosX) {
posX += XINC * speed / maxSpeed;
currentDirection = TURNRIGHT;
} else {
posX -= XINC * speed / maxSpeed;
currentDirection = TURNLEFT;
}
}
} else { // INCONSTANT
if (currentDirection == TURNRIGHT) {
const float prevPosX = posX;
posX += XINC * speed / maxSpeed;
if (posX >= 0.9f)
currentDirection = TURNLEFT;
else if (((prevPosX < -0.5f && posX >= -0.5f) || (prevPosX < 0.0f && posX >= 0.0f) ||
(prevPosX < 0.5f && posX >= 0.5f)) && (random_zero_one() < 0.5f))
currentDirection = TURNRIGHT; // Lane change
} else if (currentDirection == TURNLEFT) {
const float prevPosX = posX;
posX -= XINC * speed / maxSpeed;
if (posX <= -0.9f)
currentDirection = TURNRIGHT;
else if (((prevPosX > -0.5f && posX <= -0.5f) || (prevPosX > 0.0f && posX <= 0.0f) ||
(prevPosX > 0.5f && posX <= 0.5f)) && (random_zero_one() < 0.5f))
currentDirection = TURNLEFT; // Lane change
} else {
if (random_zero_one() < 0.5f)
currentDirection = TURNRIGHT;
else
currentDirection = TURNLEFT;
}
}
}
if (speed > maxSpeed)
speed = maxSpeed;
else if (speed < 0)
speed = 0;
if (posX > 0.9f) {
posX = 0.9f;
currentDirection = RIGHT;
} else if (posX < -0.9f) {
posX = -0.9f;
currentDirection = RIGHT;
}
previousY = posY;
posY += speed;
}
void Enemy::update(float iniPos, float endPos, float maxAggressiveness) {
speed = maxSpeed * random_float(0.25f, 0.75f);
posY = random_float(iniPos, endPos);
previousY = posY;
current_direction_counter = 0;
max_direction_counter = 0;
minScreenX = 0;
maxScreenX = 0;
setAI(maxAggressiveness);
}
void Enemy::setAI(float maxAggressiveness) {
if (maxAggressiveness == 0.0f)
probAI = 0.0f;
else
probAI = random_float(maxAggressiveness / 2.0f, maxAggressiveness);
const float p = random_zero_one();
if (p < 0.333f) {
typeAI = OBSTACLE;
} else if (p < 0.666f) {
typeAI = EVASIVE;
} else {
typeAI = INCONSTANT;
probAI *= 2.0f;
}
}
void Enemy::draw(const Elevation &e, const float camX) {
if (counter_code_image >= maxCounterToChange) {
counter_code_image = 0;
if (speed > 0.0f)
current_code_image++;
if (textures.size() == ENEMY_TEXTURES) {
if (e == FLAT) {
if (currentDirection == RIGHT) {
if (posX <= camX) {
if (current_code_image < 1 || current_code_image > 2)
current_code_image = 1;
} else {
if (current_code_image < 3 || current_code_image > 4)
current_code_image = 3;
}
} else if (currentDirection == TURNLEFT) {
if (current_code_image < 7 || current_code_image > 8)
current_code_image = 7;
} else { // Turn right
if (current_code_image < 5 || current_code_image > 6)
current_code_image = 5;
}
} else if (e == UP) {
if (currentDirection == RIGHT) {
if (posX <= camX) {
if (current_code_image < 13 || current_code_image > 14)
current_code_image = 13;
} else {
if (current_code_image < 15 || current_code_image > 16)
current_code_image = 15;
}
} else if (currentDirection == TURNLEFT) {
if (current_code_image < 15 || current_code_image > 16)
current_code_image = 15;
} else { // Turn right
if (current_code_image < 13 || current_code_image > 14)
current_code_image = 13;
}
} else { // Down
if (currentDirection == RIGHT) {
if (posX <= camX) {
if (current_code_image < 9 || current_code_image > 10)
current_code_image = 9;
} else {
if (current_code_image < 11 || current_code_image > 12)
current_code_image = 11;
}
} else if (currentDirection == TURNLEFT) {
if (current_code_image < 11 || current_code_image > 12)
current_code_image = 11;
} else { // Turn right
if (current_code_image < 9 || current_code_image > 10)
current_code_image = 9;
}
}
}
} else {
counter_code_image++;
}
}
void Enemy::setMinScreenX(float screenX) {
minScreenX = screenX;
}
void Enemy::setMaxScreenX(float screenX) {
maxScreenX = screenX;
}
const Texture *Enemy::getCurrentTexture() const {
return &textures[current_code_image - 1];
}
float Enemy::getScale() const {
return scale;
}
bool Enemy::hasCrashed(float prevY, float currentY, float minX, float maxX, float &crashPos) const {
if (minScreenX != maxScreenX && ((prevY <= posY + 2.5f && currentY >= posY - 2.5f) ||
(currentY <= posY + 2.5f && prevY >= posY - 2.5f)) && // y matches
((minX >= minScreenX && minX <= maxScreenX) ||
(maxX >= minScreenX && maxX <= maxScreenX) ||
(minScreenX >= minX && minScreenX <= maxX) ||
(maxScreenX >= minX && maxScreenX <= maxX))) { // x matches
crashPos = posY;
return true;
}
return false;
}
bool
Enemy::isVisible(const Config &c, float minY, float playerX, float playerY, float &distanceX, float &distanceY) const {
if (posY < minY || posY > minY + float(c.renderLen) || minScreenX < 0 || maxScreenX > c.w.getSize().y) {
return false;
} else {
distanceX = abs(playerX - posX);
distanceY = abs(playerY - posY);
return true;
}
}