-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmaps.lua
544 lines (529 loc) · 11.6 KB
/
maps.lua
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
origG = G
origRGX = RGX
origGX = GX
map = obj {
nam = 'map';
var {
data = {};
nr = 1;
prev = 0;
map = {};
d = 0;
};
dist = function(s, n)
local nn = s.d
if n then s.d = n end
return nn
end;
mirror = function(s)
return maps[s.nr].mirror
end;
select = function(s, n)
if n then
if s.nr and maps[s.nr] and maps[s.nr].exit then maps[s.nr].exit(s.data) end
s.prev = s.nr
s.nr = n;
if n == CONTMAP then
save_music(game)
set_music 'snd/nothing.ogg'
end
else
n = s.nr
end
G = origG
RGX = origRGX
GX = origGX
if s.title then
sprite.free(s.title);
end
s.title = sprite.text(fn, string.format("%d: %s", n, _(maps[n].title)), "black");
if hero:state() == DEAD or #s.map == 0 then
sound_init()
if maps[n].color then
bg_color = maps[n].color;
else
bg_color = '#9cccfc';
end
local y, x
s.map = {}
for y = 1, 30 do
s.map[y] = {}
for x = 1, 40 do
local c = string.sub(maps[n].map[y], x, x);
if c == '#' then
c = BLOCK
elseif c == '.' then
c = INVI
elseif c == '@' then
c = SNOW
elseif c == '^' then
c = FAKE
elseif c == '~' then
c = WATER
elseif c == '*' then
c = EMERGENCY
elseif c == '%' then
c = SEMIBLOCK
elseif c == '=' then
c = BRIDGE
elseif c == '-' then
c = ROPE
elseif c == 'm' then
c = MINE
elseif c == '>' then
c = nil
s.map.x = (x - 1) * BW
s.map.y = (y) * BH - hero.h
elseif c == '+' then
c = HEART
elseif c == ' ' then
c = 0
else
c = c -- same character
end
table.insert(s.map[y], { c })
end
end
end
if hero:state() == DEAD then
hero.x = s.map.x
hero.y = s.map.y
map.data = {}
hero:state(WALK);
hero.speed_x = 0
hero.dir = 1
end
end;
next = function(s)
local n
n = s.nr + 1;
if s.nr == CONTMAP then
n = s.prev
game_lifes = LIVES
game:dist(0)
hero:state(DEAD)
restore_music(game)
end
s.data = {}
s.map = {}
s:select(n)
s:dist(0)
if hero:state() ~= DEAD then
hero.x = 0
hero.dir = 1
end
end;
pos2block = function(s, x, y)
x = math.floor(x / BW);
y = math.floor(y / BH);
return x, y
end;
show = function(s)
local y, x
for y=0, 29 do
for x=0, 39 do
local c = s:cell(x, y);
if c[1] == BLOCK or c[1] == FAKE then
sprite.fill(sprite.screen(), x * 16, y * 16, 16 - 1, 16 - 1, 'black');
elseif c[1] == SNOW then
sprite.fill(sprite.screen(), x * 16, y * 16, 16, 16, 'white');
elseif c[1] == SEMIBLOCK then
if (not c.move) or (c.move < SEMI_TO) then
sprite.fill(sprite.screen(), x * 16, y * 16, 16 - 1, 16 - 1, SEMICOL);
end
elseif c[1] == WATER then
sprite.fill(sprite.screen(), x * 16, y * 16, 16, 16, 'blue');
elseif c[1] == EMERGENCY or c[1] == MINE then
sprite.fill(sprite.screen(), x * 16 + 1, y * 16 + 1, 16 - 2, 16 - 2, 'red');
elseif c[1] == BRIDGE then
sprite.fill(sprite.screen(), x * 16, y * 16, 16 - 1, 8, 'black');
elseif c[1] == ROPE then
sprite.fill(sprite.screen(), x * 16, y * 16, 16 - 1, 4, 'black');
elseif c[1] == HEART then
if not heart_blink then heart_blink = 0 end
if math.floor(heart_blink / 20) % 2 ~= 0 then
sprite.draw(heart_bonus_spr, sprite.screen(), x * 16, y * 16);
end
heart_blink = heart_blink + 1
if heart_blink >= 40 then heart_blink = 0 end
end
end
end
end;
cell = function(s, x, y)
if x < 0 or y < 0 or x >= 40 or y >= 30 then
return
end
return s.map[y + 1][x + 1];
end,
block = function(s, x, y)
if x < 0 or y < 0 or x >= 40 or y >= 30 then
return
end
local l = s.map[y + 1];
local c = l[x + 1][1];
return c
end;
is_fall = function(s, x, y, w, dy)
local xx
if not dy then dy = 0 end
local rc = true
local water = false
local emerg = false
if game_state == INTRO then
return false
end
for xx = 0, math.floor((w - 1) / BW) do
local bx, by = s:pos2block(x + xx * BW, y)
local c = s:block(bx, by)
if c == BLOCK or c == SNOW or c == INVI then
rc = false
elseif c == SEMIBLOCK then
c = s:cell(bx, by)
if not c.move then c.move = 0 end
if c.move < SEMI_TO then
rc = false
end
elseif c == WATER then
water = true
elseif c == EMERGENCY or c == MINE then
hero:state(FLY)
return false
elseif c == BRIDGE and dy >= 0 and y - dy <= by * BH then
rc = false
elseif c == ROPE and dy == 0 and y - dy <= by * BH and hero.speed_x ~= 0 then
rc = false
elseif c == HEART then
game_lifes = game_lifes + 1
sound.play(bonus_snd)
c = s:cell(bx, by)
c[1] = 0
end
end
if rc and dy >= 0 and water then
hero:state(DROWN)
rc = false
end
return rc
end;
is_move = function(s, x, y, h)
local yy
local rc = true
if game_state == INTRO then
return true
end
for yy = 0, math.floor((h - 1) / BH) do
local c = s:cell(s:pos2block(x, y + yy*BH))
if c then
if c[1] == BLOCK or (c[1] == SEMIBLOCK and (not c.move or c.move < SEMI_TO)) then
return false
end
if c[1] == SNOW or c[1] == INVI then
return false
end
if c[1] == EMERGENCY or c[1] == MINE then
hero:state(FLY)
return false
elseif c[1] == HEART then
game_lifes = game_lifes + 1
sound.play(bonus_snd)
c[1] = 0
end
end
end
return true
end;
move = function(s, x, y, dx, dy, w, h)
local block_x = false
local block_y = false
local xx = x
local yy = y
if dx >= 0 then
xx = xx + w
end
if dy >= 0 then
yy = yy + h
end
if s:is_fall(x, yy + dy, w, dy) then
y = y + dy
else
y = math.floor((yy + dy) / BH) * BH
if dy >= 0 then
y = y - h
else
y = y + BH
end
block_y = true
end
if s:is_move(xx + dx, y, h) then
x = x + dx
else
x = math.floor((xx + dx) / BW) * BW
if dx >= 0 then
x = x - w
else
x = x + BW
end
block_x = true
end
if x < -hero.w / 2 and not map:mirror() then
x = -hero.w / 2
end
if dx >= 0 then
x = math.floor(x)
else
x = math.ceil(x)
end
y = math.floor(y)
return x, y, block_x, block_y
end;
life = function(s)
local y, x
for y = 1, 30 do
for x = 1, 40 do
local yy
local c = s.map[y][x]
if c.move then c.move = c.move + 1 end
if c[1] == MINE and not c.activated then
if hero:distance(x * BW + BW / 2, y * BH + BH /2) < MINE_DIST then
c.activated = true
sound.play(beep_snd)
c.move = 0
end
end
if c[1] == SEMIBLOCK and c.move then
if c.move >= SEMI_TO then
if not c.y then
c.y = (y - 1)* BH
end
c.y = c.y + (c.move - SEMI_TO) * G;
sprite.fill(sprite.screen(), (x - 1) * BW,
c.y,
16 - 1,
16 - 1, SEMICOL);
end
if c.move and c.y and c.y > 480 then
c[1] = 0
end
elseif c[1] == EMERGENCY or (c[1] == MINE and c.activated) then
if not c.step then
sprite.fill(sprite.screen(), (x - 1) * BW, (y - 1) * BH, BW - 1, BH/2, 'yellow');
end
c.step = not c.step
end
if c[1] == MINE and c.activated and c.move > MINE_TO then
explode.add(map.data, x - 1, y - 1)
end
end
end
explode.draw(s.data)
if maps[s.nr].life then
maps[s.nr].life(s.data)
end
end;
before = function(s)
if maps[s.nr].before then
maps[s.nr].before(s.data)
end
end;
after = function(s)
if maps[s.nr].after then
maps[s.nr].after(s.data)
end
end
}
ending_txt = {
"CONGRATULATIONS!",
"YOU HELPED THE CAT TO ESCAPE THE EVIL STATION!",
"THANK YOU FOR YOUR EFFORTS!",
":)",
"CODE: PETER KOSYH",
"ENGINE: PETER KOSYH",
"http://instead.syscall.ru",
"LEVELS: PETER KOSYH AND ANDREW LOBANOV",
"MUSIC: RoccoW",
"INPIRED BY GAMES:",
"Snoopy (1984)",
"Sqrzx",
"Giana's Return...",
"YOU CAN ENTER LEVEL NUMBER WITH KEYS 0..9",
"AND PRESS RETURN",
}
explode = {
add = function(s, x, y, m)
if not s.mines then
s.mines = {}
end
local v = {}
v.x = x * BW + BW / 2 - 2
v.y = y * BH + BH / 2 - 2
v.step = 0
local c = map:cell(x, y)
c[1] = 0
sound.play(boom_snd)
if m then
table.insert(m, v)
else
table.insert(s.mines, v)
end
end;
draw = function(s)
local SP = 8
if not s.mines then return end
local k,v
local mines = {}
for k, v in ipairs(s.mines) do
local x, y = v.x, v.y
local xx, yy
local p = v.step
local i
if p then
v.step = v.step + 1
p = v.step
for i = 1, 8 do
if not v[i] then
xx = x + p * SP * math.cos(3.14 * i / 4)
yy = y + p * SP * math.sin(3.14 * i / 4)
sprite.fill(sprite.screen(), xx, yy, 4, 4, 'black')
local c = map:cell(map:pos2block(xx, yy))
if c and c[1] ~= 0 and c[1] and c[1] ~= ' ' and c[1] ~= HEART then
v[i] = true
elseif hero:collision(xx, yy, 4, 4) then
hero:state(FLY)
end
if c and (c[1] == EMERGENCY or c[1] == MINE) then
local x1, y1 = map:pos2block(xx, yy)
explode.add(s, x1, y1, mines)
end
end
end
if p * SP > 40 * BW then
v.step = false
else
table.insert(mines, v)
end
end
end;
s.mines = mines
end
}
laser = {
on = function(x, y, len)
local w, h
local c = 'red'
if rnd(50) > 25 then
c = 'yellow'
end
if len > 0 then
x = x * BW + BW / 2 - 2
y = y * BH
w = 3
h = len * BH
else
x = x * BW
y = y * BH + BH/2 - 2
w = -len * BW
h = 3
end
sprite.fill(sprite.screen(), x, y, w, h, c)
laser_play()
if hero:collision(x, y, w, h) then
hero:state(FLY)
end
end;
off = function()
laser_mute();
end
}
snake = {
step = function(s)
local k, v
local dx = 0
local dy = 0
if not s.pos then s.pos = 1 end
local to = s.path[s.pos]
local x, y = s.snake[1][1], s.snake[1][2]
if to[1] == x and to[2] == y then
s.pos = s.pos + 1
to = s.path[s.pos]
if s.pos > #s.path then
s.pos = 1
to = s.path[s.pos]
end
end
if to[1] < x then dx = -1 elseif to[1] > x then dx = 1 end
if to[2] < y then dy = -1 elseif to[2] > y then dy = 1 end
x = x + dx
y = y + dy
for k, v in ipairs(s.snake) do
local ox, oy = v[1], v[2]
v[1], v[2] = x, y
x, y = ox, oy
end
end;
draw = function(s)
local k, v
for k, v in ipairs(s.snake) do
if k == 1 then
sprite.fill(sprite.screen(), v[1] * BW, v[2] * BH, BW, BH, 'yellow');
if not snake_step then
sprite.fill(sprite.screen(), v[1] * BW + 2, v[2] * BH + 2, BW - 4, BH - 4, 'red');
end
snake_step = not snake_step
else
sprite.fill(sprite.screen(), v[1] * BW, v[2] * BH, BW - 1, BH - 1, 'crimson');
end
if hero:collision(v[1] * BW, v[2] * BH, BW, BH) then
hero:state(FLY)
end
end
end;
}
lift = {
activate = function(s, x, y, w, dir, u, d)
s.y = y
s.x = x
s.dir = dir
s.w = w
s.hi = u
s.low = d
end;
draw = function(s)
local x, y
for x = s.x, s.x + s.w - 1 do
local c = map:cell(x, math.floor(s.y))
c[1] = 0
end
local on = false
if s.dir > 0 and hero:collision(s.x * BW, math.floor(s.y * BH) - BH, s.w * BW, BH)
and hero.y + hero.h <= s.y * BH and
map:is_fall(hero.x, hero.y + hero.h, hero.w, s.dir)
then
on = true
end
s.y = s.y + s.dir
if math.floor(s.y) < s.hi then
s.dir = -1 * s.dir
s.y = s.hi
end
if math.floor(s.y) > s.low then
s.dir = -1 * s.dir
s.y = s.low
end
for x = s.x, s.x + s.w - 1 do
local c = map:cell(x, math.floor(s.y))
c[1] = BRIDGE
end
if s.dir < 0 and hero:collision(s.x * BW, math.floor(s.y * BH), s.w * BW, BH) and
hero.y + hero.h - BH < s.y * BH
then
on = true
end
if on then
map:move(hero.x, hero.y, 0, s.y * BH - (hero.y + hero.h), hero.w, hero.h)
hero.y = math.floor(s.y) * BH - hero.h
end
end
}
dofile "levels.lua"
dofile "end.lua"