From 4eeacc60cab3d6cb070bcd19a5259b7a95832a1d Mon Sep 17 00:00:00 2001 From: Andy Belle-Isle Date: Sat, 31 Aug 2019 00:40:21 -0400 Subject: 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 --- src/components/IdleFunc.hpp | 39 ------------------------------- src/components/Script.hpp | 56 +++++++++++++++++++++++++++++++++++++++++++++ src/engine.cpp | 11 +++++++++ src/script.cpp | 47 +++++++++++++++---------------------- 4 files changed, 85 insertions(+), 68 deletions(-) delete mode 100644 src/components/IdleFunc.hpp create mode 100644 src/components/Script.hpp (limited to 'src') diff --git a/src/components/IdleFunc.hpp b/src/components/IdleFunc.hpp deleted file mode 100644 index 29bcd7b..0000000 --- a/src/components/IdleFunc.hpp +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright (C) 2019 Belle-Isle, Andrew - * Author: Belle-Isle, Andrew - * - * 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 . - */ - -#ifndef IDLEFUNC_HPP_ -#define IDLEFUNC_HPP_ - -#include - -struct IdleFunc : Component, entityx::Component -{ - sol::function luafunc; - public: - IdleFunc() {} - - IdleFunc FromLua(sol::object ref) - { - if (ref.get_type() == sol::type::function) { - this->luafunc = ref; - } - return *this; - } -}; - -#endif//IDLEFUNC_HPP_ diff --git a/src/components/Script.hpp b/src/components/Script.hpp new file mode 100644 index 0000000..0e7c9db --- /dev/null +++ b/src/components/Script.hpp @@ -0,0 +1,56 @@ +/* + * Copyright (C) 2019 Belle-Isle, Andrew + * Author: Belle-Isle, Andrew + * + * 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 . + */ + +#ifndef SCRIPT_COMPONENT_HPP_ +#define SCRIPT_COMPONENT_HPP_ + +#include + +struct Scripted : Component, entityx::Component +{ + public: + sol::table caller; + Scripted() {} + Scripted(sol::table call): caller(call) {} + + + ~Scripted() + {} + + void cleanup() + { + 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//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(); @@ -44,8 +47,16 @@ void Engine::logicLoop(void) while (shouldRun()) { systems.update(dt); + + // All entities with an idle function should be run here + entities.each([](entityx::Entity, Scripted &f){ + f.exec(); + }); std::this_thread::sleep_for(100ms); } + + // Remove all Lua references from entities + entities.each([](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 -#include -#include -#include - void ScriptSystem::doFile(void) { + auto result = lua.script_file("Scripts/init.lua"); + if (!result.valid()) { std::cout << "Lua error: " << std::endl; } - - manager->each( - [&](entityx::Entity, Position& p) - {std::cout << p.x << "," << p.y << std::endl;}); - - manager->each( - [&](entityx::Entity, Name& n) - {std::cout << n.name << std::endl;}); - - manager->each( - [&](entityx::Entity, Render& r) - {std::cout << r.texture << ": " << r.visible << std::endl;}); } /******************** * SCRIPT PARSING * ********************/ -// TODO here +#include +#include +#include +#include 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().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().FromLua(tab["Position"])).get(); } - if (tab["init"] != nullptr) { - toRet["init"] = tab["init"]; - } - if (tab["Name"] != nullptr) { - toRet["Name"] = + (*toRet)["Name"] = e.assign(Name().FromLua(tab["Name"])).get(); } if (tab["Render"] != nullptr) { - toRet["Render"] = + (*toRet)["Render"] = e.assign(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; } -- cgit v1.2.3