-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.js
66 lines (56 loc) · 1.47 KB
/
player.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
import { globals } from "./globals.js";
export class Player {
constructor(startCoord) {
this.startCoord = startCoord;
this.visitedLinks = [];
this.visited = [this.startCoord];
this.head = this.visited[this.visited.length - 1];
this.moveCooldown = 0;
this.lastMove = Date.now();
}
canMove() {
if (Date.now() - this.lastMove >= this.moveCooldown) {
return true;
} else {
this.moveCooldown -= Date.now() - this.lastMove;
return false;
}
}
move(direction) {
for (const visited of this.visited) {
if (
this.head[0] + direction[0][0] == visited[0] &&
this.head[1] + direction[0][1] == visited[1]
) {
return;
}
}
this.visited.push([
this.head[0] + direction[0][0],
this.head[1] + direction[0][1],
]);
this.visitedLinks.push(direction[1]);
this.head = this.visited[this.visited.length - 1];
this.moveCooldown = globals.MOVE_COOLDOWN;
this.lastMove = Date.now();
}
clear(display) {
for (const coord of this.visited) {
display.clearNode(coord);
}
for (const coord of this.visitedLinks) {
display.hideLink(coord);
}
this.visitedLinks = [];
this.visited = [this.startCoord];
this.head = this.visited[this.visited.length - 1];
}
draw(display) {
for (const coord of this.visited) {
display.setNode(coord);
}
for (const coord of this.visitedLinks) {
display.showLink(coord);
}
}
}