forked from SethRobinson/arduman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGhost.cpp
350 lines (290 loc) · 6.18 KB
/
Ghost.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
#include "Ghost.h"
#include "ardu_main.h"
#include "Music.h"
uint8_t GHOST_COUNT = STARTING_GHOSTS;
Ghost ghosts[GHOST_MAX];
void DontDrawPelletHere(uint8_t x, uint8_t y)
{
for (int i=0; i < GHOST_COUNT; i++)
{
ghosts[i].OnReleasePellet(x,y);
}
}
uint8_t RandomDirection()
{
int r = random(0,4);
switch (r)
{
case 0: return LEFT_BUTTON;
case 1: return RIGHT_BUTTON;
case 2: return UP_BUTTON;
case 3: return DOWN_BUTTON;
}
return LEFT_BUTTON; //error
}
uint8_t GetDirTowardsPlayer(int16_t x, int16_t y, uint8_t curDir)
{
if (curDir == LEFT_BUTTON || curDir == RIGHT_BUTTON)
{
if (y > player.m_y) return UP_BUTTON; else return DOWN_BUTTON;
}
if (x < player.m_x) return RIGHT_BUTTON;
return LEFT_BUTTON;
}
uint8_t RandomTurn(uint8_t dir)
{
#ifdef _DEBUG
//LogMsg("Rand turn starting with %s", DirToText(dir));
#endif
int r = random(0,2);
if (dir == LEFT_BUTTON || dir == RIGHT_BUTTON)
{
switch (r)
{
case 0: return UP_BUTTON;
case 1: return DOWN_BUTTON;
}
} else
{
switch (r)
{
case 0: return LEFT_BUTTON;
case 1: return RIGHT_BUTTON;
}
}
return LEFT_BUTTON; //error
}
Ghost::Ghost()
{
m_x = 0;
m_y = 0;
m_id = 0;
m_pauseTimer = 0;
m_foodX = m_foodY = 0;
}
Ghost::~Ghost()
{
}
void ResetGhostPositions(bool bNewGame)
{
GHOST_COUNT = STARTING_GHOSTS + (GHOSTS_TO_ADD_EACH_LEVEL*(g_level-1));
for (int i=0; i < GHOST_COUNT; i++)
{
if (bNewGame)
ghosts[i].ResetForNewGame();
ghosts[i].SetGhostID(i);
ghosts[i].ResetPosition();
ghosts[i].SetPauseTimer(millis()+500+800*i);
}
}
void Ghost::ResetForNewGame()
{
m_foodX = 0;
m_x = 0;
m_y = 0;
}
void Ghost::SetPauseTimer(long time)
{
m_pauseTimer = time;
m_thinkTimer = m_thinkTimer+= time;
}
float Ghost::GetAdjustedSpeed()
{
return GHOST_SPEED; //if we wanted them faster each level +(0.02f*(g_level-1));
}
void Ghost::ResetPosition()
{
UnRender();
m_foodX = 0;
m_oldY = m_oldX =0;
m_speed = GetAdjustedSpeed();//make it harder..
if (m_speed > 1) m_speed = 1;
m_x = GHOST_CUBBY_HOLE_X;
int offset;
if (m_id < 4)
{
offset = (m_id*5);
} else
{
offset = random(0, 15);
}
m_y = (float)GHOST_CUBBY_HOLE_Y+ offset;
m_curDir = RandomTurn(UP_BUTTON); //so we only turn left or right at the start
m_bIsChasing = false;
m_thinkTimer = random(500, 4000);
}
void Ghost::SetLastTurnedSpot(int16_t x, int16_t y)
{
m_oldX = x;
m_oldY = y;
}
bool Ghost::IsNewTurnSpot(int16_t x, int16_t y)
{
return x != m_oldX || y != m_oldY;
}
bool Ghost::IsChasing()
{
if (m_thinkTimer < millis())
{
if (!m_bIsChasing)
{
m_thinkTimer = millis()+random(5000,8000);
} else
{
m_thinkTimer = millis()+random(1000,3000); //let's chase more than we don't chase
}
m_bIsChasing = !m_bIsChasing;
}
return m_bIsChasing;
}
void Ghost::Update()
{
bool bIgnoreNextTurn = false;
uint8_t tempDir = m_curDir;
if (m_pauseTimer > millis())
{
//do nothing
} else
{
if (m_x == GHOST_CUBBY_HOLE_X && m_y > GHOST_START_Y && m_y < GHOST_CUBBY_HOLE_Y+(4*4))
{
//they are still in the cubby!
if (m_y > GHOST_START_Y)
{
//float up
m_y = int(m_y-1);
return;
}
m_y = GHOST_START_Y;
bIgnoreNextTurn = true;
}
//get new position
float x = m_x;
float y = m_y;
if (EntitiesAreTouching(m_x, m_y, player.m_x, player.m_y, 2.0f))
{
if (player.PowerIsActive())
{
#ifdef RT_ARDUDEV
GetAudioManager()->Play("audio/eat_ghost.wav");
#else
tunes.playScore(music_kill_ghost);
#endif
Render(true);
delay(GHOST_KILL_DELAY_MS);
arduboy.display();
ResetPosition();
SetPauseTimer(millis()+player.GetPowerTimeLeft()+(500*m_id));
player.OnKilledGhost();
return;
} else
{
player.OnHurt();
}
}
uint8_t crumbDir =crumbManager.GetDirToPlayer((int16_t)x, (int16_t)y);
/*
if (crumbDir)
LogMsg("Enemy located player to the %s", DirToText(crumbDir));
*/
if (!bIgnoreNextTurn && IsNewTurnSpot((int16_t)x,(int16_t)y) && CanTurn((int16_t)x,(int16_t)y, m_curDir) &&
(random(3) == 0||crumbDir || IsChasing()) && m_y > 5 && m_y < 60)
{
if (crumbDir != 0)
{
tempDir = crumbDir;
} else
{
if (IsChasing())
{
tempDir = GetDirTowardsPlayer((int16_t)m_x, (int16_t)m_y, m_curDir);
} else
{
tempDir = RandomTurn(m_curDir);
}
}
#ifdef _DEBUG
//LogMsg("Chose dir %s", DirToText(tempDir));
#endif
SetLastTurnedSpot((int16_t)x,(int16_t)y);
MovePosition(&x,&y, tempDir, 1);
while (HitWall((int16_t)x, (int16_t)y))
{
//bad turn, undo it
x = m_x;
y = m_y;
tempDir = RandomTurn(m_curDir);
#ifdef _DEBUG
// LogMsg("Chose dir2 %s", DirToText(tempDir));
#endif
MovePosition(&x,&y, tempDir, 1);
}
} else
{
float speed;
if (player.PowerIsActive() || IsInTunnel((int16_t)x,(int16_t)y))
{
speed = m_speed*GHOST_TUNNEL_SPEED_MOD;
} else
{
speed = m_speed;
}
MovePosition(&x,&y, m_curDir, speed);
while(HitWall((int16_t)x, (int16_t)y) && m_y > 0 && m_y < 60)
{
uint8_t crumbDir =crumbManager.GetDirToPlayer((int16_t)m_x, (int16_t)m_y);
if (crumbDir)
{
tempDir = crumbDir;
//LogMsg("Enemy located player to the %s (wall bump)", DirToText(crumbDir));
} else
{
tempDir = RandomTurn(m_curDir);
}
x = m_x;
y = m_y;
#ifdef _DEBUG
// LogMsg("Chose dir3 %s", DirToText(tempDir));
#endif
MovePosition(&x,&y, tempDir, 1);
SetLastTurnedSpot((int16_t)x,(int16_t)y);
}
}
//valid move
m_curDir = tempDir;
m_x = x;
m_y = y;
WorldWrapY(m_y);
}
m_foodX = m_foodY = 0;
if (GetFoodWeAreOn(&m_foodX, &m_foodY, (int16_t)m_x, (int16_t)m_y))
{
}
}
void Ghost::Blit(bool color, bool bDying)
{
if (m_x == 0) return;
if (!player.PowerIsActiveDisplay() || millis()%100 > 50 || bDying)
arduboy.drawBitmap((int16_t)m_x, (int16_t)m_y, ghost_bmp, PLAY_W,PLAY_H, color);
}
void Ghost::UnRender()
{
if (m_x == 0) return;
arduboy.drawBitmap((int16_t)m_x, (int16_t)m_y, solid_box, PLAY_W,PLAY_H, 0);
//put back any food pellets
if (m_foodX != 0)
{
arduboy.drawPixel(m_foodX, m_foodY, 1);
}
}
void Ghost::Render(bool bDying)
{
Blit(1, bDying);
}
void Ghost::OnReleasePellet(uint8_t foodX, uint8_t foodY)
{
if (foodX == m_foodX && m_foodY == foodY)
{
m_foodX = m_foodY = 0;
}
}