main.lua, etc. - use a sorted list of mobs

The mobs are characters to animate.  They're sorted by screen depth,
so that nearer ones render over farther ones.
This commit is contained in:
Nick Stokoe
2022-07-02 22:59:14 +01:00
parent 1cfc037283
commit cb49e2b933
3 changed files with 28 additions and 14 deletions

View File

@@ -52,6 +52,8 @@ return function(size, discs, topCol, rightCol, leftCol)
end end
return { return {
getDepth = function() return -1; end,
getWidth = function() getWidth = function()
return size*width*2 return size*width*2
end, end,

View File

@@ -162,6 +162,8 @@ return function(arena, draw, update)
local se, sw = 0, 0 local se, sw = 0, 0
local jumpPath = newJumpPath(arena.getColWidth(), arena.getRowHeight()) local jumpPath = newJumpPath(arena.getColWidth(), arena.getRowHeight())
return { return {
getDepth = function() return sw+se; end,
jump = function(dir) jump = function(dir)
if jumpTime ~= nil then if jumpTime ~= nil then
return -- already jumping return -- already jumping

View File

@@ -11,27 +11,22 @@ local time = 0
local qbert = mkQbert( local qbert = mkQbert(
arena arena
) )
local mobs = { qbert, arena }
function love.load() function love.load()
end end
function love.draw() function love.draw()
arena.draw() for ix, mob in ipairs(mobs) do
qbert.draw() mob.draw()
end
end end
local dirs = {"se", "sw", "ne", "nw"}
local ix = 1
function love.update(dt) function love.update(dt)
--[[ time = time + dt
if (time > 2) then
time = 0
local dir = dirs[ix]
ix = (ix+1)%4 + 1
qbert.jump(dir)
end
]]--
if love.keyboard.isDown('i') then if love.keyboard.isDown('i') then
qbert.jump('ne') qbert.jump('ne')
end end
@@ -44,7 +39,22 @@ function love.update(dt)
if love.keyboard.isDown('j') then if love.keyboard.isDown('j') then
qbert.jump('sw') qbert.jump('sw')
end end
if love.keyboard.isDown('escape') then
love.event.quit(0)
end
arena.update(dt);
qbert.update(dt); for ix, mob in ipairs(mobs) do
mob.update(dt)
end
local mobs2 = {}
for ix, mob in ipairs(mobs) do
if not mob.dead then
mobs2[#mobs2+1] = mob
end
end
table.sort(mobs2, function(a, b) return a.getDepth() < b.getDepth(); end)
mobs = mobs2
end end