-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.lua
339 lines (291 loc) · 14.4 KB
/
main.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
local TILE_SIZE = 24
local SLOW_TIME_SEC = 0.125
local screen_width = 600
local screen_height = 400
local window_flags = {resizable = true}
love.window.setMode(screen_width, screen_height, window_flags)
local player = {
width = 23, height = 48,
pos = {x = 0, y = 0},
prev_pos = {x = 0, y = 50},
vel = {x = 0, y = 0},
accel = {x = 0, y = 0},
grounded = false,
}
-- 0: empty
-- 1: generic tile
-- 3: spike
-- 8: finish
-- 9: player
local block_table = {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}
local input_buffer
local next_input_index
local player_img
local tile_img, forward_img, jump_img, goal_img
local slow_time = 0
local my_input_column = 0
local win
function reset()
win = false
input_buffer = find_buffer_tiles()
next_input_index = 1
-- set player position
for row_index, row in ipairs(block_table) do
for column_index in ipairs(row) do
if get_block(column_index, row_index) == 9 then
player.pos.x, player.pos.y = index_to_pos(column_index, row_index)
player.pos.x = player.pos.x + player.width/2
player.prev_pos.x = player.pos.x
player.prev_pos.y = player.pos.y
end
end
end
player.grounded = false
player.vel.x = 0
player.vel.y = 0
end
function love.load()
love.graphics.setBackgroundColor(0.75, 0.75, 0.87)
player_img = love.graphics.newImage("player.png")
tile_img = love.graphics.newImage("tile.png")
forward_img = love.graphics.newImage("forward.png")
jump_img = love.graphics.newImage("jump.png")
goal_img = love.graphics.newImage("goal.png")
spike_img = love.graphics.newImage("spike.png")
reset()
end
function absolute(a)
if a < 0 then
a = a * -1
end
return a
end
-- returned buffer is indexed by column
function find_buffer_tiles()
local buffer_tiles = {}
local found_tile = false
for column_index=1,#block_table[1] do
for row_index=#block_table, 1, -1 do
if get_block(column_index, row_index) == 1 then
found_tile = true
else
if found_tile then
table.insert(buffer_tiles, {column_index = column_index, row_index = row_index, input=nil})
found_tile = false
break
end
end
end
end
return buffer_tiles
end
function love.resize(width, height)
screen_width = width
screen_height = height
end
function get_block(column_index, row_index)
local row = block_table[row_index]
if not row then
return 0
end
local val = row[column_index]
if not val then
return 0
end
return val
end
function pos_to_index(x, y)
local column_index = math.floor(x/TILE_SIZE) + 1
local row_index = math.floor(y/TILE_SIZE) + 1
return column_index, row_index
end
function index_to_pos(column_index, row_index)
return (column_index-1) * TILE_SIZE, row_index * TILE_SIZE
end
function love.draw()
love.graphics.setColor(1, 1, 1, 1)
love.graphics.translate(-player.pos.x + screen_width/2, -player.pos.y + screen_height/2)
love.graphics.setBlendMode("alpha")
love.graphics.draw(player_img, player.pos.x - player.width/2, player.pos.y - player.height)
for row_index,row in ipairs(block_table) do
for column_index, tile_val in pairs(row) do
if tile_val == 1 then
love.graphics.draw(tile_img, (column_index -1) * TILE_SIZE, (row_index -1) * TILE_SIZE)
elseif tile_val == 8 and not win then
love.graphics.draw(goal_img, (column_index -1) * TILE_SIZE, (row_index -1) * TILE_SIZE)
elseif tile_val == 3 then
love.graphics.draw(spike_img, (column_index -1) * TILE_SIZE, (row_index -1) * TILE_SIZE)
end
end
end
-- love.graphics.setColor(1, 0, 0, 1)
-- local column_index, row_index = pos_to_index(player.pos.x, player.pos.y - player.height/2)
-- for row_offset = -1, 1 do
-- for column_offset = -1, 1 do
-- local blockx, blocky = index_to_pos(column_index + column_offset, row_index + row_offset)
--love.graphics.polygon('line', blockx, blocky, blockx + TILE_SIZE, blocky, blockx +TILE_SIZE, blocky - TILE_SIZE, blockx, blocky - TILE_SIZE)
-- end
-- end
love.graphics.setColor(1, 1, 0, 1)
for _, input in ipairs(input_buffer) do
if input.input ~= nil then
local blockx, blocky = index_to_pos(input.column_index, input.row_index)
if input.input == 'move' then
love.graphics.draw(forward_img, blockx, blocky)
elseif input.input == 'jump' then
love.graphics.draw(jump_img, blockx, blocky)
end
end
end
love.graphics.origin()
love.graphics.setColor(0.1, 0.2, 0.1, 1)
if win then
love.graphics.print("Win, nice!", screen_width/2, 0, 0, 1.5, 1.5)
end
love.graphics.print("right: forward\nup: jump\nr: reset", 10, screen_height-50, 0, 1, 1)
love.graphics.setColor(1, 1, 1, 1)
end
function player_block_overlap(column_index, row_index)
local blockx, blocky = index_to_pos(column_index, row_index)
if player.pos.y > blocky - TILE_SIZE and player.pos.y - player.height < blocky then
if player.pos.x + player.width/2 > blockx and player.pos.x - player.width/2 < blockx + TILE_SIZE then
return true
end
end
return false
end
function get_block_neighbours(column_index, row_index)
top = get_block(column_index, row_index - 1)
bottom = get_block(column_index, row_index + 1)
left = get_block(column_index - 1, row_index)
right = get_block(column_index + 1, row_index)
return top, bottom, left, right
end
function love.update(dt)
if win == true then
return
end
if slow_time > 0 then
slow_time = slow_time - dt
return
end
if slow_time < 0 then
slow_time = 0
end
-- compute player speed and check for jump inputs
local player_column_index, player_row_index = pos_to_index(player.pos.x - player.width/2, player.pos.y - player.height/2)
local last_input
local input
local input_count = 0
repeat
input = input_buffer[next_input_index - 1 - input_count]
last_input = input
if not input or not input.input or player_column_index >= input.column_index then
break
end
input_count = input_count + 1
until false
if player.grounded and last_input and last_input.input == 'jump' then
local last_input_x, last_input_y = index_to_pos(last_input.column_index, last_input.row_index)
if last_input.column_index == player_column_index and absolute(last_input_y - player.pos.y) < 5 then
player.vel.y = -200
end
end
player.prev_pos.x = player.pos.x
player.prev_pos.y = player.pos.y
player.accel.y = 240
player.vel.x = input_count * 30
player.vel.y = player.vel.y + player.accel.y * dt
player.pos.x = player.pos.x + player.vel.x * dt
player.pos.y = player.pos.y + player.vel.y * dt
if player.pos.y > (#block_table + 5) * TILE_SIZE then
reset()
end
-- handle static block collisions
player.grounded = false
local player_column_index, player_row_index = pos_to_index(player.pos.x, player.pos.y - player.height/2)
for row_offset = -1, 1 do
for column_offset = -1, 1 do
local block_column_index = player_column_index + column_offset
local block_row_index = player_row_index + row_offset
local block_type = get_block(block_column_index, block_row_index)
if block_type ~= 0 then
if player_block_overlap(block_column_index, block_row_index) then
if block_type == 8 then
win = true
return
elseif block_type == 3 then
reset()
return
end
local top, bottom, left, right = get_block_neighbours(block_column_index, block_row_index)
local blockx, blocky = index_to_pos(block_column_index, block_row_index)
if player.vel.y > 0 and top ~= 1 then
if player.prev_pos.y <= blocky - TILE_SIZE then -- if was above previously
player.vel.y = 0
player.pos.y = blocky - TILE_SIZE
player.grounded = true
end
elseif player.vel.y < 0 and bottom ~= 1 then
if player.prev_pos.y - player.height >= blocky then
player.vel.y = 0
player.pos.y = blocky + player.height
end
end
if player.vel.x > 0 and left ~= 1 then
if player.prev_pos.x + player.width/2 <= blockx then
player.pos.x = blockx - player.width/2
player.vel.x = 0
end
elseif player.vel.x < 0 and right ~= 1 then
if player.prev_pos.x - player.width/2 >= blockx + TILE_SIZE then
player.pos.x = blockx + TILE_SIZE + player.width/2
player.vel.x = 0
end
end
end
end
end
end
end
function love.keypressed(key)
local input_type = nil
--if key == 'a' or key == 'left' then
-- input_type = 'undo'
if key == 'd' or key == 'right' then
input_type = 'move'
elseif key == 'w' or key == 'up' then
input_type = 'jump'
elseif key == 'r' then
reset()
return
else
return
end
local player_index, _ = pos_to_index(player.pos.x, player.pos.y - player.height/2)
local next_input
repeat
next_input = input_buffer[next_input_index]
if not next_input then
return
end
next_input_index = next_input_index + 1
until next_input.column_index > player_index
next_input.input = input_type
slow_time = SLOW_TIME_SEC
end