-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGhosts.c
122 lines (108 loc) · 3.11 KB
/
Ghosts.c
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
#include <stdlib.h>
#include <stdio.h>
#include "Ghosts.h"
#include "Player.h"
#include "Map.h"
void moveGhost(struct Ghost *ghost, struct GameState *game) {
struct Position up = {ghost->pos.x-1, ghost->pos.y};
int upFree = !isWall(game->map, up);
struct Position down = {ghost->pos.x+1, ghost->pos.y};
int downFree = !isWall(game->map, down);
struct Position left = {ghost->pos.x, ghost->pos.y-1};
int leftFree = !isWall(game->map, left);
struct Position right = {ghost->pos.x, ghost->pos.y+1};
int rightFree = !isWall(game->map, right);
enum Direction dir;
int isFree = 0;
if(!upFree && !downFree && !leftFree && !rightFree){
return;
}
while(!isFree){
dir = rand() % 4;
switch (dir) {
case UP:
isFree = upFree && ghost->direction != DOWN;
break;
case DOWN:
isFree = downFree && ghost->direction != UP;
break;
case LEFT:
isFree = leftFree && ghost->direction != RIGHT;
break;
case RIGHT:
isFree = rightFree && ghost->direction != LEFT;
break;
}
}
ghost->direction = dir;
switch (dir) {
case UP:
ghost->pos.x--;
break;
case DOWN:
ghost->pos.x++;
break;
case LEFT:
ghost->pos.y--;
break;
case RIGHT:
ghost->pos.y++;
break;
}
switch (isOutOfMap(game->map, ghost->pos)) {
case 1:
ghost->pos.y = 0;
break;
case 2:
ghost->pos.x = 0;
break;
case 3:
ghost->pos.y = game->map->width - 1;
break;
case 4:
ghost->pos.x = game->map->length - 1;
break;
}
if(!game->powerUpEatGhostsTime){
if(comparePositions(&game->player->head, &ghost->pos)){
game->alive = 3;
}else if(game->player->tail && comparePositions(&game->player->tail->pos, &ghost->pos)){
removeTails(game->player->tail);
game->player->tail = NULL;
}else{
struct Tail *tail = game->player->tail;
struct Tail *oldtail = game->player->tail;
while(tail && !comparePositions(&tail->pos, &ghost->pos)) {
oldtail = tail;
tail = tail->tail;
}
if(tail){
removeTails(tail);
if(oldtail){
oldtail->tail = NULL;
}
}
}
}else{
if(isPlayerPos(game->player, ghost->pos)){
moveGhostHome(ghost);
}
}
}
void moveGhosts(struct Ghost *ghost, struct GameState *game) {
while(ghost->next){
moveGhost(ghost, game);
ghost = ghost->next;
}
moveGhost(ghost, game);
}
void moveGhostHome(struct Ghost *ghost) {
ghost->pos = ghost->homePos;
}
void moveGhostsHome(struct Ghost *ghost) {
while(ghost->next){
moveGhostHome(ghost);
ghost = ghost->next;
}
moveGhostHome(ghost);
}