-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.lua
More file actions
596 lines (499 loc) · 12.8 KB
/
Copy pathgame.lua
File metadata and controls
596 lines (499 loc) · 12.8 KB
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
585
586
587
588
589
590
591
592
593
594
595
596
local gravity = nil
local bob = nil
local BOB_STATE_JUMP = 0
local BOB_STATE_FALL = 1
local BOB_STATE_HIT = 2
local BOB_JUMP_VELOCITY = -5
local BOB_MOVE_VELOCITY = 10
local Bob = {
state = BOB_STATE_FALL,
stateTime = 0,
accel = 0,
velocity = nil,
y_camera = 0,
coins = 0,
prev = {}
}
Bob.create = function(data)
local dither = {0xA5A5, 0x8181, 0x8080, 0x0101, 0x1010}
local b = new(Bob, data)
b.velocity = vec2.create()
for i = 1, 5 do
table.insert(b.prev, {
x = b.x,
y = b.y,
d = dither[i]
})
end
return b
end
Bob.hitSquirrel = function(self)
self.velocity = vec2.create()
self.state = BOB_STATE_HIT
self.stateTime = 0
end
Bob.hitPlatform = function(self)
self.velocity.y = BOB_JUMP_VELOCITY
self.state = BOB_STATE_JUMP
self.stateTime = 0
end
Bob.hitSpring = function(self)
self.velocity.y = BOB_JUMP_VELOCITY * 1.5
self.state = BOB_STATE_JUMP
self.stateTime = 0
end
Bob._update = function(self)
local touch = ctrl.touching(0)
if touch == nil then
elseif touch.x < self.x + self.width * 0.5 then
self.accel = 2
else
self.accel = -2
end
if ctrl.pressing(keys.left) then
self.accel = 2
elseif ctrl.pressing(keys.right) then
self.accel = -2
end
if (self.state ~= BOB_STATE_HIT) then
self.velocity.x = -self.accel / 10 * BOB_MOVE_VELOCITY
end
self.velocity = vec2.add(self.velocity, gravity)
self.x = self.x + self.velocity.x
self.y = self.y + self.velocity.y
if (self.velocity.y > 0 and self.state ~= BOB_STATE_HIT) then
if self.state ~= BOB_STATE_JUMP then
self.state = BOB_STATE_JUMP
self.stateTime = 0
end
end
if (self.velocity.y < 0 and self.state ~= BOB_STATE_HIT) then
if self.state ~= BOB_STATE_FALL then
self.state = BOB_STATE_FALL
self.stateTime = 0
end
end
if self.x < 0 - self.width then
self.x = self.world_width
end
if self.x > self.world_width then
self.x = 0
end
self.stateTime = self.stateTime + tiny.dt
self.y_camera = juice.powIn2(self.y - self.world_height * 0.5, self.y_camera, 0.98)
local prev = self
for i = 1, #self.prev do
self.prev[i].x = juice.linear(prev.x, self.prev[i].x, 0.8)
self.prev[i].y = juice.linear(prev.y, self.prev[i].y, 0.8)
prev = self.prev[i]
end
gfx.camera(0, math.max(self.y_camera, 0))
-- out of screen
if self.y < -16 then
self.y = -60
end
end
Bob._draw = function(self)
local spr_index = 0
if self.velocity.y > 0 then
spr_index = 1
end
for i = #self.prev, 1, -1 do
gfx.dither(self.prev[i].d)
local x, y = self.prev[i].x, self.prev[i].y
spr.draw(spr_index, x, y, self.accel > 0)
end
gfx.dither()
spr.draw(spr_index, self.x, self.y, self.accel > 0)
-- draw score
print(self.coins, 2, 10 + self.y_camera)
end
local EndTitle = {
x = 8,
y = -160,
target_y = 160,
ttl = nil
}
EndTitle._update = function(self)
if not self.ttl then
return
end
if (self.ttl < 0) then
self.ttl = self.ttl + tiny.dt
self.y = juice.powIn2(self.target_y, self.y, 0.98)
if self.ttl > 0 then
self.y = self.target_y
end
else
self.ttl = self.ttl + tiny.dt
end
end
EndTitle._draw = function(self)
if not self.ttl then
return
end
local step = 2
for i = 0, 240, step do
spr.sdraw(self.x + i, self.y + math.cos(4 * math.pi * i / 240 + tiny.t * 8) * 8, 8 + i, 184, step, 33)
end
end
local EndLevel = {
candles = {}
}
EndLevel.create = function(data)
local e = new(EndLevel, data)
local step = 2 * math.pi / 8
for i = 1, 8 do
table.insert(e.candles, {
i = i,
x = math.cos(i * step + tiny.t),
y = math.sin(i * step + tiny.t)
})
end
return e
end
EndLevel._update = function(self)
local step = 2 * math.pi / 8
for e in all(self.candles) do
e.x = math.cos(e.i * step + tiny.t * 1.5)
e.y = math.sin(e.i * step + tiny.t * 1.5)
end
end
EndLevel._draw = function(self)
for e in all(self.candles) do
spr.sdraw(self.x + e.x * self.width, self.y + e.y * self.height, 72, 48, 9, 7)
end
end
local EndScreen = {}
EndScreen.create = function(data)
return new(EndScreen, data)
end
local PLATFORM_TYPE_STATIC = 0
local PLATFORM_TYPE_MOVING = 1
local PLATFORM_STATE_NORMAL = 0
local PLATFORM_STATE_PULVERIZING = 1
local PLATFORM_PULVERIZE_TIME = 0.2 * 4
local PLATFORM_VELOCITY = 2;
local decals_y = {48, 64, 80, 96, 112, 128, 128, 128}
local Platform = {
type = PLATFORM_TYPE_STATIC,
state = PLATFORM_STATE_NORMAL,
stateTime = 0,
velocity = nil,
position = nil
}
Platform.pulverize = function(self)
self.state = PLATFORM_STATE_PULVERIZING
self.stateTime = 0
self.velocity.x = 0
end
Platform.create = function(data)
local p = new(Platform, data)
p.velocity = vec2.create()
if (p.customFields["Type"] == "MOVING") then
p.type = PLATFORM_TYPE_MOVING
p.velocity.x = PLATFORM_VELOCITY
else
p.type = PLATFORM_TYPE_MOVING
end
p.decals_y = math.rnd(decals_y)
return p
end
Platform._update = function(self)
if self.type == PLATFORM_TYPE_MOVING then
self.x = self.x + self.velocity.x
if self.x < 0 then
self.velocity.x = self.velocity.x * -1
self.x = 0
end
if self.x > self.world_width - self.width then
self.velocity.x = self.velocity.x * -1
self.x = self.world_width - self.width
end
end
if self.state == PLATFORM_STATE_PULVERIZING and self.stateTime > PLATFORM_PULVERIZE_TIME then
-- to be deleted
return true
end
self.stateTime = self.stateTime + tiny.dt
return false
end
Platform._draw = function(self)
spr.sdraw(self.x, self.y - 16, 0, self.decals_y, self.width, 16)
if self.state == PLATFORM_STATE_PULVERIZING then
if (tiny.frame % 15) < 7 then
spr.sdraw(self.x, self.y, 0, 32, self.width, self.height)
else
spr.sdraw(self.x, self.y, 0, 16, self.width, self.height)
end
else
spr.sdraw(self.x, self.y, 0, 16, self.width, self.height)
end
end
local Particles = {
x = 0,
y = 0,
r = 4,
c = 6,
ttl = 0.6
}
local particles_conf = {{
accel = 0.05,
r = 2,
ttl = 0.7
}, {
accel = -0.05,
r = 2,
ttl = 0.7
}, {
accel = 0.25,
r = 4,
ttl = 0.5
}, {
accel = -0.25,
r = 4,
ttl = 0.5
}, {
accel = 0.2,
r = 3,
ttl = 0.6
}, {
accel = -0.2,
r = 3,
ttl = 0.6
}, {
accel = 0.15,
r = 3,
ttl = 0.7
}, {
accel = -0.15,
r = 3,
ttl = 0.7
}}
Particles.create = function(i, x, y)
local r = particles_conf[i].r
local xx = x + 8
local a = particles_conf[i].accel
local ttl = particles_conf[i].ttl
local p = new(Particles, {
x = xx,
y = y + 15,
r = r,
r_start = r,
ttl = ttl,
accel = a
})
return p
end
Particles.createRadial = function(x, y, angle)
local r = 4
local xx = x + math.cos(angle) * 8
local yy = y + math.sin(angle) * 8
local a = 1
local ttl = 0.2
local p = new(Particles, {
x = xx,
y = yy,
r = r,
r_start = r,
ttl = ttl,
radial = true,
accel_x = a * math.cos(angle),
accel_y = a * math.sin(angle),
c = 11
})
return p
end
Particles._update = function(self)
self.ttl = self.ttl - tiny.dt
if self.radial then
self.x = self.x + self.accel_x
self.y = self.y + self.accel_y
self.r = juice.powIn2(0, self.r, 0.98)
else
self.r = juice.powOut2(self.r_start, 0, 0.5 - self.ttl)
self.x = self.x + self.accel
end
return self.ttl < 0
end
Particles._draw = function(self)
shape.circlef(self.x, self.y, self.r, self.c)
end
local Coins = {
touched = false,
target_x = 0,
target_y = 0
}
Coins.create = function(data)
return new(Coins, data)
end
Coins._update = function(self)
if (self.touched) then
-- move coins in the uperleft
self.target_y = 8 + bob.y - bob.world_height * 0.5
self.x = juice.powIn2(self.target_x, self.x, 0.98)
if not self.fixed then
self.y = juice.powIn2(self.target_y, self.y, 0.98)
else
self.y = 8 + bob.y_camera
end
if (math.abs(self.target_y - self.y) < 2) then
self.fixed = true
end
end
end
Coins._draw = function(self)
function cycleValue(x)
local cycle_length = 8
local index = (x - 1) % cycle_length
if index < 4 then
return index
else
return cycle_length - index - 1
end
end
if self.touched then
spr.sdraw(self.x, self.y, 96 + 8, 0, 8, 8)
else
local offset = cycleValue(math.ceil(tiny.frame * 0.2))
spr.sdraw(self.x, self.y, 96 + offset * 8, 0, 8, 8)
end
end
local platforms = {}
local particles = {}
local coins = {}
local endLevel = nil
local endScreen = nil
local endTitle = nil
local endGameTransition = nil
function _init(w, h)
endGameTransition = nil
platforms = {}
coins = {}
gravity = vec2.create(0, 0.1)
for e in all(map.entities["Bob"]) do
bob = Bob.create(e)
bob.world_width = w
bob.world_height = h
end
for e in all(map.entities["Coins"]) do
table.insert(coins, Coins.create(e))
end
for e in all(map.entities["Platform"]) do
local p = Platform.create(e)
p.world_width = w
table.insert(platforms, p)
end
for e in all(map.entities["EndLevel"]) do
endLevel = EndLevel.create(e)
end
for e in all(map.entities["EndScreen"]) do
endScreen = EndScreen.create(e)
end
endTitle = new(EndTitle)
bob.y_camera = bob.y - h * 0.5
gfx.camera(0, bob.y_camera)
end
function overlaps(self, r)
return self.x < r.x + r.width and self.x + self.width > r.x and self.y < r.y + r.height and self.y + self.height >
r.y
end
function checkCollisions()
for c in all(coins) do
if not c.touched and overlaps(bob, c) then
bob.coins = bob.coins + 1
c.touched = true
c.target_x = 8 + bob.coins * 4
c.index = bob.coins
local step = math.pi * 2 / 8
for i = 1, 8 do
table.insert(particles, Particles.createRadial(c.x, c.y, i * step))
end
end
end
if overlaps(bob, endScreen) then
bob.velocity.y = BOB_JUMP_VELOCITY * 5
endTitle.ttl = -3
end
if bob.velocity.y < 0 then
return
end
for p in all(platforms) do
if p.y > bob.y then
if overlaps(p, bob) then
bob:hitPlatform()
for i = 1, #particles_conf do
table.insert(particles, Particles.create(i, bob.x, bob.y))
end
if math.rnd(100) > 50 then
p:pulverize()
end
end
end
end
end
function _update()
if ctrl.pressed(keys.space) then
_init(256, 256)
end
bob:_update()
for i, e in rpairs(platforms) do
if e:_update() then
table.remove(platforms, i)
end
end
for i, e in rpairs(particles) do
if e:_update() then
table.remove(particles, i)
end
end
for i, c in rpairs(coins) do
if (c:_update()) then
table.remove(coins, i)
end
end
table.sort(coins, function(a, b)
if a.index == nil and b.index == nil then
return false
elseif a.index == nil then
return false
elseif b.index == nil then
return true
else
return a.index < b.index
end
end)
if bob.state ~= BOB_STATE_HIT then
checkCollisions()
end
if bob.y > map.height() and not endGameTransition then
-- end game
endGameTransition = 1.5
elseif endGameTransition then
endGameTransition = endGameTransition - tiny.dt
if endGameTransition < 0 then
tiny.exit(0)
end
end
endLevel:_update()
endTitle:_update()
end
function _draw()
gfx.cls(8)
map.draw()
for e in all(platforms) do
e:_draw()
end
for e in all(particles) do
e:_draw()
end
for c in all(coins) do
c:_draw()
end
endLevel:_draw()
bob:_draw()
endTitle:_draw()
if endGameTransition then
shape.circlef(256 * 0.5, bob.y_camera + 384 * 0.5, juice.powIn2(0, 356, 1.5 - endGameTransition), 1)
end
end