From e9758416b18b27a65337c28d9641afc0ee89b34b Mon Sep 17 00:00:00 2001 From: Andy Belle-Isle Date: Thu, 29 Aug 2019 01:39:40 -0400 Subject: Created (very messy) script system --- Scripts/init.lua | 14 +++ src/Script/entityx/EntityLua.hpp | 242 -------------------------------------- src/Script/entityx/LuaTypes.hpp | 96 --------------- src/Script/entityx/entity_lua.cpp | 183 ---------------------------- src/Script/entityx/entity_lua.hpp | 13 -- src/components/Component.hpp | 33 ++++++ src/components/Position.hpp | 47 ++++++++ src/main.cpp | 118 +++++++++++-------- src/script.hpp | 154 ++++++++++++++++++++++++ 9 files changed, 316 insertions(+), 584 deletions(-) delete mode 100644 src/Script/entityx/EntityLua.hpp delete mode 100644 src/Script/entityx/LuaTypes.hpp delete mode 100644 src/Script/entityx/entity_lua.cpp delete mode 100644 src/Script/entityx/entity_lua.hpp create mode 100644 src/components/Component.hpp create mode 100644 src/components/Position.hpp create mode 100644 src/script.hpp diff --git a/Scripts/init.lua b/Scripts/init.lua index 20ea044..5b0f915 100644 --- a/Scripts/init.lua +++ b/Scripts/init.lua @@ -24,5 +24,19 @@ bird = { end } +dog = { + Position = { + x = 6.5, + y = 1.3 + }, + init = function(self) + print(self.Position.x .. "," .. self.Position.y) + end +} + birdSpawn = game.spawn(bird); birdSpawn:init() + +dogSpawn = game.spawn(dog); +dogSpawn:init() +dogSpawn.Position.x = 37.5 diff --git a/src/Script/entityx/EntityLua.hpp b/src/Script/entityx/EntityLua.hpp deleted file mode 100644 index 780f051..0000000 --- a/src/Script/entityx/EntityLua.hpp +++ /dev/null @@ -1,242 +0,0 @@ -#ifndef ENTITYLUA_HPP -#define ENTITYLUA_HPP - -// Check is_trivially_destructible so we don't need __gc for userdata. -static_assert(std::is_trivially_destructible::value, "Entity::Id is not trivially destructible"); -// Check is_copy_constructible so that we can copy data onto memory for userdata created by lua. -static_assert(std::is_copy_constructible::value, "Entity::Id is not copy constructible"); - -namespace entityx -{ - namespace lua - { - typedef std::shared_ptr EntityManagerSharedPtr; - - // This component is internally assigned to all entities created from lua. - // It is used to store the ref of the lua table - struct LuaComponent : entityx::Component - { - // ref of the lua object in LUA_REGISTRYINDEX - int ref; - LuaComponent(int ref = LUA_REFNIL) : ref(ref) {} - }; - - // This function should be called right after luaL_openlibs, to create global entityx table - void setup_entityx_api(lua_State* L); - - // This function add a new entity manager to lua global "entityx.xxx" where xxx is the name provided - void new_entity_manager(lua_State* L, const EntityManagerSharedPtr& manager, const char* name); - - // This function is used to push an entity manager to lua. - // It only pushes the userdata onto the stack, so use lua api such as lua_setglobal, - // lua_setfield, or lua_settable to really save it to lua. - void push_entity_manager(lua_State* L, const EntityManagerSharedPtr& manager); - - typedef std::map> ComponentList; - extern ComponentList components; - - template - class MemberRegister - { - lua_State* L; - public: - MemberRegister(lua_State* L) : L(L) {} - - template - void add(const char* name, T C::*ptr) - { - // Now the ComponentHandler should be at #-2 of stack (-1 is the metatable), this will be used as up-values - lua_pushvalue(L, -2); - // Also remember the offset - int offset = typename ComponentHelper::offset(ptr); - lua_pushinteger(L, offset); - lua_pushcclosure(L, [](lua_State* L){ - // This function is used as both getter and setter: - // value = entity.comp.x() - // entity.comp.x(value) - // Note that self is not needed (that is, use comp.x() instead of comp:x()) - typename ComponentHelper::ptr* pptr = static_cast::ptr*>(lua_touserdata(L, lua_upvalueindex(1))); - int offset = lua_tointeger(L, lua_upvalueindex(2)); - - T& ref = typename ComponentHelper::get(**pptr, offset); - if (lua_gettop(L) == 0 || lua_isnil(L, 1)) - { - // Getter mode, push value of at pptr - CToLua(L, ref); - } - else - { - // Setter mode, set value to pptr - LuaToC(L, ref); - } - return 1; - }, 2); - lua_setfield(L, -2, name); - } - }; - - template - struct ComponentHelper - { - public: - // Use this ptr to get the ComponentHandle type, to support both 0.x and 1.x of entityx. - typedef decltype(entityx::Entity().assign()) ptr; - - template - static ptrdiff_t offset(Member ptr) - { - C c; - return static_cast(static_cast(&(c.*ptr))) - static_cast(static_cast(&c)); - } - - template - static T& get(C& c, ptrdiff_t offset) - { - return *static_cast(static_cast( - static_cast(static_cast(&c)) + offset - )); - } - - typedef std::function&)> memreg_func; - }; - - namespace lua_params - { - // gens::type = seq<0, 1, ..., n-1> - template - struct seq {}; - - template - struct gens : gens { }; - - template - struct gens<0, S...> { - typedef seq type; - }; - - template - T get_param_from_lua(lua_State* L, int n) - { - T ret; - lua_pushvalue(L, n); - LuaToC(L, ret); - return ret; - } - - template - Ret call_func(lua_State* L, Func func, seq) - { - // This function is called with a table on top of the stack, - // first we call table.unpack to get the elements in the table - lua_getglobal(L, "table"); - lua_getfield(L, -1, "unpack"); - lua_remove(L, -2); // now function is pushed - lua_pushvalue(L, -2); // push the table - lua_call(L, 1, sizeof...(N)); // we only receive sizeof...(N) results - - // Then we call func with get_param_from_lua - int start_from = lua_gettop(L) - sizeof...(N)+1; - // Note that N... itself starts from 0 - return func(std::forward(get_param_from_lua(L, N + start_from))...); - } - - // std::function version - template - Ret call_func(lua_State* L, const std::function& func) - { - return call_func, Ret, Args...>(L, func, gens::type()); - } - } - - template - std::function wrap_ctor(const std::function& func) - { - return [func](lua_State* L) { - return lua_params::call_func(L, func); - }; - } - - template - std::function wrap_ctor_no_args(C(*func)()) - { - return [func](lua_State* L) { - return lua_params::call_func(L, func); - }; - } - - template - std::function use_default_ctor() - { - static_assert(std::is_constructible::value, "Not able to use default ctor here"); - return [](lua_State* L) { - return C(); - }; - } - - template - void export_component(Str name, Ctor ctor, const std::function&)>& memreg) - { - static_assert(std::is_constructible::value, "Bad type of name"); - static_assert(std::is_same>::value, "Bad type of ctor"); - - // Create memreg as a std::function - //ComponentHelper::memreg_func memreg_func_save = memreg; - - std::function create_component = [ctor, memreg](lua_State* L) { - // This function is called with (name, entity_manager_weak_pointer, id, params) - EntityManagerSharedPtr* m = static_cast(lua_touserdata(L, 2)); - entityx::Entity::Id* id = static_cast(lua_touserdata(L, 3)); - entityx::Entity entity = (*m)->get(*id); - - typedef typename ComponentHelper::ptr CompPtr; - // Create the c++ component - CompPtr component = entity.assign(std::move(ctor(L))); - - // Create the component as a lightuserdata (although no data is stored in it) - lua_pushlightuserdata(L, 0); - - // Push CompPtr which will be used in MemberRegister (will be removed after pushing members) - lua_newuserdata(L, sizeof(CompPtr)); - new (lua_touserdata(L, -1)) CompPtr(component); - - // If needed, create metatable for it (we only need __gc) - if (!std::is_trivially_destructible::value) - { - lua_newtable(L); - lua_pushcfunction(L, [](lua_State* L){ - CompPtr* component = static_cast(lua_touserdata(L, -1)); - component->~CompPtr(); - return 0; - }); - lua_setfield(L, -2, "__gc"); - lua_setmetatable(L, -2); - } - - // Stack: lightuserdata(0) | CompPtr - - // Create metatable for the lightuserdata - lua_newtable(L); - lua_pushvalue(L, -1); - lua_setfield(L, -2, "__index"); - - // Stack: lightuserdata(0) | CompPtr | metatable - - // Now we're ready to push members - MemberRegister reg(L); - memreg(reg); - - // Stack: lightuserdata(0) | CompPtr | metatable - - // Remove the CompPtr - lua_remove(L, -2); - // Set metatable for the lightuserdata - lua_setmetatable(L, -2); - - // Leave the result on top of the stack - }; - components[name] = std::move(create_component); - } - } -} - -#endif//ENTITYLUA_HPP diff --git a/src/Script/entityx/LuaTypes.hpp b/src/Script/entityx/LuaTypes.hpp deleted file mode 100644 index 66d1f47..0000000 --- a/src/Script/entityx/LuaTypes.hpp +++ /dev/null @@ -1,96 +0,0 @@ -#ifndef LUATYPES_HPP -#define LUATYPES_HPP - -#include "lua.hpp" - -namespace entityx -{ - namespace lua - { - /************* - * GENERIC * - *************/ - template - inline void LuaToC(lua_State* L, T& ref); - - template - inline void CToLua(lua_State* L, T& ref); - - /************ - * DOUBLE * - ************/ - template<> - inline void LuaToC(lua_State* L, double& ref) - { - ref = lua_tonumber(L, -1); - } - - template<> - inline void CToLua(lua_State* L, double& ref) - { - lua_pushnumber(L, ref); - } - - /*********** - * FLOAT * - ***********/ - template<> - inline void LuaToC(lua_State* L, float& ref) - { - ref = static_cast(lua_tonumber(L, -1)); - } - - template<> - inline void CToLua(lua_State* L, float& ref) - { - lua_pushnumber(L, ref); - } - - /************* - * INTEGER * - *************/ - template<> - inline void LuaToC(lua_State* L, int& ref) - { - ref = lua_tointeger(L, -1); - } - - template<> - inline void CToLua(lua_State* L, int& ref) - { - lua_pushnumber(L, ref); - } - - /************* - * BOOLEAN * - *************/ - template<> - inline void LuaToC(lua_State* L, bool& ref) - { - ref = lua_toboolean(L, -1); - } - - template<> - inline void CToLua(lua_State* L, bool& ref) - { - lua_pushboolean(L, ref); - } - - /************ - * STRING * - ************/ - template<> - inline void LuaToC(lua_State* L, std::string& ref) - { - ref = lua_tostring(L, -1); - } - - template<> - inline void CToLua(lua_State* L, std::string& ref) - { - lua_pushstring(L, ref.c_str()); - } - } -} - -#endif//LUATYPES_HPP diff --git a/src/Script/entityx/entity_lua.cpp b/src/Script/entityx/entity_lua.cpp deleted file mode 100644 index b23628b..0000000 --- a/src/Script/entityx/entity_lua.cpp +++ /dev/null @@ -1,183 +0,0 @@ -//#include "entityx_lua.h" -#include