summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorSteph Enders <smenders@gmail.com>2022-06-17 11:19:51 -0400
committerSteph Enders <smenders@gmail.com>2022-06-17 11:19:51 -0400
commit87a53f2a09a204ca9bcdde9c73db414c79075326 (patch)
tree5dbb78e554d01a8f9dd1e3cdf11d4077f0d15676 /include
parent49bb775d4cf9935b0f0d54daa358423ac786c9be (diff)
Add scene controls and win/loss scenarios
Setup ability to check collisions and transition game scene
Diffstat (limited to 'include')
-rw-r--r--include/algs.lua2
-rw-r--r--include/constants.lua5
-rw-r--r--include/defaults.lua97
3 files changed, 87 insertions, 17 deletions
diff --git a/include/algs.lua b/include/algs.lua
index 2541102..c9e6438 100644
--- a/include/algs.lua
+++ b/include/algs.lua
@@ -104,7 +104,7 @@ local function pathfind(start_pos, target_pos, map)
end
if (push_moves(start_pos.x, start_pos.y, nil, visit_map, queue, target_pos)) then
- return { dx = start_pos.x - target_pos.x, dy = start_pos.y - target_pos.y }
+ return { dx = target_pos.x - start_pos.x, dy = target_pos.y - start_pos.y }
end
while queue:empty() ~= true do
diff --git a/include/constants.lua b/include/constants.lua
index 922a35b..ab5fcf1 100644
--- a/include/constants.lua
+++ b/include/constants.lua
@@ -8,3 +8,8 @@ KEY_SPACE = ' '
MAP_WALL = 1
MAP_SPACE = 0
MAP_VISITED = -1
+
+SCENE_INTRO = 0
+SCENE_LEVEL = 1
+SCENE_WIN = 2
+SCENE_LOSS = 3 \ No newline at end of file
diff --git a/include/defaults.lua b/include/defaults.lua
index a6cefe5..3651060 100644
--- a/include/defaults.lua
+++ b/include/defaults.lua
@@ -16,33 +16,44 @@ c_fatal(msg)
--]]
require "include.constants";
local algs = require "include.algs";
+local hasLost = false;
+local hasWon = false;
+local hasIntro = false;
--- setup random
--math.randomseed(os.time())
---@param pressedKey number
function onKeyPress(pressedKey)
+ scene = c_get_scene()
+ assert(type(scene) == "number", "scene is not a number")
+ if scene == SCENE_INTRO then
+ if pressedKey == KEY_S then
+ print("Lua start");
+ c_trigger_level_start();
+ end
+ elseif scene == SCENE_LEVEL then
+ dx = 0
+ dy = 0
+ if (pressedKey == KEY_W) then
+ dy = -1
+ elseif pressedKey == KEY_A then
+ dx = -1
+ elseif pressedKey == KEY_S then
+ dy = 1
+ elseif pressedKey == KEY_D then
+ dx = 1
+ end
- dx = 0
- dy = 0
- if (pressedKey == KEY_W) then
- dy = -1
- elseif pressedKey == KEY_A then
- dx = -1
- elseif pressedKey == KEY_S then
- dy = 1
- elseif pressedKey == KEY_D then
- dx = 1
+ c_move_player(dx, dy)
end
-
- c_move_player(dx, dy)
end
function onUpdate()
- enemies = c_get_enemies() -- external
+ enemies = c_get_enemies()
assert(type(enemies) == "table", "Enemies not a table")
- player = c_get_player_position() -- external
+ player = c_get_player_position()
assert(type(player) == "table", "Player is not a table")
map = c_get_map();
@@ -50,7 +61,58 @@ function onUpdate()
for _, v in ipairs(enemies) do
local next = algs.pathfind(v, player, map)
- c_move_enemy(v.id, next.dx, next.dy)
+ 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
+ c_trigger_loss()
+ end
+ 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
+ c_score_treasure(t.id)
+ if #treasures == 1 then
+ c_trigger_win()
+ end
+ end
+ end
+end
+
+function onWin()
+ if hasWon == false then
+ hasWon = true
+ print("You WIN!!!!!!!!!")
+ end
+end
+
+function onLoss()
+ if hasLost == false then
+ hasLost = true
+ print("Oh no! You lost!")
+ end
+end
+
+function onIntro()
+ if hasIntro == false then
+ hasIntro = true
+ print([[
+...................
+.. dng ..
+...................
+.. controls ..
+...................
+.. start -- s ..
+.. move -- wasd ..
+.. quit -- q ..
+...................
+.. goal ..
+...................
+.. Find treasure ..
+.. Avoid enemies ..
+...................
+ ]])
end
end
@@ -58,4 +120,7 @@ end
return {
onKeyPress = onKeyPress,
onUpdate = onUpdate,
-} \ No newline at end of file
+ onWin = onWin,
+ onLoss = onLoss,
+ onIntro = onIntro,
+}