diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/components/Component.hpp | 33 | ||||
-rw-r--r-- | src/components/Position.hpp | 47 | ||||
-rw-r--r-- | src/engine.cpp | 2 | ||||
-rw-r--r-- | src/main.cpp | 88 | ||||
-rw-r--r-- | src/script.hpp | 166 | ||||
-rw-r--r-- | src/script/script.hpp | 70 |
6 files changed, 406 insertions, 0 deletions
diff --git a/src/components/Component.hpp b/src/components/Component.hpp new file mode 100644 index 0000000..84e860c --- /dev/null +++ b/src/components/Component.hpp @@ -0,0 +1,33 @@ +/* + * Copyright (C) 2019 Belle-Isle, Andrew <drumsetmonkey@gmail.com> + * Author: Belle-Isle, Andrew <drumsetmonkey@gmail.com> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef COMPONENT_HPP_ +#define COMPONENT_HPP_ + +#include "LuaBridge/LuaBridge.h" + +template <typename T> +class Component +{ + public: + Component(){}; + + virtual T FromLua(luabridge::LuaRef) = 0; +}; + +#endif//COMPONENT_HPP_ diff --git a/src/components/Position.hpp b/src/components/Position.hpp new file mode 100644 index 0000000..aaa99f9 --- /dev/null +++ b/src/components/Position.hpp @@ -0,0 +1,47 @@ +/* + * Copyright (C) 2019 Belle-Isle, Andrew <drumsetmonkey@gmail.com> + * Author: Belle-Isle, Andrew <drumsetmonkey@gmail.com> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef POSITION_HPP_ +#define POSITION_HPP_ + +#include "components/Component.hpp" + +struct Position : Component<Position>, entityx::Component<Position> +{ + + public: + float x,y; + Position(float _x, float _y): x(_x), y(_y) {} + Position(void){x = y = 0.0;} + + Position FromLua(luabridge::LuaRef ref) + { + if (ref.isTable()){ + if (!ref["x"].isNil()) + this->x = ref["x"]; + if (!ref["y"].isNil()) + this->y = ref["y"]; + } else { + throw std::string("Position table not formatted properly"); + } + + return *this; + } +}; + +#endif//POSITION_HPP_ diff --git a/src/engine.cpp b/src/engine.cpp index 0bc647b..0a3b810 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -22,12 +22,14 @@ #include "gamerun.hpp" #include "input.hpp" #include "window.hpp" +#include "script.hpp" int Engine::init(void) { systems.add<GameRunSystem>(); systems.add<InputSystem>(); systems.add<WindowSystem>(); + systems.add<ScriptSystem>(); systems.configure(); return 0; diff --git a/src/main.cpp b/src/main.cpp index a1c605e..386aafc 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -18,6 +18,10 @@ * along with this program. If not, see <https://www.gnu.org/licenses/>. */ +#include <lua.hpp> +#include <entityx/entityx.h> +#include <LuaBridge/LuaBridge.h> + #include "engine.hpp" #include <SDL2/SDL.h> @@ -35,6 +39,8 @@ int main([[maybe_unused]] int argc, [[maybe_unused]] char *argv[]) atexit(SDL_Quit); } + //LuaTest(); + // Create the engine Engine engine; engine.init(); @@ -45,3 +51,85 @@ int main([[maybe_unused]] int argc, [[maybe_unused]] char *argv[]) return 0; } +/* +using namespace entityx; +namespace lb = luabridge; + +EventManager events; +EntityManager entities(events); + +lua_State* L; + +//lb::LuaRef spawn(lb::LuaRef ref) +//{ +// lb::LuaRef entity(L); +// entity = lb::newTable(L); +// +// if (ref.isTable()) { +// +// Entity e = entities.create(); +// +// for (auto &&comp : lb::pairs(ref)) { +// if (comp.first.cast<std::string>() == "Position") { +// entity["Position"] = +// e.assign<Position>(Position().FromLua(comp.second)).get(); +// } else if (comp.first.cast<std::string>() == "init") { +// entity["init"] = comp.second; +// } +// } +// } else { +// std::cerr << "Parameter to spawn() must be a table!" << std::endl; +// } +// +// return entity; +//} +// + +ScriptSystem sc; + +lb::LuaRef spawn(lb::LuaRef ref) +{ + return sc.spawn(ref); +} + + +void LuaTest(void) +{ + + sc.configure(entities, events); + + // Functions export + lb::getGlobalNamespace(sc.getState()) + .beginNamespace("game") + .addFunction("spawn", spawn) + .endNamespace(); + + sc.doFile(); + + + //L = luaL_newstate(); + //luaL_openlibs(L); + + //lb::getGlobalNamespace(L). + // beginNamespace("comp") + // .beginClass<Position>("Position") + // .addConstructor<void(*)(float, float)>() + // .addProperty("x", &Position::x) + // .addProperty("y", &Position::y) + // .endClass() + // .endNamespace(); + + //lb::getGlobalNamespace(L) + // .beginNamespace("game") + // .addFunction("spawn", spawn) + // .endNamespace(); + + //if (luaL_dofile(L, "Scripts/init.lua")) { + // std::cout << "Lua error: " << lua_tostring(L, -1) << std::endl; + //} + + //entities.each<Position>([&](Entity, Position& p){std::cout << p.x << "," << p.y << std::endl;}); + + //lua_close(L); +} +*/ diff --git a/src/script.hpp b/src/script.hpp new file mode 100644 index 0000000..e9373c0 --- /dev/null +++ b/src/script.hpp @@ -0,0 +1,166 @@ +/* + * Copyright (C) 2019 Belle-Isle, Andrew <drumsetmonkey@gmail.com> + * Author: Belle-Isle, Andrew <drumsetmonkey@gmail.com> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef SCRIPT_HPP_ +#define SCRIPT_HPP_ + +#include <entityx/entityx.h> +#include <lua.hpp> +#include <LuaBridge/LuaBridge.h> +#include <script/script.hpp> + +/**************** +* COMPONENTS * +****************/ +#include <components/Position.hpp> + +namespace lb = luabridge; + +struct EntitySpawnEvent { + EntitySpawnEvent (lb::LuaRef ref) + : ref(ref), ret(nullptr){} + + lb::LuaRef ref; + lb::LuaRef ret; +}; + +/** + * @class ScriptSystem + * Handles all the game's scripting requests + */ +class ScriptSystem : public entityx::System<ScriptSystem> + , public entityx::Receiver<ScriptSystem> +{ + private: + /** + * The script systems internal lua state that handles all + * interactions between C and Lua + */ + std::unique_ptr<lua_State, void(*)(lua_State *)> state; + entityx::EventManager events; + entityx::EntityManager manager; + + public: + ScriptSystem(void) + : state(luaL_newstate(), lua_close), + manager(events){} + + ~ScriptSystem(void) + { + + } + + lua_State* getState() + { + return state.get(); + } + + int init(void) + { + luaL_openlibs(state.get()); + scriptExport(); + //doFile(); + + Script::CreateNewState(); + + return 0; + } + + void configure([[maybe_unused]]entityx::EntityManager& entities, + [[maybe_unused]]entityx::EventManager& events) final + { + //manager = std::make_shared<entityx::EntityManager>(std::move(entities)); + + events.subscribe<EntitySpawnEvent>(*this); + + init(); + } + + void update([[maybe_unused]] entityx::EntityManager& entites, + [[maybe_unused]] entityx::EventManager& events, + [[maybe_unused]] entityx::TimeDelta dt) final + { + + } + + void doFile(void) + { + if (luaL_dofile(state.get(), "Scripts/init.lua")) { + std::cout << "Lua error: " << lua_tostring(state.get(), -1) << std::endl; + } + + manager.each<Position>( + [&](entityx::Entity, Position& p) + {std::cout << p.x << "," << p.y << std::endl;}); + } + + lb::LuaRef spawn(lb::LuaRef ref) + { + lb::LuaRef entity(state.get()); + entity = lb::newTable(state.get()); + + if (ref.isTable()) { + + entityx::Entity e = manager.create(); + + for (auto &&comp : lb::pairs(ref)) { + if (comp.first.cast<std::string>() == "Position") { + entity["Position"] = + e.assign<Position>(Position().FromLua(comp.second)).get(); + } else if (comp.first.cast<std::string>() == "init") { + entity["init"] = comp.second; + } + } + } else { + std::cerr << "Parameter to spawn() must be a table!" << std::endl; + } + + return entity; + } + + void scriptExport() + { + // Components export + lb::getGlobalNamespace(state.get()). + beginNamespace("comp") + .beginClass<Position>("Position") + .addConstructor<void(*)(float, float)>() + .addProperty("x", &Position::x) + .addProperty("y", &Position::y) + .endClass() + .endNamespace(); + + lb::getGlobalNamespace(state.get()) + .beginNamespace("game") + .endNamespace(); + + // Functions export + //lb::getGlobalNamespace(state.get()) + // .beginNamespace("game") + // .addFunction("spawn", &ScriptSystem::spawn) + // .endNamespace(); + } + + void receive (const EntitySpawnEvent &toSpawn) + { + //toSpawn.ret = spawn(toSpawn.ref); + (void)toSpawn; + } +}; + +#endif//SCRIPT_HPP_ diff --git a/src/script/script.hpp b/src/script/script.hpp new file mode 100644 index 0000000..340fd6f --- /dev/null +++ b/src/script/script.hpp @@ -0,0 +1,70 @@ +/* + * Copyright (C) 2019 Belle-Isle, Andrew <drumsetmonkey@gmail.com> + * Author: Belle-Isle, Andrew <drumsetmonkey@gmail.com> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef SCRIPT_NAMESPACE_HPP_ +#define SCRIPT_NAMESPACE_HPP_ + +#include <sol/sol.hpp> +#include <entityx/entityx.h> + +#include <components/Position.hpp> + +#include <iostream> + +entityx::EventManager events; +entityx::EntityManager manager(events); +entityx::Entity e; + +namespace Script +{ + sol::state lua; + + sol::table newPosition([[maybe_unused]]sol::table tab) + { + sol::table toRet = lua.create_table_with(); + + e.assign<Position>(4.5, 2.3).get(); + + toRet["Position"] = e.component<Position>().get(); + + return toRet; + } + + void CreateNewState() + { + e = manager.create(); + + lua.open_libraries(sol::lib::base); + + //lua["q"] = Position(4.5, 3.4); + + lua.new_usertype<Position>("Position", + sol::constructors<Position(float x, float y), Position()>(), + "x", &Position::x, + "y", &Position::y); + + auto gamespace = lua["game"].get_or_create<sol::table>(); + gamespace.set_function("testfunc", newPosition); + + lua.script_file("Scripts/init.lua"); + auto p = e.component<Position>().get(); + std::cout << p->x << "," << p->y << std::endl; + } +} + +#endif//SCRIPT_NAMESPACE_HPP_ |