From 2bd1eaef1b9ec848933a40049ec8866e2b83a47d Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Mon, 16 Oct 2017 18:43:06 -0400 Subject: do not touch birb --- include/components/aggro.hpp | 11 +++++----- include/systems/lua.hpp | 12 ++++++----- src/systems/lua.cpp | 12 +++++------ src/systems/movement.cpp | 41 ++++++++++++++++++++++++++++++------ src/world.cpp | 3 ++- xml/!town.xml | 2 +- xml/entities.xml | 49 ++++++++++++++++++++++++++++++++++++++++++-- 7 files changed, 103 insertions(+), 27 deletions(-) diff --git a/include/components/aggro.hpp b/include/components/aggro.hpp index 17d646c..f559e6f 100644 --- a/include/components/aggro.hpp +++ b/include/components/aggro.hpp @@ -7,18 +7,17 @@ * Causes the entity to get mad at the player, charge and fight. */ struct Aggro : public Component { - Aggro(const std::string& a) - : arena(a) {} + Aggro(bool y = false) + : yes(y) {} Aggro(XMLElement* imp, XMLElement* def) { fromXML(imp, def); } - std::string arena; + bool yes; void fromXML(XMLElement* imp, XMLElement* def) final { - (void)imp; - // TODO null check..?, imp given - arena = def->StrAttribute("arena"); + (void)imp, (void)def; + // TODO } }; diff --git a/include/systems/lua.hpp b/include/systems/lua.hpp index 1137308..cb859ad 100644 --- a/include/systems/lua.hpp +++ b/include/systems/lua.hpp @@ -7,6 +7,8 @@ #include using LuaVariable = std::tuple; +using LuaList = std::vector; +using LuaRetList = std::vector; class LuaScript { private: @@ -14,7 +16,7 @@ private: std::string script; void setGlobal(const LuaVariable&) const; - void getReturns(std::vector& rets) const; + void getReturns(LuaRetList& 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++) { @@ -45,10 +47,10 @@ public: } void operator()(const std::string& func = "update") const; - 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, LuaList vars) const; + void operator()(const std::string& func, LuaRetList& rets, LuaList vars) const; + void operator()(LuaList vars) const; + void operator()(LuaRetList& rets, LuaList vars) const; }; class LuaSystem { diff --git a/src/systems/lua.cpp b/src/systems/lua.cpp index e412334..18d2ef1 100644 --- a/src/systems/lua.cpp +++ b/src/systems/lua.cpp @@ -6,7 +6,7 @@ void LuaScript::setGlobal(const LuaVariable& nv) const lua_setglobal(state, std::get(nv).c_str()); } -void LuaScript::getReturns(std::vector& rets) const +void LuaScript::getReturns(LuaRetList& rets) const { int count = lua_gettop(state); for (int i = 1; i <= count; i++) @@ -14,7 +14,7 @@ void LuaScript::getReturns(std::vector& rets) const lua_pop(state, count); } -void LuaScript::operator()(const std::string& func, std::vector vars) const +void LuaScript::operator()(const std::string& func, LuaList vars) const { for (auto& v : vars) setGlobal(v); @@ -25,8 +25,8 @@ void LuaScript::operator()(const std::string& func, std::vector var } } -void LuaScript::operator()(const std::string& func, std::vector& rets, - std::vector vars) const +void LuaScript::operator()(const std::string& func, LuaRetList& rets, + LuaList vars) const { for (auto& v : vars) setGlobal(v); @@ -38,7 +38,7 @@ void LuaScript::operator()(const std::string& func, std::vector& rets, } } -void LuaScript::operator()(std::vector vars) const +void LuaScript::operator()(LuaList vars) const { for (auto& v : vars) setGlobal(v); @@ -49,7 +49,7 @@ void LuaScript::operator()(std::vector vars) const } } -void LuaScript::operator()(std::vector& rets, std::vector vars) const +void LuaScript::operator()(LuaRetList& rets, LuaList vars) const { for (auto& v : vars) setGlobal(v); diff --git a/src/systems/movement.cpp b/src/systems/movement.cpp index 9948e03..73e5113 100644 --- a/src/systems/movement.cpp +++ b/src/systems/movement.cpp @@ -45,16 +45,16 @@ void MovementSystem::update(entityx::EntityManager &en, entityx::EventManager &e auto ppos = PlayerSystem::getPosition(); if (ppos.x > position.x && ppos.x < position.x + entity.component()->width) { - if (entity.has_component()) { + /*if (entity.has_component()) { //auto dim = entity.component(); //ev.emit(vec2(position.x + dim->width, position.y + dim->height), ATTACKKKKKK, false); - /*auto& h = entity.component()->health; + auto& h = entity.component()->health; if (h > 0) { fight = true; toFight = entity; h = 0; - }*/ - } else if (entity.has_component()) { + } + } else*/ if (entity.has_component()) { static bool triggering = false; if (!triggering) { triggering = true; @@ -72,11 +72,40 @@ void MovementSystem::update(entityx::EntityManager &en, entityx::EventManager &e } } + static auto doAttack = [](lua_State* s) -> int { + vec2 pos (lua_tonumber(s, 1), lua_tonumber(s, 2)); + LuaScript script ("effect = function()\nflash(255,0,0)\ndamage(1)\nend\n\ + hit = function()\nxrange = 5\nend"); + AttackSystem::initLua(script); + Attack attack = {vec2(), vec2(5, 5), vec2(), vec2(), + script, TextureIterator()}; + game::events.emit(AttackEvent(pos, + attack, false)); + return 0; + }; + // make the entity wander // TODO initialX and range? if (entity.has_component()) { - entity.component()->script({LuaVariable("vely", direction.y), - LuaVariable("velx", direction.x)}); + float aggro = 0; + LuaList vars = { + LuaVariable("vely", direction.y), + LuaVariable("velx", direction.x), + LuaVariable("playerx", ppos.x), + LuaVariable("playery", ppos.y), + LuaVariable("selfx", position.x), + LuaVariable("selfy", position.y), + LuaVariable("aggro", aggro) + }; + + bool hasAggro = entity.has_component(); + if (hasAggro) + aggro = entity.component()->yes ? 1 : 0; + if (aggro) + entity.component()->script.addFunction("attack", doAttack); + entity.component()->script(aggro ? "hostile" : "update", vars); + if (hasAggro) + entity.component()->yes = aggro > 0 ? 1 : 0; } } }); diff --git a/src/world.cpp b/src/world.cpp index 6a8dad8..3455b0e 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -150,10 +150,11 @@ static std::vector savedEntities; void WorldSystem::fight(entityx::Entity entity) { + UserError("fights unimplemented?"); std::string exit = currentXMLFile; savedEntities.emplace_back(entity.id()); - load(entity.component()->arena); + //load(entity.component()->arena); savedEntities.clear(); entity.component()->health = entity.component()->maxHealth; diff --git a/xml/!town.xml b/xml/!town.xml index 5c66a25..30adc94 100644 --- a/xml/!town.xml +++ b/xml/!town.xml @@ -22,7 +22,7 @@ - + diff --git a/xml/entities.xml b/xml/entities.xml index fda45b4..67f4fc2 100644 --- a/xml/entities.xml +++ b/xml/entities.xml @@ -86,9 +86,13 @@ end countdown = countdown - 1 end + + hostile = function() + + end - + + @@ -108,8 +112,18 @@ countdown = 0 + onetime = 0 update = function() + if (onetime == 0) then + onetime = 1 + selfy = playery + 300 + end + + if (math.abs(selfx - playerx) <= 150) then + aggro = 1 + end + if (countdown == 0) then countdown = math.random(3000, 5000) if (velx >= 0) then @@ -123,7 +137,38 @@ end countdown = countdown - 1 end + + dipping = 0 + oldy = 0 + + hostile = function() + if (dipping == 1) then + if (selfy >= oldy) then + dipping = 0 + aggro = 0 + vely = 0 + onetime = 0 + else + vely = vely + 0.0006 + if (math.abs(vely) < 0.01) then + attack(selfx, selfy) + end + end + else + if (selfy - playery > 200) then + if (playerx >= selfx) then + velx = 0.01 + else + velx = -0.01 + end + dipping = 1 + oldy = selfy + vely = -.2 + end + end + end + -- cgit v1.2.3