-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.lua
More file actions
451 lines (392 loc) · 11.1 KB
/
main.lua
File metadata and controls
451 lines (392 loc) · 11.1 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
-- In Real Life version: use scrabble tiles, flipped over
-- use "O" tiles for static blocks
-- use Carcassonne meeples for players and black ones for Beasts
-- Start with coop lives -- 3 x # players
-- Level | # Beasts
-- 1 | 1
-- 2 | 1
-- 3 | 1* (Super)
-- 4 | 2
-- 5 | 2
-- 6 | 1*
-- 7 | 3
-- 8 | 3
-- 9 | 1*
local bump = require "lib/bump"
local class = require "lib/middleclass"
local vector = require "lib/vector"
local baton = require "lib/baton"
if os.getenv("LOCAL_LUA_DEBUGGER_VSCODE") == "1" then
require("lldebugger").start()
end
-- game variables
local player_speed = 300-- 400
local beast_speed = 100 -- 150
local grid_size = 32
local players = {}
local blocks = {}
local enemies = {}
local num_lives
local level = 1
local player_colors = {
{140/255, 60/255, 140/255},
{62/255, 98/255, 187/255},
{62/255, 187/255, 66/255},
{187/255, 166/255, 62/255},
{62/255, 187/255, 183/255}
}
-- generic drawable sprite (for prototyping, just a filled rect)
local Sprite = class('Sprite')
function Sprite:draw()
love.graphics.setColor(self.color)
love.graphics.rectangle("fill", self.x, self.y, self.width, self.height)
end
function Sprite:push(other, x, y)
local actualX, actualY, cols, len = world:check(self, x, y, function(item, other)
return "cross"
end)
local success = true
for i=1, len do
local col = cols[i]
if col.other ~= other then
local other_x = col.other.x
local other_y = col.other.y
if col.normal.x == -1 then
other_x = other_x + grid_size -- self.x + self.width
elseif col.normal.x == 1 then
other_x = other_x - grid_size -- self.x - col.other.width
end
if col.normal.y == -1 then
other_y = other_y + grid_size -- self.y + self.height
elseif col.normal.y == 1 then
other_y = other_y - grid_size -- self.y - col.other.height
end
if not col.other:push(self, other_x, other_y) then
success = false
end
end
end
if success then
actualX, actualY = world:move(self, x, y, function(item, other)
return "cross"
end)
self.x = actualX
self.y = actualY
end
return success
end
-- Playable Player
local Player = class('Player', Sprite)
function Player:initialize(x, y, input, color)
self.input = input
self.color = color
self.width = grid_size
self.height = grid_size
self.x = x
self.y = y
self.pulled_block = nil
Sprite.initialize(self)
end
function Player:update(dt)
self.input:update(dt)
local x = self.x
local y = self.y
local dx, dy = self.input:get('move')
-- user is moving
if dx ~= 0 or dy ~= 0 then
x = x + (dx * player_speed * dt)
y = y + (dy * player_speed * dt)
end
-- user is moving slower in this dimension
-- so snap to the grid
if math.abs(dx) < 0.5 then
x = snap(x)
end
if math.abs(dy) < 0.5 then
y = snap(y)
end
-- pull blocks if the pull button is pressed
local pulled_block, pull_dest_x, pull_dest_y
if self.input:down('pull') and (x ~= self.x or y ~= self.y) then
local is_horiz = math.abs(dx) > math.abs(dy)
local behind_x
local behind_y
if is_horiz then
if dx > 0 then
behind_x = self.x - 1
else
behind_x = self.x + grid_size + 1
end
behind_y = self.y + grid_size / 2
else
if dy > 0 then
behind_y = self.y - 1
else
behind_y = self.y + grid_size + 1
end
behind_x = self.x + grid_size / 2
end
local items, items_len = world:queryPoint(behind_x, behind_y, function(item)
return item.class.name == 'Block' -- reference to the class Block doesn't match!
end)
pulled_block = items[1]
if pulled_block then
if is_horiz then
pull_dest_x = pulled_block.x + (x - self.x) -- recalc dx due to snapping above
pull_dest_y = pulled_block.y
else
pull_dest_y = pulled_block.y + (y - self.y) -- recalc dy due to snapping above
pull_dest_x = pulled_block.x
end
end
end
self:push(nil, x, y)
-- have to pull block *after* doing the move/push, so there's room for it
if pulled_block then
local actualX, actualY, cols, len = world:move(pulled_block, pull_dest_x, pull_dest_y, function(item, other)
return "slide"
end)
pulled_block.x = actualX
pulled_block.y = actualY
self.pulled_block = pulled_block
end
-- we just *stopped* pulling a block
if self.pulled_block and not pulled_block then
local block = self.pulled_block
--local actualX, actualY, cols, len =
world:update(block, snap(block.x), snap(block.y))--, function(item, other)
-- return "slide"
-- end)
print(block.y, snap(block.y), actualY)
block.x = snap(block.x)--actualX
block.y = snap(block.y)-- actualY
self.pulled_block = nil
end
end
function Player:dieAndRespawn()
num_lives = num_lives - 1
if num_lives == 0 then
love.event.quit()
end
local x, y = findEmptyGridSquare()
world:update(self, x, y)
self.x = x
self.y = y
end
-- enemy
local Enemy = class('Enemy', Sprite)
function Enemy:initialize(x, y)
self.color = {140/255, 60/255, 60/255}
self.width = grid_size
self.height = grid_size
self.x = x
self.y = y
Sprite.initialize(self)
end
function Enemy:update(dt)
local nearest_player = nil
local nearest_dist = 200 * grid_size
for i = 1, #players do
local dx = math.abs(players[i].x - self.x)
local dy = math.abs(players[i].y - self.y)
-- simple "nearest" formulation (not accurate, I know)
local dist = dx + dy
if dist < nearest_dist then
nearest_player = players[i]
nearest_dist = dist
end
end
local dir = vector(nearest_player.x - self.x, nearest_player.y - self.y)
dir:normalizeInplace()
dir = dir * beast_speed * dt
local x = self.x + dir.x
local y = self.y + dir.y
-- often the beasts get *super* close to going through a hole, but not quite
-- we can fix this by a slight adjustment: if either dimension is super-close
-- to being on a grid line, adjust it so it is on the grid line
local x_offset = x % grid_size
if math.abs(x_offset) < 2 then
if x_offset > 0 and dir.x < 0 then
x = x - x_offset
else
x = x + x_offset
end
end
local y_offset = y % grid_size
if math.abs(y_offset) < 2 then
if y_offset > 0 and dir.y < 0 then
y = y - y_offset
else
y = y + y_offset
end
end
local actualX, actualY, cols, len = world:move(self, x, y, function(item, other)
return "slide"
end)
for i=1, len do
local col = cols[i]
if col.other:isInstanceOf(Player) then
col.other:dieAndRespawn()
end
end
self.x = actualX
self.y = actualY
end
function Enemy:push(other, x, y)
-- *check* if there would be a collision
local actualX, actualY, cols, len = world:check(self, x, y, function(item, other)
return "cross"
end)
-- we get squished if there would've been a collision
-- TODO: we should probably check that it's not a player on the other side...
if #cols ~= 0 then
local ix
for i=1, #enemies do
if enemies[i] == self then
ix = i
end
end
table.remove(enemies, ix)
world:remove(self)
if #enemies == 0 then
level = level + 1
spawnEnemies()
end
end
return false
end
-- movable block
local Block = class('Block', Sprite)
function Block:initialize(x, y)
self.color = {200/255, 200/255, 200/255}
self.width = grid_size
self.height = grid_size
self.x = x
self.y = y
Sprite.initialize(self)
end
-- helper functions
function addSprite(spr)
world:add(spr, spr.x, spr.y, spr.width, spr.height)
end
function generateBlocks()
local win_width = love.graphics.getWidth()
local win_height = love.graphics.getHeight()
local num_blocks_x = win_width / grid_size
local num_blocks_y = win_height / grid_size
local block
local block_ix = 1
for y = 1, num_blocks_y do
for x = 1, num_blocks_x do
-- avoid creating blocks at 1,1 or 1,2, so they don't overlap w/ the players
if x > 1 or y > 2 then
if math.random(6) == 1 then
block = Block:new((x - 1) * grid_size, (y - 1) * grid_size)
table.insert(blocks, block)
addSprite(block)
end
end
end
end
end
function spawnEnemies()
local num_enemies = level * 2 + 2
for i = 1, num_enemies do
local x, y = findEmptyGridSquare()
enemy = Enemy:new(x, y)
table.insert(enemies, enemy)
addSprite(enemy)
end
end
function findEmptyGridSquare()
-- TODO: store the *initial* num_blocks x/y and use that always
local win_width = love.graphics.getWidth()
local win_height = love.graphics.getHeight()
local num_blocks_x = win_width / grid_size
local num_blocks_y = win_height / grid_size
local x, y, items, len_items
x = math.random(num_blocks_x) * 32
y = math.random(num_blocks_y) * 32
items, len_items = world:queryRect(x, y, grid_size, grid_size)
-- TODO: fix this DRY; I tried to do a do-while, but it failed
while (len_items > 0)
do
x = math.random(num_blocks_x) * 32
y = math.random(num_blocks_y) * 32
items, len_items = world:queryRect(x, y, grid_size, grid_size)
end
return x, y
end
function snap(val)
local offset = val % grid_size
if offset < (grid_size / 2) then
val = val - offset
else
val = val + (grid_size - offset)
end
return val
end
-- game callbacks
function love.load()
local joysticks = love.joystick.getJoysticks()
local num_players = #joysticks
if num_players == 0 then
num_players = 1 -- if there are no joysticks, fallback to one keyboard/mouse player
end
num_lives = num_players * 2
-- prepare simple AABB collision world w/ cell size
-- nearest neightbor & full-screen
love.graphics.setDefaultFilter( 'nearest', 'nearest' )
love.window.setFullscreen(true)
love.mouse.setVisible(false)
love.graphics.setBackgroundColor({150/255, 150/255, 150/255})
world = bump.newWorld(64)
generateBlocks()
for i=1, num_players do
local x, y = findEmptyGridSquare()
table.insert(players, Player:new(x, y, baton.new({
controls = {
move_left = {'key:a', 'axis:leftx-', 'button:dpleft'},
move_right = {'key:d', 'axis:leftx+', 'button:dpright'},
move_up = {'key:w', 'axis:lefty-', 'button:dpup'},
move_down = {'key:s', 'axis:lefty+', 'button:dpdown'},
pull = {'key:space', 'button:a'},
},
pairs = {
move = {'move_left', 'move_right', 'move_up', 'move_down'},
},
deadzone = 0.2,
squareDeadzone = true, -- helps w/ player grid snapping when transitioning from diagonal to vert/horiz movement
joystick = joysticks[i]
}), player_colors[i]))
end
for i = 1, #players do
addSprite(players[i])
end
spawnEnemies()
end
function love.update(dt)
for i = 1, #players do
players[i]:update(dt)
end
for i = 1, #enemies do
enemies[i]:update(dt)
end
end
function love.draw()
for i = 1, #players do
players[i]:draw()
end
for i = 1, #blocks do
blocks[i]:draw()
end
for i = 1, #enemies do
local enemy = enemies[i]
enemy:draw()
end
end
function love.keypressed(k)
if k == 'escape' then
love.event.quit()
end
end