-
Notifications
You must be signed in to change notification settings - Fork 0
/
food.js
33 lines (28 loc) · 810 Bytes
/
food.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
class Food {
constructor(gameMap, agentPos=null) {
this.gameMap = gameMap;
this.agentPos = agentPos;
this.generatePos();
}
generatePos() {
let x = floor(random(this.gameMap.width));
let y = floor(random(this.gameMap.height));
while(this.isInvalidPosition(x, y)) {
x = floor(random(this.gameMap.width));
y = floor(random(this.gameMap.height));
}
this.pos = {x, y};
}
isInvalidPosition(x, y) {
if (this.agentPos) {
return this.gameMap.grid[x][y] == OBSTACULO || (this.agentPos.x == x && this.agentPos.y == y);
}
return this.gameMap.grid[x][y] == OBSTACULO;
}
drawFood() {
rectMode(CORNER);
stroke(0);
fill(cores[COMIDA]);
rect(this.pos.x*GRID_SIZE, this.pos.y*GRID_SIZE, GRID_SIZE, GRID_SIZE);
}
}