From b709a392436d4ed17e214cd9e302ddbd23d71c21 Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Thu, 12 Oct 2017 08:32:21 -0400 Subject: more lua scripting --- include/attack.hpp | 5 ++++- include/components/hit.hpp | 8 ++++---- include/systems/lua.hpp | 18 +++++++++++++++--- 3 files changed, 23 insertions(+), 8 deletions(-) (limited to 'include') diff --git a/include/attack.hpp b/include/attack.hpp index 941e3d7..deea418 100644 --- a/include/attack.hpp +++ b/include/attack.hpp @@ -6,16 +6,17 @@ #include #include +#include #include #include struct Attack { - int power; vec2 offset; vec2 range; vec2 vel; // TODO use vec2 accel; // TODO use + LuaScript script; TextureIterator effect; }; @@ -50,6 +51,8 @@ public: bool receive(const AttackEvent& ae); void update(entityx::EntityManager& en, entityx::EventManager& ev, entityx::TimeDelta dt) override; static void render(void); + + static void initLua(LuaScript& s); }; #endif // ATTACK_HPP_ diff --git a/include/components/hit.hpp b/include/components/hit.hpp index 334712b..45e6466 100644 --- a/include/components/hit.hpp +++ b/include/components/hit.hpp @@ -3,14 +3,14 @@ #include "base.hpp" +#include #include struct Hit : public Component { - Hit(int d, bool p = false) - : damage(d), pierce(p) {} + Hit(Attack* a) + : attack(a) {} - int damage; - bool pierce; + Attack* attack; TextureIterator effect; void fromXML(XMLElement* imp, XMLElement* def) final { diff --git a/include/systems/lua.hpp b/include/systems/lua.hpp index 8c4698a..2df038a 100644 --- a/include/systems/lua.hpp +++ b/include/systems/lua.hpp @@ -13,7 +13,8 @@ private: lua_State* state; std::string script; - void setGlobal(const LuaVariable&); + void setGlobal(const LuaVariable&) const; + void getReturns(std::vector& rets) const; static void replace(std::string& s, const std::string& rid, const std::string& put) { for (unsigned int i = 0; i < s.size(); i++) { @@ -35,8 +36,19 @@ public: lua_pcall(state, 0, 0, 0); } - void operator()(std::vector vars); - void operator()(void); + inline lua_State* getState(void) + { return state; } + + inline void addFunction(const std::string& name, lua_CFunction func) { + lua_pushcclosure(state, func, 0); + lua_setglobal(state, name.c_str()); + } + + void operator()(const std::string& func, std::vector vars) const; + void operator()(const std::string& func, std::vector& rets, std::vector vars) const; + void operator()(std::vector vars) const; + void operator()(std::vector& rets, std::vector vars) const; + void operator()(const std::string& func = "update") const; }; class LuaSystem { -- cgit v1.2.3