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 --- src/main.cpp | 80 ++++++++++++++++++++++++++---------------------------------- 1 file changed, 35 insertions(+), 45 deletions(-) (limited to 'src/main.cpp') diff --git a/src/main.cpp b/src/main.cpp index eef9777..a9b5d3b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,20 +1,16 @@ -#include "Api.h" +#include "CApi.h" #include "Level.h" +#include "LuaApi.h" #include #include #include #include const char *DEFAULT_PROC = "include/defaults.lua"; -std::shared_ptr lvl; -struct LState { - lua_State *onkeypress; - lua_State *onupdate; -} typedef LState; +std::shared_ptr lvl; -bool call_onkeypress(lua_State *L, char pressedKey); -bool call_onupdate(lua_State *L); +Scene scene; int main(int argc, char **argv) { @@ -28,6 +24,7 @@ int main(int argc, char **argv) { std::filesystem::path luaFile{lvl_pfx + "/proc.lua"}; lvl = std::make_shared(); + scene = Scene::INTRO; lvl->loadLevelFromFile(mapFile.c_str()); @@ -47,20 +44,11 @@ int main(int argc, char **argv) { } // Initialize to default - LState l_state = {.onkeypress = L_default, .onupdate = L_default}; + LState *l_state = init_default(L_default); if (std::filesystem::exists(luaFile) && luaL_dofile(L_lvl, luaFile.c_str()) == LUA_OK) { - - // overwrite defaults - lua_getglobal(L_lvl, "onKeyPress"); - if (lua_isfunction(L_lvl, -1)) { - l_state.onkeypress = L_lvl; - } - lua_getglobal(L_lvl, "onUpdate"); - if (lua_isfunction(L_lvl, -1)) { - l_state.onupdate = L_lvl; - } + override_file_fns(L_lvl, l_state); } else if (std::filesystem::exists(luaFile)) { std::cout << "[C] No Good" << std::endl; luaL_error(L_lvl, "Error: %s\n", lua_tostring(L_lvl, -1)); @@ -72,12 +60,35 @@ int main(int argc, char **argv) { char in; do { - lvl->print(); - std::cin >> in; - if (!call_onkeypress(l_state.onkeypress, in)) { + if (scene == Scene::INTRO) { + if (!lua_onintro(l_state->onintro)) { + quit = true; + } + std::cin >> in; + if (!lua_onkeypress(l_state->onkeypress, in)) { + quit = true; + } + } else if (scene == Scene::LEVEL) { + lvl->print(); + + std::cin >> in; + if (!lua_onkeypress(l_state->onkeypress, in)) { + quit = true; + } + if (!lua_onupdate(l_state->onupdate)) { + quit = true; + } + } else if (scene == Scene::WIN) { + lvl->player.x = -1; // hide + lvl->player.y = -1; // hide + lvl->print(); + lua_onwin(l_state->onwin); quit = true; - } - if (!call_onupdate(l_state.onupdate)) { + } else if (scene == Scene::LOSS) { + lvl->player.x = -1; // hide + lvl->player.y = -1; // hide + lvl->print(); + lua_onloss(l_state->onloss); quit = true; } if (!quit && in == 'q') { @@ -89,24 +100,3 @@ int main(int argc, char **argv) { return EXIT_SUCCESS; } - -bool call_onkeypress(lua_State *L, char pressedKey) { - lua_getglobal(L, "onKeyPress"); - if (!lua_isfunction(L, -1)) { - std::cout << "[C] Error onKeyPress not function | not found" << std::endl; - return false; - } - lua_pushinteger(L, pressedKey); - lua_pcall(L, 1, 1, 0); - return true; -} - -bool call_onupdate(lua_State *L) { - lua_getglobal(L, "onUpdate"); - if (!lua_isfunction(L, -1)) { - std::cout << "[C] Error onUpdate not function | not found" << std::endl; - return false; - } - lua_pcall(L, 0, 1, 0); - return true; -} \ No newline at end of file -- cgit v1.2.3-54-g00ecf