-
Notifications
You must be signed in to change notification settings - Fork 0
/
cube.js
74 lines (67 loc) · 1.63 KB
/
cube.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
class Cube {
constructor(i, j, color) {
this.i = i;
this.j = j;
this.x = i * spacing + padding / 2;
this.y = j * spacing + padding / 2;
this.color = color;
this.moving = null;
}
calculPos() {
this.x = this.i * spacing + padding / 2;
this.y = this.j * spacing + padding / 2;
}
show(i, j) {
this.updatePos()
const c = color(this.color);
fill(c);
noStroke();
rect(this.x, this.y, spacing - padding, spacing - padding, 16)
}
move(dir) {
this.moving = dir
}
updatePos() {
if (this.moving === 'UP') {
this.y -= deltaSpeed;
if (this.y <= (this.j - 1) * spacing + padding / 2) {
this.j -= 1;
if (this.j === -1) {
this.j = rows - 1
}
this.calculPos()
this.moving = null
}
} else if (this.moving === 'DOWN') {
this.y += deltaSpeed;
if (this.y >= (this.j + 1) * spacing + padding / 2) {
this.j += 1;
if (this.j === rows) {
this.j = 0
}
this.calculPos()
this.moving = null
}
} else if (this.moving === 'RIGHT') {
this.x += deltaSpeed;
if (this.x >= (this.i + 1) * spacing + padding / 2) {
this.i += 1;
if (this.i === cols) {
this.i = 0
}
this.calculPos()
this.moving = null
}
} else if (this.moving === 'LEFT') {
this.x -= deltaSpeed;
if (this.x <= (this.i - 1) * spacing + padding / 2) {
this.i -= 1;
if (this.i === -1) {
this.i = cols - 1
}
this.calculPos()
this.moving = null
}
}
}
}