y = 3.4
},
Name = "bord",
- init = function(self)
+ Init = function(self)
print(self.Position.x .. "," .. self.Position.y)
end
}
texture = "assets/tex.png",
visible = true
},
- init = function(self)
+ Init = function(self)
print(self.Position.x .. "," .. self.Position.y)
+ end,
+ Idle = function(self)
+ self.Position.x = self.Position.x + 0.01;
end
}
}
birdSpawn = game.spawn(bird);
-birdSpawn:init()
-print(birdSpawn.Name.value)
+birdSpawn:Init()
dogSpawn = game.spawn(dog);
-dogSpawn:init()
+dogSpawn:Init()
dogSpawn.Position.x = 37.5
animalSpawn = game.spawn(animal);
-animalSpawn.Render.texture = "assets/newText.png"
+++ /dev/null
-/*
- * 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 IDLEFUNC_HPP_
-#define IDLEFUNC_HPP_
-
-#include <components/Component.hpp>
-
-struct IdleFunc : Component<IdleFunc>, entityx::Component<IdleFunc>
-{
- 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_
--- /dev/null
+/*
+ * 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_COMPONENT_HPP_
+#define SCRIPT_COMPONENT_HPP_
+
+#include <components/Component.hpp>
+
+struct Scripted : Component<Scripted>, entityx::Component<Scripted>
+{
+ 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_
#include "window.hpp"
#include "script.hpp"
+#include "components/Script.hpp"
+#include "components/Position.hpp"
+
int Engine::init(void)
{
systems.add<GameRunSystem>();
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)
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()
{
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();
}
std::cerr << "Parameter to spawn() must be a table!" << std::endl;
}
- return toRet;
+ return *toRet;
}