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
return {
getDepth = function() return -1; end,
getWidth = function()
return size*width*2
end,

View File

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

View File

@@ -11,27 +11,22 @@ local time = 0
local qbert = mkQbert(
arena
)
local mobs = { qbert, arena }
function love.load()
end
function love.draw()
arena.draw()
qbert.draw()
for ix, mob in ipairs(mobs) do
mob.draw()
end
end
local dirs = {"se", "sw", "ne", "nw"}
local ix = 1
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
qbert.jump('ne')
end
@@ -44,7 +39,22 @@ function love.update(dt)
if love.keyboard.isDown('j') then
qbert.jump('sw')
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