From 87a53f2a09a204ca9bcdde9c73db414c79075326 Mon Sep 17 00:00:00 2001 From: Steph Enders Date: Fri, 17 Jun 2022 11:19:51 -0400 Subject: Add scene controls and win/loss scenarios Setup ability to check collisions and transition game scene --- include/algs.lua | 2 +- include/constants.lua | 5 +++ include/defaults.lua | 97 ++++++++++++++++++++++++++++++++++++++++++--------- 3 files changed, 87 insertions(+), 17 deletions(-) (limited to 'include') 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, +} -- cgit v1.2.3-54-g00ecf