From cb49e2b9334d0d14d294d43c23c45620a18fd167 Mon Sep 17 00:00:00 2001 From: Nick Stokoe Date: Sat, 2 Jul 2022 22:59:14 +0100 Subject: [PATCH] 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. --- arena.lua | 2 ++ hopper.lua | 2 ++ main.lua | 38 ++++++++++++++++++++++++-------------- 3 files changed, 28 insertions(+), 14 deletions(-) diff --git a/arena.lua b/arena.lua index be8f4d1..6d4bc3d 100644 --- a/arena.lua +++ b/arena.lua @@ -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, diff --git a/hopper.lua b/hopper.lua index 60b47d6..a94b23d 100644 --- a/hopper.lua +++ b/hopper.lua @@ -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 diff --git a/main.lua b/main.lua index 68c1ca8..480234f 100644 --- a/main.lua +++ b/main.lua @@ -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