-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPlayer.js
More file actions
61 lines (53 loc) · 1.55 KB
/
Player.js
File metadata and controls
61 lines (53 loc) · 1.55 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
function Player() {
this.cell = goal.c + (goal.r * rows);
this.x = goal.c * size + size / 2;
this.y = goal.r * size + size / 2;
this.s = (size / updateFrequency);
this.dir = {
x: 0,
y: 0
}
this.show = function () {
fill(theme.player);
noStroke()
ellipse(this.x, this.y, size / 2);
}
this.update = function () {
let currentIndex = getIndex(round((this.y - size / 2) / size), round((this.x - size / 2) / size));
let currentCell = grid[currentIndex];
this.cell = currentIndex;
let nextIndex = getIndex(round((this.y - size / 2) / size) + this.dir.y, round((this.x - size / 2) / size) + this.dir.x);
let nextCell = grid[nextIndex];;
// if(this.x - grid[nextIndex].c * size > 5 && this.x - grid[nextIndex].c * size > 5 ) {
// return;
// }
// let nextCellCoordinates = {
// x: nextCell.c * size + size / 2,
// y: nextCell.r * size + size / 2
// }
if (!areNeighbors(currentCell, nextCell)) {
// console.log(!areNeighbors(currentCell, nextCell));
this.dir.x = 0;
this.dir.y = 0;
}
// Get the coin
if(coins[currentIndex]) {
score ++;
coins[currentIndex] =false;
}
this.x += this.dir.x * this.s;
this.y += this.dir.y * this.s;
if (!((this.x - (size / 2)) > 0 && (this.x - size / 2) < width - size)) {
this.dir.x = 0;
}
if (!((this.y - size / 2) > 0 && (this.y - size / 2) < height - size)) {
this.dir.y = 0;
}
if(this.dir.x == 0) {
this.x = lerp(this.x, currentCell.c * size + size / 2, .5);
}
if(this.dir.y == 0) {
this.y = lerp(this.y, currentCell.r * size + size / 2, .5);
}
}
}