diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/components/Component.hpp | 4 | ||||
-rw-r--r-- | src/components/Position.hpp | 14 | ||||
-rw-r--r-- | src/main.cpp | 84 | ||||
-rw-r--r-- | src/script.hpp | 93 | ||||
-rw-r--r-- | src/script/script.hpp | 70 |
5 files changed, 50 insertions, 215 deletions
diff --git a/src/components/Component.hpp b/src/components/Component.hpp index 84e860c..9c6b79d 100644 --- a/src/components/Component.hpp +++ b/src/components/Component.hpp @@ -19,7 +19,7 @@ #ifndef COMPONENT_HPP_ #define COMPONENT_HPP_ -#include "LuaBridge/LuaBridge.h" +#include "sol/sol.hpp" template <typename T> class Component @@ -27,7 +27,7 @@ class Component public: Component(){}; - virtual T FromLua(luabridge::LuaRef) = 0; + virtual T FromLua(sol::object) = 0; }; #endif//COMPONENT_HPP_ diff --git a/src/components/Position.hpp b/src/components/Position.hpp index aaa99f9..856b3d7 100644 --- a/src/components/Position.hpp +++ b/src/components/Position.hpp @@ -29,17 +29,17 @@ struct Position : Component<Position>, entityx::Component<Position> Position(float _x, float _y): x(_x), y(_y) {} Position(void){x = y = 0.0;} - Position FromLua(luabridge::LuaRef ref) + Position FromLua(sol::object ref) { - if (ref.isTable()){ - if (!ref["x"].isNil()) - this->x = ref["x"]; - if (!ref["y"].isNil()) - this->y = ref["y"]; + if (ref.get_type() == sol::type::table){ + sol::table tab = ref; + if (tab["x"] != nullptr) + this->x = tab["x"]; + if (tab["y"] != nullptr) + this->y = tab["y"]; } else { throw std::string("Position table not formatted properly"); } - return *this; } }; diff --git a/src/main.cpp b/src/main.cpp index 386aafc..3dc7bed 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -20,7 +20,6 @@ #include <lua.hpp> #include <entityx/entityx.h> -#include <LuaBridge/LuaBridge.h> #include "engine.hpp" @@ -50,86 +49,3 @@ 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 index e9373c0..f6111d0 100644 --- a/src/script.hpp +++ b/src/script.hpp @@ -21,22 +21,18 @@ #include <entityx/entityx.h> #include <lua.hpp> -#include <LuaBridge/LuaBridge.h> -#include <script/script.hpp> +#include <sol/sol.hpp> /**************** * COMPONENTS * ****************/ #include <components/Position.hpp> -namespace lb = luabridge; - struct EntitySpawnEvent { - EntitySpawnEvent (lb::LuaRef ref) - : ref(ref), ret(nullptr){} + EntitySpawnEvent (sol::object ref) + : ref(ref) {} - lb::LuaRef ref; - lb::LuaRef ret; + sol::object ref; }; /** @@ -51,32 +47,27 @@ class ScriptSystem : public entityx::System<ScriptSystem> * 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; + sol::state lua; + public: ScriptSystem(void) - : state(luaL_newstate(), lua_close), - manager(events){} + : manager(events){} ~ScriptSystem(void) { } - lua_State* getState() - { - return state.get(); - } - int init(void) { - luaL_openlibs(state.get()); + lua.open_libraries(sol::lib::base); scriptExport(); - //doFile(); + doFile(); - Script::CreateNewState(); + //Script::CreateNewState(); return 0; } @@ -100,8 +91,9 @@ class ScriptSystem : public entityx::System<ScriptSystem> void doFile(void) { - if (luaL_dofile(state.get(), "Scripts/init.lua")) { - std::cout << "Lua error: " << lua_tostring(state.get(), -1) << std::endl; + auto result = lua.script_file("Scripts/init.lua"); + if (!result.valid()) { + std::cout << "Lua error: " << std::endl; } manager.each<Position>( @@ -109,51 +101,48 @@ class ScriptSystem : public entityx::System<ScriptSystem> {std::cout << p.x << "," << p.y << std::endl;}); } - lb::LuaRef spawn(lb::LuaRef ref) + sol::table spawn([[maybe_unused]]sol::object param) { - lb::LuaRef entity(state.get()); - entity = lb::newTable(state.get()); + sol::table toRet = lua.create_table_with(); + + //lb::LuaRef entity(state.get()); + //entity = lb::newTable(state.get()); - if (ref.isTable()) { + if (param.get_type() == sol::type::table) { + sol::table tab = param; 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; - } + if (tab["Position"] != nullptr) { + toRet["Position"] = + e.assign<Position>(Position().FromLua(tab["Position"])).get(); } + + if (tab["init"] != nullptr) { + toRet["init"] = tab["init"]; + } + } else { std::cerr << "Parameter to spawn() must be a table!" << std::endl; } - return entity; + //return entity; + return toRet; } 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(); + + std::function<sol::table(sol::table)> func = + [this](sol::table t){ return spawn(t);}; + + 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("spawn", func); } void receive (const EntitySpawnEvent &toSpawn) diff --git a/src/script/script.hpp b/src/script/script.hpp deleted file mode 100644 index 340fd6f..0000000 --- a/src/script/script.hpp +++ /dev/null @@ -1,70 +0,0 @@ -/* - * 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_ |