summaryrefslogtreecommitdiff
path: root/src/main.cpp
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 /src/main.cpp
parent49bb775d4cf9935b0f0d54daa358423ac786c9be (diff)
Add scene controls and win/loss scenarios
Setup ability to check collisions and transition game scene
Diffstat (limited to 'src/main.cpp')
-rw-r--r--src/main.cpp80
1 files changed, 35 insertions, 45 deletions
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 <SFML/Graphics.hpp>
#include <filesystem>
#include <iostream>
#include <lua.hpp>
const char *DEFAULT_PROC = "include/defaults.lua";
-std::shared_ptr<Level> lvl;
-struct LState {
- lua_State *onkeypress;
- lua_State *onupdate;
-} typedef LState;
+std::shared_ptr<Level> 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<Level>();
+ 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