forked from hackclub/sprig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShooting-Stars!.js
422 lines (385 loc) · 7.6 KB
/
Shooting-Stars!.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/*
@title: Shooting Stars!
@author: Sapmix
@tags: []
@addedOn: 2024-11-11
*/
const player = "p"
const enemy = "e"
const laser = "l"
const bkg = "b";
const obstacle = "o";
let is_enemy_hit = false;
let enemies = [];
let currentObs = [];
let numEnemies = 3;
let numObs = 0;
let initialized = false;
let dead = false;
let loop = 0;
let points = 0;
setLegend(
[enemy, bitmap`
................
................
................
................
................
.......0........
......020.......
.....02220......
.....02220......
...001000L00....
..011LLLLLLL0...
.013LLL3LLL3L0..
..001LLLLLL00...
....0606060.....
................
................`],
[player, bitmap`
.......0........
......050.......
.....05550......
.....05550......
.....05250......
.....05250......
.....05550......
.....05550......
....0755550.....
...077555550....
...075555550....
...075555550....
...075555550....
...075555550....
....0333990.....
......966.......`],
[laser, bitmap`
................
................
................
.......4........
.......4........
.......4........
.......4........
................
................
................
................
................
................
................
................
................`],
/*[bkg1, bitmap`
0000000000000000
0000000000000000
0002000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000002000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000200000000000
0000000000020000
0000000000000000
0000000000000000
0000000000000000`],
[bkg2, bitmap`
0000000000000000
0000000000000000
000000LLL0000000
00000L1L1L000000
00000L111L002000
00020LL1L0000000
000000LL00000000
0000000000000000
0000000000200000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0002000000000000
0000000000000000
0000000000000000`],
[bkg3, bitmap`
0000000000000000
0000000000000000
0000000000000000
000LLLLL00020000
00LL11LLL0000000
00L11111L0000000
00L11L11L0002000
00L1111LL0000000
000LLLLL00000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0200000000002000
0000002000000000
0000000000000000`],*/
[bkg, bitmap`
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000`],
[obstacle, bitmap`
9999999999999999
9999999999999999
9999999999999999
9999999999999999
9999999999999999
9999999999999999
9999999999999999
9999999999999999
9999999999999999
9999999999999999
9999999999999999
9999999999999999
9999999999999999
9999999999999999
9999999999999999
9999999999999999`]
);
setSolids([obstacle, laser]);
let level = 0
const levels = [
map`
............
............
............
............
............
............
............
............
............`
/*{ enemies: 6, map: map`
............
............
.oo..o..oo..
............
............
............
............
............
............`, obs: [{ x: 1, y: 2 }, { x: 2, y: 2 }, { x: 5, y: 2 }, { x: 8, y: 2 }, { x: 9, y: 2 }], win: false },
{ enemies: 0, map: map`
............
............
............
............
............
............
............
............
............`, obs: [], win: true },*/
]
setBackground(bkg);
setPushables({})
onInput("d", () => {
getFirst(player).x += 1
})
onInput("a", () => {
getFirst(player).x -= 1
})
onInput("l", () => {
addSprite(getFirst(player).x, getFirst(player).y, laser);
})
onInput("j", () => {
clearInterval(loop);
clearText();
is_enemy_hit = false;
enemies = [];
currentObs = [];
numObs = 0;
numEnemies = 3;
initialized = false;
points = 0;
level = 0;
dead = false;
initialize();
loop = setInterval(update, 125);
})
afterInput(_ => {})
initialize();
loop = setInterval(update, 125);
function initialize() {
initialized = false;
// First get player coordinates before map gets cleared
let playy = 8;
let playx = 6;
if (getFirst(player) !== undefined) {
let play = getFirst(player);
playy = play.y;
playx = play.x;
}
setMap(levels[0]);
clearObs();
addObs();
// Add player
addSprite(playx, playy, player);
// Add enemies
addEnemy();
initialized = true;
}
function clearObs() {
// Function to clear previous obstacles
currentObs = [];
for (let obs in getAll(obstacle)) {
clearTile(obs.x, obs.y);
};
}
function addObs() {
if (numObs < 8) {
numObs += 1
}
// Time to generate new positions for the obstacles!
for (let a = 0; a < numObs; a++) {
let x = 0;
while (true) {
x = Math.floor(Math.random() * (12 - 0) + 0);
if (typeof currentObs.find(e => (e.x == x)) === "undefined") {
break;
}
}
let y = 2;
currentObs.push({ x: x, y: y});
addSprite(x, y, obstacle);
}
}
function addEnemy() {
if (numEnemies < 12) {
numEnemies += 1
}
for (let a = 0; a < numEnemies; a++) {
let x = 0;
while (true) {
x = Math.floor(Math.random() * (12 - 0) + 0);
if (typeof enemies.find(e => (e.x == x)) === "undefined") {
break;
}
}
enemies.push({ x: x, y: 1, direction: 1 });
addSprite(x, 1, enemy);
}
}
function update() {
if (dead) {
death();
return;
}
if (initialized) {
checkState();
drawText();
updateLasers();
updateEnemies();
}
}
function death() {
clearInterval(loop);
setMap(levels[0]); // load map, map is updated dynamically and randomly so level variable is only used for counting!
clearText();
addText(`You died!\nXP: ${points}`, { x: 6, y: 5, color: color`6` });
addText(`Press j to restart`, { x: 1, y: 10, color: color`6` });
}
function checkState() {
if (enemies.length == 0) {
level++;
initialize();
}
}
function detectLaserCollision(x, y) {
for (var sprite1 of getTile(x, y)) {
// Obstacle
obstacleCollision(sprite1, x, y);
// Enemy
enemyCollision(sprite1, x, y);
}
}
function collideWithPlayer(x, y) {
if (getTile(x, y).find(e => e.type === 'p')) {
dead = true;
}
}
function obstacleCollision(s, x, y) {
if (s.type != "l") return;
for (let ob of currentObs) {
if (s.x == ob.x && s.y - 1 == ob.y) {
clearTile(x, y);
}
}
}
function enemyCollision(s, x, y) {
if (s.type != "e") return;
let enemies_on_tile = enemies.length;
enemies = enemies.filter(function(o) {
return !(o.x == x && o.y == y);
});
clearTile(x, y);
enemies_on_tile = enemies_on_tile - enemies.length;
points += enemies_on_tile
}
function drawText() {
clearText();
//if (!levels[level]) {
// Text overflows if level is above 9, so fix x coordinates dynamically
let levell = level.toString().length-1;
addText(`XP: ${points}`, { x: 0, y: 0, color: color`6` });
addText(`Level: ${level}`, { x: 12-levell, y: 0, color: color`6` });
/*} else {
addText(`You won!!!`, { x: 6, y: 7, color: color`6` });
}*/
}
function updateEnemies() {
let i = 0;
for (let e of enemies) {
if (e.direction == -1) {
if (e.x <= 0) {
e.direction = 1
if (e.y > 0) {
e.y += 1;
}
}
e.x += e.direction
} else if (e.direction == 1) {
if (e.x >= width() - 1) {
e.direction = -1
if (e.y > 0) {
e.y += 1;
}
}
e.x += e.direction
}
let sprite = getAll(enemy)[i];
sprite.y = e.y;
sprite.x = e.x;
collideWithPlayer(e.x, e.y);
i += 1;
}
}
function updateLasers() {
for (let l of getAll(laser)) {
// Also handles collisions and points
detectLaserCollision(l.x, l.y);
if (l.y == 0 && getTile(l.x, l.y).length <= 1) {
clearTile(l.x, l.y);
};
l.y -= 1;
}
}