summaryrefslogtreecommitdiff
path: root/dnglib
diff options
context:
space:
mode:
Diffstat (limited to 'dnglib')
-rw-r--r--dnglib/algs.lua12
-rw-r--r--dnglib/constants.lua2
-rw-r--r--dnglib/defaults.lua33
3 files changed, 39 insertions, 8 deletions
diff --git a/dnglib/algs.lua b/dnglib/algs.lua
index d2e6252..b5f331f 100644
--- a/dnglib/algs.lua
+++ b/dnglib/algs.lua
@@ -150,10 +150,12 @@ end
---@param target_pos table [x, y]
-- @param enemies list of enemy positions (cannot pass thru enemy)
-- @param treasures list of treasure positions (cannot pass thru treasure)
+-- @param door_keys list of key positions (cannot pass thru treasure)
+-- @param doors list of door positions (cannot pass thru treasure)
---@param map table 2D map array
---@return table best move to target [x, y]
---
-local function pathfind(start_pos, target_pos, enemies, treasures, map)
+local function pathfind(start_pos, target_pos, enemies, treasures, door_keys, doors, map)
local queue = Queue:new()
local visit_map = {}
@@ -173,7 +175,13 @@ local function pathfind(start_pos, target_pos, enemies, treasures, map)
for _, t in ipairs(treasures) do
visit_map[t.y][t.x] = MAP_WALL -- use wall value for impass
end
-
+ for _, k in ipairs(door_keys) do
+ visit_map[k.y][k.x] = MAP_WALL -- use wall value for impass
+ end
+ for _, d in ipairs(doors) do
+ visit_map[d.y][d.x] = MAP_WALL -- use wall value for impass
+ end
+
-- since we mutate the visit_map let's calc this early if need be
local best_effort = best_effort_move(start_pos, target_pos, visit_map)
diff --git a/dnglib/constants.lua b/dnglib/constants.lua
index 098f308..2e1fa52 100644
--- a/dnglib/constants.lua
+++ b/dnglib/constants.lua
@@ -77,7 +77,7 @@ KEY_UP = 73
KEY_DOWN = 74
-- map values
-MAP_WALL = 1
+MAP_WALL = 9
MAP_SPACE = 0
MAP_VISITED = -1
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