-
Notifications
You must be signed in to change notification settings - Fork 0
/
aseplatform.lua
369 lines (324 loc) · 9.11 KB
/
aseplatform.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
--[[
Aseprite playable platformer mockup. Just for FUN!
MIT License
DarkDes, 2023
]]--
-- Search in the top level of layers in the current sprite
local function find_layer(name)
for i,layer in ipairs(app.sprite.layers) do
if layer.name == name then
return layer
end
end
return nil
end
-- Don't mess up the original sprite we run from, add a cloned sprite!
app.sprite = Sprite(app.sprite)
-- Canvas & Viewport
VIEW_W = 192
VIEW_H = 144
imageBuffer = Image(VIEW_W, VIEW_H)
-- Level
local TMC = find_layer("TMCollision")
-- Interface
local INT = find_layer("Interface")
local interface = {}
interface.cels = {}
interface.cels_positions = {}
interface.score_point = Point(0,0)
interface.health_point = Point(0,0)
interface.health_layer = nil
-- Player
local P = find_layer("Player")
local player = {}
player.cels = {}
player.health = 4
player.score = 0
player.do_not_hurt_me = false
player.finished = false
player.input_block = false
-- Input move
player.side = 1
player.move_x = 0
player.move_y = 0
-- Velocity
player.vel_y = 0
player.on_ground = 0
player.position = Point(0,0)
player.bounds = nil
-- Camera
camera_x = 0
camera_y = 0
-- Player cels
if P.isGroup then
for j,layer in ipairs(P.layers) do
for i,cel in ipairs(layer.cels) do
if player.bounds == nil then
player.bounds = Rectangle(cel.bounds)
end
table.insert(player.cels, cel)
player.bounds = player.bounds:union(cel.bounds)
end
end
elseif P.isImage then
for i,cel in ipairs(P.cels) do
if player.bounds == nil then
player.bounds = Rectangle(cel.bounds)
end
table.insert(player.cels, cel)
player.bounds = player.bounds:union(cel.bounds)
end
end
-- Scanning Layers:
-- Interface
if INT.isGroup then
for j,layer in ipairs(INT.layers) do
if layer.isGroup then
for j,layer2 in ipairs(layer.layers) do
for i,cel in ipairs(layer2.cels) do
table.insert(interface.cels, cel)
table.insert(interface.cels_positions, Point(cel.position))
end
end
else
for i,cel in ipairs(layer.cels) do
table.insert(interface.cels, cel)
table.insert(interface.cels_positions, Point(cel.position))
end
end
if layer.name == "ScorePoint" then
interface.score_point = layer.cels[1].position
end
if layer.name == "Health" then
interface.health_layer = layer
player.health = #layer.layers
end
end
end
-- FINISH
local finish_layers = find_layer("Finish")
local FINISH_cels = {}
for j,layer in ipairs(finish_layers.layers) do
for i,cel in ipairs(layer.cels) do
table.insert(FINISH_cels, cel)
end
end
-- Dangers
local LGD = find_layer("Dangers") -- Layer group of layers and cals
local LGD_cels = {}
if LGD.isGroup then
for j,layer in ipairs(LGD.layers) do
for i,cel in ipairs(layer.cels) do
table.insert(LGD_cels, cel)
end
end
end
-- Bonuses
local LGB = find_layer("Bonuses") -- Layer Group of layers and cels
local LGB_cels = {}
if LGB.isGroup then
for j,layer in ipairs(LGB.layers) do
for i,cel in ipairs(layer.cels) do
table.insert(LGB_cels, cel)
end
end
end
-- Functions for canvas:
function dcanv_onkeyup(ev)
if ev.code == "ArrowLeft" or ev.code == "ArrowRight" then
player.move_x = 0
end
if ev.code == "ArrowUp" or ev.code == "ArrowDown" then
player.move_y = 0
end
end
function dcanv_onkeydown(ev)
if ev.code == "ArrowLeft" then
player.move_x = -1
elseif ev.code == "ArrowRight" then
player.move_x = 1
end
-- Jumping
if ev.code == "ArrowUp" then
player.move_y = -1
if player.on_ground and player.input_block == false then
player.vel_y = -10
player.on_ground = false
end
elseif ev.code == "ArrowDown" then
player.move_y = 1
end
if player.move_x ~= 0 or player.move_y ~= 0 then
ev:stopPropagation()
end
end
function dcanv_onpaint(ev)
local gc = ev.context
imageBuffer:drawSprite( app.sprite, app.frame, Point(-camera_x, -camera_y) )
gc:drawImage( imageBuffer, 0, 0 )
gc.color = Color{r=255,g=0,b=0}
if player.health <= 0 then
local text_size = gc:measureText("FINISHED!")
gc:fillText( "GAME OVER!", (VIEW_W - text_size.w)/2, (VIEW_H - text_size.h)/2 )
end
gc.color = Color{r=200,g=200,b=100}
if player.finished then
local text_size = gc:measureText("FINISHED!")
gc:fillText( "FINISHED!", (VIEW_W - text_size.w)/2, (VIEW_H - text_size.h)/2 )
end
-- Score
gc.color = Color{r=200,g=200,b=100}
gc:fillText( player.score, interface.score_point.x, interface.score_point.y )
end
-- Dialog & Canvas
local D = Dialog{ title = "Game", onclose = function() async_rendering_timer:stop() app.command.PlayAnimation() end }
D:canvas{
id = "canvas",
width = VIEW_W,
height = VIEW_H,
hexpand = true,
vexpand = true,
onkeyup = dcanv_onkeyup,
onkeydown = dcanv_onkeydown,
onpaint = dcanv_onpaint
}
-- Timer for disabling invincibility
hurt_timer = Timer{ interval = 1.0, ontick = function()
player.do_not_hurt_me = false
hurt_timer:stop()
end }
-- Main "Game" Loop
async_rendering_timer =
Timer{
interval = 1.0/30.0, -- 30 fps
ontick = function()
-- as one event in history (per frame, ew)
app.transaction(
function()
-- Gravitation
if player.on_ground == false then
if player.vel_y < 2 then
player.vel_y = player.vel_y + 1
end
else
player.vel_y = 0
end
local p_vel_x = player.move_x
local p_vel_y = player.vel_y
local collided
-- Player state (if dead, then no input)
if player.input_block then
p_vel_x = 0
end
-- Collisions: use read pixels and check alpha.
-- If any point on the line have has an non-transparent pixel, true is returned.
-- Collision Left and Right
function collision_line_vertical(px, py, ph)
local color = Color()
for i=py, ph+py, 1 do
color = Color( TMC.cels[1].image:getPixel( px, i ) )
if color.alpha > 128 then
return true
end
end
return false
end
-- Collision Top and Bottom
function collision_line_horisontal(px, py, pw)
local color = Color()
for i=px, pw+px, 1 do
color = Color( TMC.cels[1].image:getPixel( i, py ) )
if color.alpha > 128 then
return true
end
end
return false
end
-- Horizontal collisions
collided = collision_line_vertical(player.position.x + p_vel_x, player.position.y, player.bounds.height-2)
if collided then p_vel_x = 0 end
collided = collision_line_vertical(player.position.x + player.bounds.width + p_vel_x, player.position.y, player.bounds.height-2)
if collided then p_vel_x = 0 end
-- Vertical collisions
collided = collision_line_horisontal( player.position.x, player.position.y + p_vel_y, player.bounds.width )
if collided then p_vel_y = 0 end
collided = collision_line_horisontal( player.position.x+2, player.position.y + p_vel_y + player.bounds.height, player.bounds.width-4 )
if collided then
p_vel_y = 0
player.on_ground = true
else
player.on_ground = false
if p_vel_y < 1 then
p_vel_y = p_vel_y + 1
end
end
-- Move player
for i,cel in ipairs(player.cels) do
cel.position = Point(cel.position.x + p_vel_x, cel.position.y + p_vel_y)
end
player.position = player.cels[1].position
-- end move
local _bounds = Rectangle( player.position.x, player.position.y, player.bounds.width, player.bounds.height)
-- I don't know why or how it works with tilemap,
-- so just use the hazards\dangers and bonuses as separate layers of the target with appropriate boundaries.
-- Bonus Collision
for i,cel in ipairs(LGB_cels) do
if cel.layer.isVisible == true and _bounds:intersects(cel.bounds) then
-- Pick up!
cel.layer.isVisible = false
player.score = player.score + 100
end
end
-- Danger Collision
for i,cel in ipairs(LGD_cels) do
if player.do_not_hurt_me == false and _bounds:intersects(cel.bounds) then
-- HURT
if player.health > 0 then
player.health = player.health - 1
player.do_not_hurt_me = true
hurt_timer:start()
-- Game Over
if player.health <= 0 then
player.input_block = true
end
-- Change health bar indicator count
local hp = interface.health_layer
for h=1, #hp.layers do
if player.health < h then
hp.layers[h].isVisible = false
else
hp.layers[h].isVisible = true
end
end
end
end
end
-- Finish Collision
for i,cel in ipairs(FINISH_cels) do
if _bounds:intersects(cel.bounds) then
-- Finish!
cel.layer.isVisible = false -- Hide "finish object"
player.finished = true
player.input_block = true
end
end
-- Camera follow
camera_x = player.position.x - VIEW_W/2 + player.bounds.width/2
camera_y = player.position.y - VIEW_H/2 + player.bounds.height/2
-- Clamp camera position to sprite(location) borders
if camera_x < 0 then camera_x = 0 elseif camera_x > app.sprite.width-VIEW_W then camera_x = app.sprite.width - VIEW_W end
if camera_y < 0 then camera_y = 0 elseif camera_y > app.sprite.height-VIEW_H then camera_y = app.sprite.height - VIEW_H end
-- Update position of Interface
for i,cel in ipairs(interface.cels) do
cel.position = Point(interface.cels_positions[i].x + camera_x, interface.cels_positions[i].y + camera_y)
end
-- Update screen
D:repaint()
end) -- history
end
}
-- Starting up!
async_rendering_timer:start()
D:show{ wait=false }
app.command.PlayAnimation()
-- End of file