-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake.js
249 lines (230 loc) · 6.15 KB
/
snake.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
"use strict";
class Snake {
#sketch;
#x;
#y;
#xspeed;
#yspeed;
#total;
#tail;
#topology; // Declare the private field '#topology' in the class 'Snake'
#scl;
#height;
#width;
#is_dead;
#directions = {
LEFT: [-1, 0],
RIGHT: [1, 0],
DOWN: [0, 1],
UP: [0, -1],
};
constructor(sketch) {
this.#sketch = sketch;
this.#x = 0;
this.#y = 0;
this.#xspeed = 1;
this.#yspeed = 0;
this.#total = 0;
this.#tail = [];
this.#is_dead = false;
this.#topology = sessionStorage.getItem("topology"); // Initialize the private field '#topology'
this.#scl = parseInt(sessionStorage.getItem("scale"));
this.#height = parseInt(sessionStorage.getItem("height"));
this.#width = parseInt(sessionStorage.getItem("width"));
}
// getters
get sketch() {
return this.#sketch;
}
get x() {
return this.#x;
}
get y() {
return this.#y;
}
get xspeed() {
return this.#xspeed;
}
get yspeed() {
return this.#yspeed;
}
get total() {
return this.#total;
}
get tail() {
return this.#tail;
}
get directions() {
return this.#directions;
}
get topology() {
return this.#topology;
}
get is_dead() {
return this.#is_dead;
}
get scl() {
return this.#scl;
}
get height() {
return this.#height;
}
get width() {
return this.#width;
}
// setters
set xspeed(xspeed) {
if (Math.abs(xspeed) <= 1) {
this.#xspeed = xspeed;
}
}
set yspeed(yspeed) {
if (Math.abs(yspeed) <= 1) {
this.#yspeed = yspeed;
}
}
eat(pos) {
let d = this.sketch.dist(this.x, this.y, pos.x, pos.y);
if (d < 1) {
this.#total++;
return true;
} else {
return false;
}
}
dir(x, y) {
this.xspeed = x;
this.yspeed = y;
}
sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
death() {
for (let i = 0; i < this.tail.length; i++) {
let pos = this.tail[i];
let d = this.sketch.dist(this.x, this.y, pos.x, pos.y);
if (d < 1 && !this.win()) {
console.log("starting over");
this.#is_dead = true;
this.sketch.noLoop(); // stop the game
this.showDeathModal(); // Show death modal
this.reset();
}
}
}
reset() {
this.#x = 0;
this.#y = 0;
this.#xspeed = 1;
this.#yspeed = 0;
this.#total = 0;
this.#tail = [];
}
// Function to check if the snake has won
win() {
let snakeLength = this.total + 1;
if (snakeLength === (this.width * this.height) / (this.scl * this.scl)) {
return true;
} else {
return false;
}
}
update() {
// Update the tail of the snake
this.#tail.unshift(this.sketch.createVector(this.x, this.y)); // Add the current position to the beginning of the tail array
this.#tail = this.tail.slice(0, this.total); // Keep the tail length equal to the total
// Update the scores
if (!this.is_dead) {
let topology = this.topology;
let snakeLength = this.total + 1; // Calculate the snake length
sessionStorage.setItem("currentScore", snakeLength);
// Calculate the highest score
let highestScore =
parseInt(localStorage.getItem("highestScore_" + topology)) || 1;
if (snakeLength > highestScore) {
highestScore = snakeLength;
localStorage.setItem("highestScore_" + topology, highestScore); // Update highest score in localStorage
}
}
// update the head of the snake
this.#x = this.x + this.xspeed * this.scl;
this.#y = this.y + this.yspeed * this.scl;
// update framerate (update speed of 1 every 5 points with initial speed of 10) and for a maximum of 30
if ((this.total + 1) % 5 === 0) {
this.sketch.frameRate(Math.max(10 + Math.floor((this.total + 1) / 5)));
}
switch (this.topology) {
case "square":
// Square topology
this.#x = this.sketch.constrain(this.x, 0, this.width - this.scl);
this.#y = this.sketch.constrain(this.y, 0, this.height - this.scl);
break;
case "cylinder":
// Cylinder topology
this.#x = this.x >= 0 ? this.x % this.width : this.width - this.scl;
this.#y = this.sketch.constrain(this.y, 0, this.height - this.scl);
break;
case "moebius":
// Moebius strip topology
let tmp_x = this.x >= 0 ? this.x % this.width : this.width - this.scl;
if (tmp_x === this.x) {
this.#y = this.sketch.constrain(this.y, 0, this.height - this.scl);
this.#x = tmp_x;
} else {
this.#x = tmp_x;
this.#y = this.height - this.scl - this.y;
}
break;
case "torus":
// Torus topology
this.#x = this.x >= 0 ? this.x % this.width : this.width - this.scl;
this.#y = this.y >= 0 ? this.y % this.height : this.height - this.scl;
break;
case "klein":
// Klein bottle topology
let tmp_y = this.y >= 0 ? this.y % this.height : this.height - this.scl;
if (tmp_y === this.y) {
this.#x = this.x >= 0 ? this.x % this.width : this.width - this.scl;
this.#y = tmp_y;
} else {
this.#y = tmp_y;
this.#x = this.width - this.scl - this.x;
}
break;
default:
console.log("Topology not found");
break;
}
}
show() {
let head_color = this.sketch.color(195, 232, 141); // to change ?
let last_color = this.sketch.color(49, 128, 25); // to change ?
for (let i = 0; i < this.tail.length; i++) {
let inter = this.sketch.map(i + 1, 0, this.tail.length, 0, 1);
let color = this.sketch.lerpColor(head_color, last_color, inter);
this.sketch.fill(color);
this.sketch.rect(this.tail[i].x, this.tail[i].y, this.scl, this.scl);
}
this.sketch.fill(head_color);
this.sketch.rect(this.x, this.y, this.scl, this.scl);
}
changeDirection(dir) {
let x, y;
[x, y] = this.directions[dir];
// allow only 90degs turns
if (this.xspeed * x >= 0 && this.yspeed * y >= 0) {
this.dir(x, y);
}
}
// Call this function when the snake dies
showDeathModal() {
// Show the modal (using Bootstrap's modal functionality)
let deathModal = new bootstrap.Modal(document.getElementById("deathModal"));
deathModal.show();
}
// Call this function when the snake wins
showWinModal() {
let winModal = new bootstrap.Modal(document.getElementById("winModal"));
winModal.show();
}
}