-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathceleste-room-2.lua
584 lines (510 loc) · 15.4 KB
/
celeste-room-2.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
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
-- ~celeste~
-- matt thorson + noel berry
-- globals --
-------------
room = {x = 0, y = 0}
objects = {}
types = {}
freeze = 0
will_restart = false
delay_restart = 0
k_left = 0
k_right = 1
k_up = 2
k_down = 3
k_jump = 4
k_dash = 5
-- entry point --
-----------------
function _init()
deaths = 0
max_djump = 1
load_room(1, 0)
end
function level_index()
return room.x % 8 + room.y * 8
end
-- player entity --
-------------------
player = {
init = function(this)
this.p_jump = false
this.p_dash = false
this.grace = 0
this.jbuffer = 0
this.djump = max_djump
this.dash_time = 0
this.dash_target = {x = 0, y = 0}
this.dash_accel = {x = 0, y = 0}
this.hitbox = {x = 1, y = 3, w = 6, h = 5}
end,
update = function(this)
this.type.update_inner(
this,
{
btn_k_left = btn(k_left),
btn_k_right = btn(k_right),
btn_k_up = btn(k_up),
btn_k_down = btn(k_down),
btn_k_jump = btn(k_jump),
btn_k_dash = btn(k_dash)
},
{
is_solid_1_0 = this.is_solid(1, 0),
is_solid_neg_1_0 = this.is_solid(-1, 0),
is_solid_0_1 = this.is_solid(0, 1),
is_solid_neg_3_0 = this.is_solid(-3, 0),
is_solid_3_0 = this.is_solid(3, 0),
y_more_than_128 = this.y > 128,
spikes_at_xspd_low = spikes_at(
this.x + this.hitbox.x,
this.y + this.hitbox.y,
this.hitbox.w,
this.hitbox.h,
-1,
0
),
spikes_at_xspd_high = spikes_at(
this.x + this.hitbox.x,
this.y + this.hitbox.y,
this.hitbox.w,
this.hitbox.h,
1,
0
),
spikes_at_yspd_low = spikes_at(
this.x + this.hitbox.x,
this.y + this.hitbox.y,
this.hitbox.w,
this.hitbox.h,
0,
-1
),
spikes_at_yspd_high = spikes_at(
this.x + this.hitbox.x,
this.y + this.hitbox.y,
this.hitbox.w,
this.hitbox.h,
0,
1
)
}
)
end,
update_inner = function(this, input_flags, pos_flags)
local input = input_flags.btn_k_right and 1 or (input_flags.btn_k_left and -1 or 0)
-- spikes collide
if
(this.spd.x <= 0 and pos_flags.spikes_at_xspd_low) or (this.spd.x >= 0 and pos_flags.spikes_at_xspd_high) or
(this.spd.y <= 0 and pos_flags.spikes_at_yspd_low) or
(this.spd.y >= 0 and pos_flags.spikes_at_yspd_high)
then
kill_player(this)
end
-- bottom death
if pos_flags.y_more_than_128 then
kill_player(this)
end
local on_ground = pos_flags.is_solid_0_1
local jump = input_flags.btn_k_jump and not this.p_jump
this.p_jump = input_flags.btn_k_jump
if jump then
this.jbuffer = 4
elseif this.jbuffer > 0 then
this.jbuffer = this.jbuffer - (1)
end
local dash = input_flags.btn_k_dash and not this.p_dash
this.p_dash = input_flags.btn_k_dash
if on_ground then
this.grace = 6
if this.djump < max_djump then
this.djump = max_djump
end
elseif this.grace > 0 then
this.grace = this.grace - (1)
end
if this.dash_time > 0 then
this.dash_time = this.dash_time - (1)
this.spd.x = appr(this.spd.x, this.dash_target.x, this.dash_accel.x)
this.spd.y = appr(this.spd.y, this.dash_target.y, this.dash_accel.y)
else
-- move
local maxrun = 1
local accel = 0.6
local deccel = 0.15
if not on_ground then
accel = 0.4
end
if abs(this.spd.x) > maxrun then
this.spd.x = appr(this.spd.x, sign(this.spd.x) * maxrun, deccel)
else
this.spd.x = appr(this.spd.x, input * maxrun, accel)
end
--facing
if this.spd.x ~= 0 then
this.flip.x = (this.spd.x < 0)
end
-- gravity
local maxfall = 2
local gravity = 0.21
if abs(this.spd.y) <= 0.15 then
gravity = gravity * (0.5)
end
-- wall slide
if (input == 1 and pos_flags.is_solid_1_0) or (input == -1 and pos_flags.is_solid_neg_1_0) then
maxfall = 0.4
end
if not on_ground then
this.spd.y = appr(this.spd.y, maxfall, gravity)
end
-- jump
if this.jbuffer > 0 then
if this.grace > 0 then
-- normal jump
this.jbuffer = 0
this.grace = 0
this.spd.y = -2
else
-- wall jump
local wall_dir = (pos_flags.is_solid_neg_3_0 and -1 or pos_flags.is_solid_3_0 and 1 or 0)
if wall_dir ~= 0 then
this.jbuffer = 0
this.spd.y = -2
this.spd.x = -wall_dir * (maxrun + 1)
end
end
end
-- dash
local d_full = 5
local d_half = d_full * 0.70710678118
if this.djump > 0 and dash then
this.djump = this.djump - (1)
this.dash_time = 4
local v_input = (input_flags.btn_k_up and -1 or (input_flags.btn_k_down and 1 or 0))
if input ~= 0 then
if v_input ~= 0 then
this.spd.x = input * d_half
this.spd.y = v_input * d_half
else
this.spd.x = input * d_full
this.spd.y = 0
end
elseif v_input ~= 0 then
this.spd.x = 0
this.spd.y = v_input * d_full
else
this.spd.x = (this.flip.x and -1 or 1)
this.spd.y = 0
end
freeze = 2
this.dash_target.x = 2 * sign(this.spd.x)
this.dash_target.y = 2 * sign(this.spd.y)
this.dash_accel.x = 1.5
this.dash_accel.y = 1.5
if this.spd.y < 0 then
this.dash_target.y = this.dash_target.y * (.75)
end
if this.spd.y ~= 0 then
this.dash_accel.x = this.dash_accel.x * (0.70710678118)
end
if this.spd.x ~= 0 then
this.dash_accel.y = this.dash_accel.y * (0.70710678118)
end
end
end
-- next level
if this.y < -4 and level_index() < 30 then
next_room()
end
end, --<end update loop
draw = function(this)
-- clamp in screen
if this.x < -1 or this.x > 121 then
this.x = clamp(this.x, -1, 121)
this.spd.x = 0
end
spr(1, this.x, this.y, 1, 1, this.flip.x, this.flip.y)
end
}
player_spawn = {
tile = 1,
init = function(this)
this.spr = 3
this.target = {x = this.x, y = this.y}
this.y = 128
this.spd.y = -4
this.state = 0
this.delay = 0
this.solids = false
end,
update = function(this)
-- jumping up
if this.state == 0 then
-- falling
if this.y < this.target.y + 16 then
this.state = 1
this.delay = 3
end
elseif this.state == 1 then
-- landing
this.spd.y = this.spd.y + (0.5)
if this.spd.y > 0 and this.delay > 0 then
this.spd.y = 0
this.delay = this.delay - (1)
end
if this.spd.y > 0 and this.y > this.target.y then
this.y = this.target.y
this.spd = {x = 0, y = 0}
this.state = 2
this.delay = 5
end
elseif this.state == 2 then
this.delay = this.delay - (1)
this.spr = 6
if this.delay < 0 then
destroy_object(this)
init_object(player, this.x, this.y)
end
end
end
}
add(types, player_spawn)
-- object functions --
-----------------------
function init_object(type, x, y)
local obj = {}
obj.type = type
obj.collideable = true
obj.solids = true
obj.spr = type.tile
obj.flip = {x = false, y = false}
obj.x = x
obj.y = y
obj.hitbox = {x = 0, y = 0, w = 8, h = 8}
obj.spd = {x = 0, y = 0}
obj.rem = {x = 0, y = 0}
obj.is_solid = function(ox, oy)
return solid_at(obj.x + obj.hitbox.x + ox, obj.y + obj.hitbox.y + oy, obj.hitbox.w, obj.hitbox.h) or
obj.check(fall_floor, ox, oy) or
obj.check(fake_wall, ox, oy)
end
obj.collide = function(type, ox, oy)
local other
for i = 1, count(objects) do
other = objects[i]
if
other ~= nil and other.type == type and other ~= obj and other.collideable and
other.x + other.hitbox.x + other.hitbox.w > obj.x + obj.hitbox.x + ox and
other.y + other.hitbox.y + other.hitbox.h > obj.y + obj.hitbox.y + oy and
other.x + other.hitbox.x < obj.x + obj.hitbox.x + obj.hitbox.w + ox and
other.y + other.hitbox.y < obj.y + obj.hitbox.y + obj.hitbox.h + oy
then
return other
end
end
return nil
end
obj.check = function(type, ox, oy)
return obj.collide(type, ox, oy) ~= nil
end
obj.move = function(ox, oy)
local amount
-- [x] get move amount
obj.rem.x = obj.rem.x + (ox)
amount = flr(obj.rem.x + 0.5)
obj.rem.x = obj.rem.x - (amount)
obj.move_x(amount, 0)
-- [y] get move amount
obj.rem.y = obj.rem.y + (oy)
amount = flr(obj.rem.y + 0.5)
obj.rem.y = obj.rem.y - (amount)
obj.move_y(amount)
end
obj.move_x = function(amount, start)
if obj.solids then
local step = sign(amount)
for i = start, abs(amount) do
if not obj.is_solid(step, 0) then
obj.x = obj.x + (step)
else
obj.spd.x = 0
obj.rem.x = 0
break
end
end
else
obj.x = obj.x + (amount)
end
end
obj.move_y = function(amount)
if obj.solids then
local step = sign(amount)
for i = 0, abs(amount) do
if not obj.is_solid(0, step) then
obj.y = obj.y + (step)
else
obj.spd.y = 0
obj.rem.y = 0
break
end
end
else
obj.y = obj.y + (amount)
end
end
add(objects, obj)
if obj.type.init ~= nil then
obj.type.init(obj)
end
return obj
end
function destroy_object(obj)
del(objects, obj)
end
function kill_player(obj)
deaths = deaths + (1)
destroy_object(obj)
restart_room()
end
-- room functions --
--------------------
function restart_room()
will_restart = true
delay_restart = 15
end
function next_room()
if room.x == 7 then
load_room(0, room.y + 1)
else
load_room(room.x + 1, room.y)
end
end
function load_room(x, y)
--remove existing objects
foreach(objects, destroy_object)
--current room
room.x = x
room.y = y
-- entities
for tx = 0, 15 do
for ty = 0, 15 do
local tile = mget(room.x * 16 + tx, room.y * 16 + ty)
foreach(
types,
function(type)
if type.tile == tile then
init_object(type, tx * 8, ty * 8)
end
end
)
end
end
end
-- update function --
-----------------------
function _update()
-- cancel if freeze
if freeze > 0 then
freeze = freeze - 1
return
end
-- restart (soon)
if will_restart and delay_restart > 0 then
delay_restart = delay_restart - (1)
if delay_restart <= 0 then
will_restart = false
load_room(room.x, room.y)
end
end
-- update each object
foreach(
objects,
function(obj)
if obj.spd.x ~= 0 or obj.spd.y ~= 0 then
obj.move(obj.spd.x, obj.spd.y)
end
if obj.type.update ~= nil then
obj.type.update(obj)
end
end
)
end
-- drawing functions --
-----------------------
function _draw()
if freeze > 0 then
return
end
-- reset all palette values
pal()
-- clear screen
local bg_col = 0
if flash_bg then
bg_col = frames / 5
elseif new_bg ~= nil then
bg_col = 2
end
rectfill(0, 0, 128, 128, bg_col)
-- draw bg terrain
map(room.x * 16, room.y * 16, 0, 0, 16, 16, 4)
-- draw terrain
map(room.x * 16, room.y * 16, 0, 0, 16, 16, 2)
-- draw objects
foreach(
objects,
function(o)
draw_object(o)
end
)
-- draw fg terrain
map(room.x * 16, room.y * 16, 0, 0, 16, 16, 8)
end
function draw_object(obj)
if obj.type.draw ~= nil then
obj.type.draw(obj)
end
end
-- helper functions --
----------------------
function clamp(val, a, b)
return max(a, min(b, val))
end
function appr(val, target, amount)
return val > target and max(val - amount, target) or min(val + amount, target)
end
function sign(v)
return v > 0 and 1 or v < 0 and -1 or 0
end
function solid_at(x, y, w, h)
return tile_flag_at(x, y, w, h, 0)
end
function tile_flag_at(x, y, w, h, flag)
for i = max(0, flr(x / 8)), min(15, (x + w - 1) / 8) do
for j = max(0, flr(y / 8)), min(15, (y + h - 1) / 8) do
if fget(tile_at(i, j), flag) then
return true
end
end
end
return false
end
function tile_at(x, y)
return mget(room.x * 16 + x, room.y * 16 + y)
end
function spikes_at(x, y, w, h, xspd, yspd)
for i = max(0, flr(x / 8)), min(15, (x + w - 1) / 8) do
for j = max(0, flr(y / 8)), min(15, (y + h - 1) / 8) do
local tile = tile_at(i, j)
if tile == 17 and ((y + h - 1) % 8 >= 6 or y + h == j * 8 + 8) and yspd >= 0 then
return true
elseif tile == 27 and y % 8 <= 2 and yspd <= 0 then
return true
elseif tile == 43 and x % 8 <= 2 and xspd <= 0 then
return true
elseif tile == 59 and ((x + w - 1) % 8 >= 6 or x + w == i * 8 + 8) and xspd >= 0 then
return true
end
end
end
return false
end