diff options
author | Andy Belle-Isle <drumsetmonkey@gmail.com> | 2019-08-31 00:40:21 -0400 |
---|---|---|
committer | Andy Belle-Isle <drumsetmonkey@gmail.com> | 2019-08-31 00:40:21 -0400 |
commit | 4eeacc60cab3d6cb070bcd19a5259b7a95832a1d (patch) | |
tree | 7e26aea1b95dec5bb5bfaf4749cba0b71dd06cf9 /src | |
parent | 3a5718d4ab0d9f726686c601579a4c586a65e269 (diff) |
Lua spawned entities have "Idle" function
Every entity spawned from Lua is given the "Scripted" component,
this component holds onto the Entities master table and it's idle
function
Diffstat (limited to 'src')
-rw-r--r-- | src/components/Script.hpp (renamed from src/components/IdleFunc.hpp) | 37 | ||||
-rw-r--r-- | src/engine.cpp | 11 | ||||
-rw-r--r-- | src/script.cpp | 47 |
3 files changed, 56 insertions, 39 deletions
diff --git a/src/components/IdleFunc.hpp b/src/components/Script.hpp index 29bcd7b..0e7c9db 100644 --- a/src/components/IdleFunc.hpp +++ b/src/components/Script.hpp @@ -16,24 +16,41 @@ * along with this program. If not, see <http://www.gnu.org/licenses/>. */ -#ifndef IDLEFUNC_HPP_ -#define IDLEFUNC_HPP_ +#ifndef SCRIPT_COMPONENT_HPP_ +#define SCRIPT_COMPONENT_HPP_ #include <components/Component.hpp> -struct IdleFunc : Component<IdleFunc>, entityx::Component<IdleFunc> +struct Scripted : Component<Scripted>, entityx::Component<Scripted> { - sol::function luafunc; public: - IdleFunc() {} + sol::table caller; + Scripted() {} + Scripted(sol::table call): caller(call) {} - IdleFunc FromLua(sol::object ref) + + ~Scripted() + {} + + void cleanup() { - if (ref.get_type() == sol::type::function) { - this->luafunc = ref; - } + caller = sol::nil; + } + + Scripted FromLua(sol::object) + { + //if (ref.get_type() == sol::type::function) { + // this->luafunc = ref; + //} + //init = true; return *this; } + + void exec() { + if (caller["Idle"] == sol::type::function) + caller["Idle"](caller); // Call idle function and pass itself + // in or to fulfill the 'self' param + } }; -#endif//IDLEFUNC_HPP_ +#endif//SCRIPT_COMPONENT_HPP_ diff --git a/src/engine.cpp b/src/engine.cpp index 4803c64..317e116 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -25,6 +25,9 @@ #include "window.hpp" #include "script.hpp" +#include "components/Script.hpp" +#include "components/Position.hpp" + int Engine::init(void) { systems.add<GameRunSystem>(); @@ -44,8 +47,16 @@ void Engine::logicLoop(void) while (shouldRun()) { systems.update<InputSystem>(dt); + + // All entities with an idle function should be run here + entities.each<Scripted>([](entityx::Entity, Scripted &f){ + f.exec(); + }); std::this_thread::sleep_for(100ms); } + + // Remove all Lua references from entities + entities.each<Scripted>([](entityx::Entity, Scripted &f){ f.cleanup(); }); } void Engine::renderLoop(void) diff --git a/src/script.cpp b/src/script.cpp index bb413fe..4dc3a2a 100644 --- a/src/script.cpp +++ b/src/script.cpp @@ -62,36 +62,23 @@ int ScriptSystem::init(void) return 0; } -// TODO move all of these below once the test printouts are gone -#include <components/Position.hpp> -#include <components/Name.hpp> -#include <components/Render.hpp> -#include <components/IdleFunc.hpp> - void ScriptSystem::doFile(void) { + auto result = lua.script_file("Scripts/init.lua"); + if (!result.valid()) { std::cout << "Lua error: " << std::endl; } - - manager->each<Position>( - [&](entityx::Entity, Position& p) - {std::cout << p.x << "," << p.y << std::endl;}); - - manager->each<Name>( - [&](entityx::Entity, Name& n) - {std::cout << n.name << std::endl;}); - - manager->each<Render>( - [&](entityx::Entity, Render& r) - {std::cout << r.texture << ": " << r.visible << std::endl;}); } /******************** * SCRIPT PARSING * ********************/ -// TODO here +#include <components/Position.hpp> +#include <components/Name.hpp> +#include <components/Render.hpp> +#include <components/Script.hpp> void ScriptSystem::scriptExport() { @@ -119,29 +106,31 @@ void ScriptSystem::scriptExport() sol::table ScriptSystem::spawn(sol::object param) { - sol::table toRet = lua.create_table_with(); - + sol::table* toRet; if (param.get_type() == sol::type::table) { sol::table tab = param; entityx::Entity e = manager->create(); + auto d = e.assign<Scripted>().get(); + d->caller = lua.create_table(); + toRet = &(d->caller); + + *toRet = tab; // Copy table to our new entity to preserve functions + // this reduces the amount of work done in this function + // as well if (tab["Position"] != nullptr) { - toRet["Position"] = + (*toRet)["Position"] = e.assign<Position>(Position().FromLua(tab["Position"])).get(); } - if (tab["init"] != nullptr) { - toRet["init"] = tab["init"]; - } - if (tab["Name"] != nullptr) { - toRet["Name"] = + (*toRet)["Name"] = e.assign<Name>(Name().FromLua(tab["Name"])).get(); } if (tab["Render"] != nullptr) { - toRet["Render"] = + (*toRet)["Render"] = e.assign<Render>(Render().FromLua(tab["Render"])).get(); } @@ -149,5 +138,5 @@ sol::table ScriptSystem::spawn(sol::object param) std::cerr << "Parameter to spawn() must be a table!" << std::endl; } - return toRet; + return *toRet; } |