diff options
-rw-r--r-- | Scripts/init.lua | 4 | ||||
-rw-r--r-- | src/components/Position.hpp | 2 | ||||
-rw-r--r-- | src/components/Script.hpp | 8 | ||||
-rw-r--r-- | src/components/Velocity.hpp | 47 | ||||
-rw-r--r-- | src/script.cpp | 10 |
5 files changed, 64 insertions, 7 deletions
diff --git a/Scripts/init.lua b/Scripts/init.lua index 807ef9a..f92e366 100644 --- a/Scripts/init.lua +++ b/Scripts/init.lua @@ -10,6 +10,10 @@ bird = { } dog = { + Velocity = { + x = 0.01, + y = 10.5 + }, Position = { x = 6.5, y = 1.3 diff --git a/src/components/Position.hpp b/src/components/Position.hpp index 0d69393..0462d1a 100644 --- a/src/components/Position.hpp +++ b/src/components/Position.hpp @@ -24,7 +24,7 @@ struct Position : Component<Position>, entityx::Component<Position> { public: - float x,y; + float x, y; Position(float _x, float _y): x(_x), y(_y) {} Position(void): x(0), y(0) {} diff --git a/src/components/Script.hpp b/src/components/Script.hpp index 0e7c9db..7570802 100644 --- a/src/components/Script.hpp +++ b/src/components/Script.hpp @@ -1,6 +1,6 @@ -/* +/** + * @file Script.hpp * 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 @@ -39,10 +39,6 @@ struct Scripted : Component<Scripted>, entityx::Component<Scripted> Scripted FromLua(sol::object) { - //if (ref.get_type() == sol::type::function) { - // this->luafunc = ref; - //} - //init = true; return *this; } diff --git a/src/components/Velocity.hpp b/src/components/Velocity.hpp new file mode 100644 index 0000000..eacadf2 --- /dev/null +++ b/src/components/Velocity.hpp @@ -0,0 +1,47 @@ +/** + * @file Velocity.hpp + * + * Copyright (C) 2019 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 VELOCITY_HPP_ +#define VELOCITY_HPP_ + +#include <components/Component.hpp> + +struct Velocity : Component<Velocity>, entityx::Component<Velocity> +{ + public: + float x, y; + Velocity(): x(0), y(0) {} + Velocity(float _x, float _y): x(_x), y(_y) {} + + Velocity FromLua(sol::object ref) + { + 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("Velocity table not formatted properly"); + } + return *this; + } +}; + +#endif//VELOCITY_HPP_ diff --git a/src/script.cpp b/src/script.cpp index 4dc3a2a..1a6bda8 100644 --- a/src/script.cpp +++ b/src/script.cpp @@ -79,6 +79,7 @@ void ScriptSystem::doFile(void) #include <components/Name.hpp> #include <components/Render.hpp> #include <components/Script.hpp> +#include <components/Velocity.hpp> void ScriptSystem::scriptExport() { @@ -130,10 +131,19 @@ sol::table ScriptSystem::spawn(sol::object param) } if (tab["Render"] != nullptr) { + if (!e.has_component<Position>()) + e.assign<Position>(); (*toRet)["Render"] = e.assign<Render>(Render().FromLua(tab["Render"])).get(); } + if (tab["Velocity"] != nullptr) { + if (!e.has_component<Position>()) + e.assign<Position>(); + (*toRet)["Velocity"] = + e.assign<Velocity>(Velocity().FromLua(tab["Velocity"])).get(); + } + } else { std::cerr << "Parameter to spawn() must be a table!" << std::endl; } |