-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsystem_ai.cpp
353 lines (325 loc) · 9.25 KB
/
system_ai.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#include "system_ai.h"
#include <set>
#include <cmath>
#include "component.h"
#include "component_motion.h"
#include "component_player.h"
#include "component_sprite.h"
#include "entity.h"
#include "engine.h"
#include "constants.h"
using namespace std;
void SystemAI::Update()
{
// Initialize (optional)
if (!initialized)
{
initialized = Initialize();
}
// Reset state and AI's decisions
if (engine->GetContext()->GetState() != 0)
{
state = -1;
Stop();
}
// Update AI's decisions if the game is not paused
if (!engine->GetContext()->IsPaused())
{
UpdateKeys();
}
// Update player's movement
UpdateMovement();
}
double SystemAI::XBallBelow(double y_target)
{
// TODO: Calculate the value of x when y is first below y_target
// x = x0 + v*t
// y = y0 +vy0*t + 0.5*a*t²
// Solve for t
double vy = cmot_ball->v_y;
double a = cmot_ball->a_y;
double y0 = cspr_ball->y;
double vx = cmot_ball->v_x;
double t;
t = (-sqrt(2*a*y_target-2*a*y0 + vy*vy) - vy)/a;
return cspr_ball->x + vx*t;
}
void SystemAI::MoveLeft()
{
pressed_left = true;
pressed_right = false;
}
void SystemAI::MoveRight()
{
pressed_left = false;
pressed_right = true;
}
void SystemAI::Jump()
{
pressed_up = true;
}
void SystemAI::Stop()
{
pressed_left = false;
pressed_right = false;
pressed_up = false;
}
float SystemAI::GetRandom()
{
return (float)rand() / (RAND_MAX);
}
void SystemAI::UpdateKeys()
{
int level = engine->GetContext()->GetLevel();
// TODO: Implement game logic for different levels
if (level == 1)
{
// If ball is on left side of the net, set state equal to -1
if(cspr_ball->x <= NET_POS_X){
state = -1;
}
// If state equals 0 or the ball is in serving position (v_x = 0, x == 600)
// Set state equal to 0 and serve only if the ball drops below y = 110 by simply moving right and jump
// Return
if(state == 0 || (cspr_ball->x == 600 && cmot_ball->v_x == 0)){
state = 0;
if(cspr_ball->y <= 110){
MoveRight();
Jump();
}
return;
}
// Calculate the x-value of the first position at which the ball drops below y = 90 (call this position P)
double P = XBallBelow(90);
// If P is on the left side of the net
// Position the slime closer than distance 6 to x = 600 (i.e. use abs(x - 600) < 6) by moving left/right (otherwise just stop)
// Return
if(P <= NET_POS_X - 30){
if(abs(cspr_player_2->x - 600) > 6){
if(cspr_player_2->x < 600){
MoveRight();
}else{
MoveLeft();
}
}else{
Stop();
}
return;
}
// If the horizontal distance between P and the slime is less than 25, and the slime is on the ground
// If Slime's x >= 675 and ball's x > 625
// Jump
// If Slime's x <= 435 and ball's x < 395 and horizontal distance between ball and slime is less than 75
// Jump
// If Horizontal distance between ball and slime is less than 110 and ball's y > 25 and ball's y < 150
// Jump
// Return
if(abs(P - cspr_player_2->x) < 25 && cspr_player_2->y <= 1){
if(cspr_player_2->x >= 675 && cspr_ball->x > 625){
Jump();
}
if(cspr_player_2->x <= 435 && cspr_ball->x < 395 && abs(P - cspr_player_2->x) < 75){
Jump();
}
if(abs(cspr_ball->x - cspr_player_2->x) < 110 && cspr_ball->y > 25 && cspr_ball->y < 150){
Jump();
}
return;
}else if(cspr_player_2->y <= 1){ // Position it as close as possible to P (use abs limit 25 instead of 6)
if(abs(cspr_player_2->x - P) > 25){
if(cspr_player_2->x < P){
MoveRight();
}else{
MoveLeft();
}
}else{
if(cspr_player_2->x < P){
MoveLeft();
}else{
MoveRight();
}
}
}else if(cspr_player_2->x >= 675){ // Move right
MoveLeft();
}else{ // Position the slime as close as possible to the ball (use abs limit 25 instead of 6)
if(abs(cspr_player_2->x - cspr_ball->x) > 25){
if(cspr_player_2->x < cspr_ball->x){
MoveRight();
}else{
MoveLeft();
}
}else{
if(cspr_player_2->x < P){
MoveLeft();
}else{
MoveRight();
}
}
}
}
else if (level == 2)
{
// If ball is on left side of the net, set state equal to -1
if(cspr_ball->x <= NET_POS_X){
state = -1;
}
// If state does not equal -1 or the ball is in serving position (v_x = 0, x == 600)
if(state != 1|| (cspr_ball->x == 600 && cmot_ball->v_x == 0)){
// If state equals -1
// Randomly set the state equal to 0, 1 or 2
if(state == -1){
state = rand() % 3;
}
// If state equals 0
// If the ball drops below y = 75, move right and jump
if(state == 0){
if(cspr_ball->y <= 75){
MoveRight();
Jump();
}
}
// If state equals 1
// If the ball drops below y = 75, move left and jump
if(state == 1){
if(cspr_ball->y <= 75){
MoveLeft();
Jump();
}
}
// If state equals 2
if(state == 2){
// If the ball's vertical speed is higher than 4.5 and slime's x < 645
// Move right (until slime's x >= 645)
if(cmot_ball->v_y > 4.5 && cspr_player_2->x < 645){
MoveRight();
}
// If slime's x >= 645
// Stop
if(cspr_player_2->x >= 645){
Stop();
}
// If the ball's vertical speed equals -1.125 and slime's x != 600
// Jump
if(cmot_ball->v_y == -1.125 && cspr_player_2->x != 600){
Jump();
}
// If the ball's vertical speeds is lower than -4.5 and the slime is mid-air and slime's x >= 633
// Move left
if(cmot_ball->v_y < -4.5 && cspr_player_2->x >= 633 && cspr_player_2->y >= 1){
MoveLeft();
}
}
return;
}
// Calculate the x-value of the first position at which the ball drops below y = 90 (call this position P)
double P = XBallBelow(90);
// If P is on the left side of the net
// Position the slime closer than distance 6 to x = 480 (i.e. use abs(x - 480) < 6) by moving left/right (otherwise just stop)
// Return
if(P <= NET_POS_X){
if(abs(cspr_player_2->x - 480) > 6){
if(cspr_player_2->x < P){
MoveRight();
}else{
MoveLeft();
}
}else{
Stop();
}
return;
}
// If the horizontal distance between P and the slime is less than 25, and the slime is on the ground
// If Slime's x >= 675 and ball's x > 625
// Jump
// If Slime's x <= 435 and ball's x < 395 and horizontal distance between ball and slime is less than 75
// Jump
// If Horizontal distance between ball and slime is less than 110 and ball's y > 25 and ball's y < 150 and random value < 0.5
// Jump
// Return
if(abs(P - cspr_player_2->x) < 25){
if(cspr_player_2->x >= 675 && cspr_ball->x > 625){
Jump();
}
if(cspr_player_2->x <= 435 && cspr_ball->x < 395 && abs(cspr_ball->x - cspr_player_2->x) < 75){
Jump();
}
if(abs(cspr_ball->x - cspr_player_2->x) < 110 && cspr_ball->y > 25 && cspr_ball->y < 150 && GetRandom() < 0.5){
Jump();
}
return;
}else if(cspr_player_2->y <= 1){
if(abs(cspr_player_2->x - P) > 25){
if(cspr_player_2->x < P){
MoveRight();
}else{
MoveLeft();
}
}else{
if(cspr_player_2->x < P){
MoveLeft();
}else{
MoveRight();
}
}
}else if(cspr_player_2->x >= 675){
MoveLeft();
}else{
if(abs(cspr_player_2->x - cspr_ball->x) > 25){
if(cspr_player_2->x < cspr_ball->x){
MoveRight();
}else{
MoveLeft();
}
}else{
if(cspr_player_2->x < cspr_ball->x){
MoveLeft();
}else{
MoveRight();
}
}
}
// Else if the slime is on the ground
// Position it as close as possible to P (use abs limit 25 instead of 6)
// Else if the slime's x >= 675
// Move right
// Else
// Position the slime as close as possible to the ball (use abs limit 25 instead of 6)
}
else if (level == 3)
{
MoveRight();
}
}
void SystemAI::UpdateMovement()
{
// TODO: Change player's movement according to AI decisions (i.e. pressed_xxx)
cmot_player_2->v_x = 0;
if(pressed_up && cspr_player_2->y <= 3){
cmot_player_2->v_y = SLIME_V_Y;
pressed_up = false;
}
if(pressed_right){
cmot_player_2->v_x = SLIME_V_X;
pressed_right = false;
}
if(pressed_left){
cmot_player_2->v_x = -SLIME_V_X;
pressed_left = false;
}
}
bool SystemAI::Initialize()
{
// TODO: Initialize all component pointers (optional)
EntityStream* es = engine->GetEntityStream();
set<Entity*> entities_player = es->WithTag(Component::PLAYER);
set<Entity*> entity_ball = es->WithTag(Component::BALL);
set<Entity*>::iterator itr;
itr = entities_player.begin();
cspr_player_1 = dynamic_cast<ComponentSprite*>((*itr)->GetComponent(Component::SPRITE));
itr++;
cspr_player_2 = dynamic_cast<ComponentSprite*>((*itr)->GetComponent(Component::SPRITE));
cmot_player_2 = dynamic_cast<ComponentMotion*>((*itr)->GetComponent(Component::MOTION));
cspr_ball = dynamic_cast<ComponentSprite*>((*entity_ball.begin())->GetComponent(Component::SPRITE));
cmot_ball = dynamic_cast<ComponentMotion*>((*entity_ball.begin())->GetComponent(Component::MOTION));
return true;
}