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/defaults.lua | 97 +++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 81 insertions(+), 16 deletions(-) (limited to 'include/defaults.lua') 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