-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenemy.php
More file actions
359 lines (323 loc) · 10.8 KB
/
enemy.php
File metadata and controls
359 lines (323 loc) · 10.8 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
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
354
355
356
357
358
359
<?php
declare(strict_types=1);
class Enemy
{
public int $id;
public int $x;
public int $y;
public float $movTimer;
public float $currentTimer;
public bool $active = false;
public bool $canMove = false;
public bool $moveWithPlayer = true;
public bool $isSeen = false;
public bool $gameover = false;
public Animation $animation;
public Mix_Chunk $foundSound;
public Mix_Chunk $loudSound;
public $gotPlayerTexture;
public SDL_Rect $gotPlayerRect;
private array $directionsAvailable;
private array $distances;
private array $positionSeen;
private int $objectiveX = 0;
private int $objectiveY = 0;
private Direction $oppositeDirection = Direction::NoDir;
private Direction $directionToMove = Direction::NoDir;
public function __construct(string $imagePath, int $id, float $timer, $windowSurface, $renderer)
{
$this->id = $id;
$this->x = 0;
$this->y = 0;
$this->movTimer = $timer;
$this->currentTimer = $timer;
$this->directionsAvailable = [];
$this->distances = [];
$this->positionSeen = [];
$origRect = new SDL_Rect;
$origRect->x = 0;
$origRect->y = $id * 14;
$origRect->w = 14;
$origRect->h = 14;
if ($id == 2) {
$this->moveWithPlayer = false;
$this->currentTimer *= 3;
$this->movTimer *= 3;
} elseif ($id == 3) {
$this->moveWithPlayer = false;
$this->currentTimer /= 3;
$this->movTimer /= 3;
}
$this->animation = new Animation($imagePath, 3, $origRect, $windowSurface, $renderer);
$this->animation->createSprite(0, $origRect->x, $origRect->y, -1, [$timer, $timer]);
$this->animation->setSprite(0);
$image = SDL_LoadBMP("graphics/fants_gotcha.bmp");
if ($image === null) {
exit("Cannot load enemy image" . PHP_EOL . SDL_GetError());
}
SDL_SetColorKey($image, true, SDL_MapRGB($windowSurface->format, 0, 255, 0));
$this->gotPlayerTexture = SDL_CreateTextureFromSurface($renderer, $image);
SDL_FreeSurface($image);
$this->gotPlayerRect = new SDL_Rect;
$this->gotPlayerRect->x = 0;
$this->gotPlayerRect->y = $id * 304;
$this->gotPlayerRect->w = 300;
$this->gotPlayerRect->h = 304;
}
public function loadAudio()
{
$path = match ($this->id) {
0 => "sound/qubodup-GhostMoan02.wav",
1 => "sound/zombie_pain.wav",
2 => "sound/qubodup-GhostMoan03.wav",
3 => "sound/scream_horror1.wav",
default => ""
};
$this->foundSound = Mix_LoadWAV($path);
if ($this->foundSound === null) {
exit("Cannot load enemy sound " . $path . PHP_EOL . Mix_GetError());
}
$this->loudSound = Mix_LoadWAV("sound/610753_prankheiteneberex_loud-jumpscare.wav");
if ($this->loudSound === null) {
exit("Cannot load enemy loud sound" . PHP_EOL . Mix_GetError());
}
}
public function activate(int $mazeWidth, int $mazeHeight)
{
$this->active = true;
$this->x = mt_rand(intdiv($mazeWidth, 2), $mazeWidth-1);
$this->y = mt_rand(intdiv($mazeHeight, 2), $mazeHeight-1);
}
public function playerInSameLine(Maze $maze, int $playerX, int $playerY): bool
{
foreach ($this->directionsAvailable as $dirValue) {
$initialX = $this->x;
$initialY = $this->y;
$keepMoving = true;
// Move until reaching a border
while ($keepMoving) {
$maze->moveCoords($initialX, $initialY, Direction::from($dirValue));
if ($initialX == $playerX && $initialY == $playerY) {
$this->directionToMove = Direction::from($dirValue);
$this->oppositeDirection = $maze->getOppositeDirection($this->directionToMove);
$this->objectiveX = $initialX;
$this->objectiveY = $initialY;
return true;
}
if ($maze->tiles[$initialY][$initialX]->borders[$dirValue]) {
$keepMoving = false;
}
}
}
return false;
}
public function setMovementValues(Maze $maze, int $directionToMove, int $x, int $y)
{
$this->directionToMove = Direction::from($directionToMove);
$this->oppositeDirection = $maze->getOppositeDirection($this->directionToMove);
$this->objectiveX = $x;
$this->objectiveY = $y;
}
public function setDirection0_1(Maze $maze, int $playerX, int $playerY)
{
$this->directionsAvailable = [];
$this->distances = [];
// Get all available directions to move
for ($i = 0; $i < 4; $i++) {
if (!$maze->tiles[$this->y][$this->x]->borders[$i]) {
$this->directionsAvailable[] = $i;
}
}
// Only one movement available, move there
if (count($this->directionsAvailable) == 1) {
$this->directionToMove = Direction::from($this->directionsAvailable[0]);
$this->oppositeDirection = $maze->getOppositeDirection($this->directionToMove);
$this->objectiveX = $this->x;
$this->objectiveY = $this->y;
$maze->moveCoords($this->objectiveX, $this->objectiveY, $this->directionToMove);
return;
}
// Check if the player can be reached by moving in a single direction
if ($this->playerInSameLine($maze, $playerX, $playerY)) {
return;
}
// Remove the opposite direction from the list
// to avoid moving back and forth
$keyOpposite = array_search($this->oppositeDirection->value, $this->directionsAvailable);
if ($keyOpposite !== false) {
unset($this->directionsAvailable[$keyOpposite]);
// Rearrange array
$this->directionsAvailable = array_values($this->directionsAvailable);
}
foreach ($this->directionsAvailable as $dir) {
$this->distances[$dir] = [
"dist" => INF,
"x" => 0,
"y" => 0,
"dir" => Direction::NoDir
];
}
// Only one movement available, move there (again)
if (count($this->directionsAvailable) == 1) {
$toMove = $this->directionsAvailable[0];
$x = $this->x;
$y = $this->y;
$maze->moveCoords($x, $y, Direction::from($toMove));
$this->setMovementValues($maze, $toMove, $x, $y);
return;
}
// Check the closest position to the player
foreach ($this->directionsAvailable as $dirValue) {
$initialX = $this->x;
$initialY = $this->y;
$keepMoving = true;
$count = 0;
$foundIntersection = false;
$opposite = $maze->getOppositeDirection(Direction::from($dirValue));
// Check if this path has an intersection,
// if it doesn't, is a dead end
while ($keepMoving) {
$maze->moveCoords($initialX, $initialY, Direction::from($dirValue));
$distX = $playerX;
$distY = $playerY;
if ($this->id == 1) {
if (mt_rand(0, 100) < 50) {
$distX = $maze->exitRect->x;
$distY = $maze->exitRect->y;
}
}
// Get smallest distance
$dist = $this->getDistance($initialX, $initialY, $distX, $distY);
if (!isset($this->distances[$dirValue])) {
return;
}
if ($dist < $this->distances[$dirValue]["dist"]) {
$this->distances[$dirValue]["dist"] = $dist;
$this->distances[$dirValue]["x"] = $initialX;
$this->distances[$dirValue]["y"] = $initialY;
$this->distances[$dirValue]["dir"] = Direction::from($dirValue);
}
// Check for an intersection
if (!$foundIntersection) {
for ($i = 0; $i < 4; $i++) {
if ($i == $dirValue) {
continue;
}
$tempX = $initialX;
$tempY = $initialY;
$maze->moveCoords($tempX, $tempY, Direction::from($i));
if (!$maze->isPositionValid($tempX, $tempY)) {
continue;
}
if (!$maze->tiles[$tempY][$tempX]->borders[$i]) {
$foundIntersection = true;
break;
}
}
}
if (!$maze->isPositionValid($initialX, $initialY)) {
$keepMoving = false;
break;
}
if ($maze->tiles[$initialY][$initialX]->borders[$dirValue]) {
$keepMoving = false;
}
}
// If no intersection is found discard this direction
/*
if (!$foundIntersection) {
$keyDist = array_search($dirValue, $this->distances);
unset($this->distances[$keyDist]);
$this->distances = array_values($this->distances);
$keyDir = array_search($dirValue, $this->directionsAvailable);
unset($this->directionsAvailable[$keyDir]);
$this->directionsAvailable = array_values($this->directionsAvailable);
}
if (count($this->distances) == 1) {
$toMove = $this->directionsAvailable[0];
$x = $this->x;
$y = $this->y;
$maze->moveCoords($x, $y, Direction::from($toMove));
$this->setMovementValues($maze, $toMove, $x, $y);
return;
}
*/
}
// Get smallest distance
$minDist = ["dist" => INF, "x" => 0, "y" => 0, "dir" => Direction::NoDir];
foreach ($this->distances as $dist) {
if ($dist["dist"] < $minDist["dist"]) {
$minDist["dist"] = $dist["dist"];
$minDist["x"] = $dist["x"];
$minDist["y"] = $dist["y"];
$minDist["dir"] = $dist["dir"];
}
}
$this->setMovementValues($maze, $minDist["dir"]->value, $minDist["x"], $minDist["y"]);
}
public function setDirection2(Maze $maze, int $playerX, int $playerY)
{
$dir = match (true) {
$this->y > $playerY => Direction::North,
$this->x < $playerX => Direction::East,
$this->y < $playerY => Direction::South,
$this->x > $playerX => Direction::West,
default => Direction::NoDir
};
$x = $this->x;
$y = $this->y;
$maze->moveCoords($x, $y, $dir);
$this->setMovementValues($maze, $dir->value, $x, $y);
}
public function setDirection3(Maze $maze, int $playerX, int $playerY)
{
$this->directionsAvailable = [];
for ($i = 0; $i < 4; $i++) {
if (!$maze->tiles[$this->y][$this->x]->borders[$i]) {
$this->directionsAvailable[] = $i;
}
}
shuffle($this->directionsAvailable);
$newDir = $this->directionsAvailable[array_rand($this->directionsAvailable)];
$x = $this->x;
$y = $this->y;
$maze->moveCoords($x, $y, Direction::from($newDir));
$this->setMovementValues($maze, $newDir, $x, $y);
}
public function update(float $dt, Maze $maze, int $playerX, int $playerY, bool $canMove)
{
$this->animation->update($dt);
if (!$canMove) return;
$this->currentTimer -= $dt;
if ($this->currentTimer <= 0 || ($this->moveWithPlayer && $this->canMove)) {
if ($this->directionToMove == Direction::NoDir) {
// This is horrible but much shorter than writing ifs/switch/match
$dirFunc = $this->id <= 1 ? "setDirection0_1" : "setDirection" . strval($this->id);
$this->$dirFunc($maze, $playerX, $playerY);
}
$this->playerInSameLine($maze, $playerX, $playerY);
$maze->moveCoords($this->x, $this->y, $this->directionToMove);
if ($this->x == $this->objectiveX && $this->y == $this->objectiveY) {
$this->directionToMove = Direction::NoDir;
$this->objectiveX = 0;
$this->objectiveY = 0;
}
$this->currentTimer = $this->movTimer;
$this->canMove = false;
}
if ($this->x == $playerX && $this->y == $playerY && !$this->gameover) {
$this->gameover = true;
Mix_PlayChannel(-1, $this->loudSound, 0);
}
}
public function draw(int $tileSize, int $offsetX, int $offsetY, $windowSurface, $renderer)
{
$x = $offsetX + ($tileSize * $this->x) + 2*$this->animation->spriteRect->w;
$y = $offsetY + ($tileSize * $this->y) + intdiv($tileSize, 4);
$this->animation->draw($x, $y, $windowSurface, $renderer);
}
public function getDistance(int $x, int $y, int $otherX, int $otherY): float
{
return sqrt(pow($x - $otherX, 2) + pow($y - $otherY, 2));
}
}