From 8e101f8bfd995321ac08a16fea9a171b549a0ae4 Mon Sep 17 00:00:00 2001 From: Steph Enders Date: Sat, 17 Dec 2022 11:04:20 -0500 Subject: Support doors with keys Add initial support for doors and keys via pre-defined mappings: k || d ------ 1 -> a 2 -> b 3 -> c 4 -> d Any key can open any door of its mapping, but is spent once used. May require additional testing --- dnglib/defaults.lua | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) (limited to 'dnglib/defaults.lua') diff --git a/dnglib/defaults.lua b/dnglib/defaults.lua index f33e603..f2251a2 100644 --- a/dnglib/defaults.lua +++ b/dnglib/defaults.lua @@ -60,6 +60,12 @@ keys = { --- setup random --math.randomseed(os.time()) +-- Checks if x,y equals for both objects +local function is_collision(a, b) + return a.x == b.x and a.y == b.y +end + + ---@param pressedKey number function onKeyPress(pressedKey) scene = c_get_scene() @@ -102,35 +108,52 @@ function onUpdate(dt) treasures = c_get_treasures() assert(type(treasures) == "table", "treasures is not a table") + door_keys = c_get_keys() + assert(type(door_keys) == "table", "keys is not a table") + + doors = c_get_doors() + assert(type(doors) == "table", "doors is not a table") + map = c_get_map(); assert(type(map) == "table", "map is not a table") for i, v in ipairs(enemies) do local next; if diff_time >= MOV_TIME then - next = algs.pathfind(v, player, enemies, treasures, map) + next = algs.pathfind(v, player, enemies, treasures, door_keys, doors, map) else next = { dx = 0, dy = 0 } end new_pos = c_move_enemy(v.id, next.dx, next.dy) assert(type(new_pos) == "table", "new_pos is not a table") - if new_pos.x == player.x and new_pos.y == player.y then + if is_collision(new_pos, player) then c_trigger_loss() end enemies[i] = new_pos -- update new position for pathfinding end - treasures = c_get_treasures() - assert(type(treasures) == "table", "treasures is not a table") for _, t in ipairs(treasures) do - if t.x == player.x and t.y == player.y then + if is_collision(t, player) then c_score_treasure(t.id) if #treasures == 1 then c_trigger_win() end end end + + for _, k in ipairs(door_keys) do + if is_collision(k, player) then + c_take_key(k.id) + end + end + + for _, d in ipairs(doors) do + if is_collision(d, player) then + c_open_door(d.id) + end + end + if diff_time > MOV_TIME then diff_time = 0 end -- cgit v1.2.3-54-g00ecf