-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpac.js
207 lines (193 loc) · 7.31 KB
/
pac.js
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
class pac
{
//const _States = {"Normal":1, "Chase":2, "Flee":3, "Powered-Up":4, "Dead": 5}
constructor(color, initX, initY)
{
this.x = initX;
this.y = initY;
this.color = color;
this.state = 1 //"Normal":1, "Chase":2, "Flee":3, "Powered-Up":4
this.rev_dir = 0; //! Direção contrária. Usada por backtrack()
this.backtracktimer = 0; //! Define por quanto tempo iremos para a direção do backtrack
}
draw()
{
fill(this.color);
circle(this.x,this.y, r);
};
reverse_dir(dir)
{
switch(dir)
{
case _Directions.Up: return _Directions.Down;
case _Directions.Down: return _Directions.Up;
case _Directions.Right: return _Directions.Left;
case _Directions.Left: return _Directions.Right;
default:
//! Should be unreachable
return dir;
}
};
backtrack(dir1, dir2)
{
while(true)
{
var newDir = Math.floor(Math.random() * 5);
if (newDir !== dir1 && newDir !== dir2)
{
break;
}
}
this.rev_dir = newDir;
this.backtracktimer = 10;
this.move(newDir);
};
//! Updates the behavior of a Ghost
AIUpdate(pacman)
{
if(this.backtracktimer > 0)
{
this.backtracktimer--;
this.move(this.rev_dir);
return;
}
//! Primeiros devemos ver qual o estado do Pac-Man
if(pacman.state == _States.Normal)
{
//! Pac-Man normal, então a IA deve segui-lo
this.state = _States.Chase;
} else {
//! Pac-Man pegou o power-up, então a IA deve fugir
this.state = _States.Flee;
}
/*! Imagine que, do ponto onde a IA está, uma linha horizontal e uma linha vertical são desenhadas,
de modo a definir quatro quadrantes. (é pleonasmo isso? kkkkkkk)
Devemos encontrar em qual quadrante o Pac-Man está, de modo a seguir (ou fugir) de acordo.
!*/
const diff_x = Math.abs(pacman.x, this.x); //! Distância entre a coord. x entre os dois
const diff_y = Math.abs(pacman.y, this.y); //! Distância entre a coord. y entre os dois
const pacIsAbove = pacman.y < this.y; //! Define se o Pac-Man está acima da IA
const pacIsBelow = !pacIsAbove; //! Define se o Pac-Man está abaixo da IA
const pacIsToTheRight = pacman.x > this.x; //! Define se o Pac-Man está à direita da IA
const pacIsToTheLeft = !pacIsToTheRight; //! Define se o Pac-Man está à esquerda da IA
var bestDirection; //! A direção para ir que trará a IA mais próxima (ou mais longínqua) do Pac-Man
var secondBestDirection; //! A segunda melhor direção. Usada quando não for possível ir para a acima
if (pacIsAbove && pacIsToTheRight)
{
//! Primeiro quadrante - Pac-Man acima e à direita
//! Se perseguindo, a IA deve ir em direção à coordenada mais próxima do Pac-Man.
//! Se fugindo, a IA deve ir em direção contrária à coordenada mais próxima do Pac-Man.
if(diff_x > diff_y)
{
bestDirection = _Directions.Right;
secondBestDirection = _Directions.Up;
} else {
bestDirection = _Directions.Up;
secondBestDirection = _Directions.Right;
}
} else if (pacIsAbove && pacIsToTheLeft)
{
//! Segundo quadrante - Pac-Man acima e à esquerda
if(diff_x > diff_y)
{
bestDirection = _Directions.Left;
secondBestDirection = _Directions.Up;
} else {
bestDirection = _Directions.Up;
secondBestDirection = _Directions.Left;
}
} else if (pacIsBelow && pacIsToTheRight)
{
//! Terceiro quadrante - Pac-Man abaixo e à direita
if(diff_x > diff_y)
{
bestDirection = _Directions.Right;
secondBestDirection = _Directions.Down;
} else {
bestDirection = _Directions.Down;
secondBestDirection = _Directions.Right;
}
} else if (pacIsBelow && pacIsToTheLeft)
{
//! Último quadrante - Pac-Man abaixo e à esquerda
if(diff_x > diff_y)
{
bestDirection = _Directions.Left;
secondBestDirection = _Directions.Down;
} else {
bestDirection = _Directions.Down;
secondBestDirection = _Directions.Left;
}
} else {
//! Should not reach this point
throw new Error();
}
var cur_pos_x = this.x; //! Posição atual em x
var cur_pos_y = this.y; //! Posição atual em y
//! Se perseguindo, a IA deve ir em direção à coordenada mais próxima do Pac-Man.
//! Se fugindo, a IA deve ir em direção contrária à coordenada mais próxima do Pac-Man.
this.move(bestDirection);
var moved = this.x !== cur_pos_x || this.y !== cur_pos_y;
if(!moved)
{
/*! Não foi possível mover para a direção dada acima, portanto tentamos mover para a outra melhor direção !*/
cur_pos_x = this.x;
cur_pos_y = this.y;
moved = this.move(secondBestDirection);
moved = this.x !== cur_pos_x || this.y !== cur_pos_y;
if (!moved) {
//! Ainda assim, não foi possível mover para a segunda direção :/
//! Portanto movemos para onde a IA veio
this.backtrack(bestDirection, secondBestDirection);
}
}
};
move(dir_v)
{
if(this.state === _States.Flee)
{
dir_v = this.reverse_dir(dir_v);
}
let row = floor(this.y / sclY);
let col = floor(this.x / sclX);
switch(dir_v)
{
case 0: // Going up
if (mapa[row+3][col - 1] != 1){
this.y -= sclY ;
}
break;
case 1: // Going down
if (mapa[row+5][col - 1] != 1){
this.y += sclY;
}
break;
case 2: // Going left
if (row == 15 && col == 2){
this.x = 27.5*sclX;
}
if (mapa[row+4][col-2] != 1){
this.x -= sclX;
}
break;
case 3: // Going right
if (row == 15 && col == 27){
this.x = 2.5*sclX;
}
if (mapa[row+4][col] != 1){
this.x += sclX;
}
break;
default:
// Should be unreachable
break;
}
this.x = constrain(this.x,2.5*sclX , 27.5*sclX );
this.y = constrain(this.y,2.5*sclY , 29.5*sclY);
if(this.backtracktimer === 0)
{
//! Não podemos alterar essa direção caso o backtracking esteja acontecendo
this.rev_dir = this.reverse_dir(dir_v);
}
};
}