diff options
author | Clyne Sullivan <tullivan99@gmail.com> | 2017-10-16 18:43:06 -0400 |
---|---|---|
committer | Clyne Sullivan <tullivan99@gmail.com> | 2017-10-16 18:43:06 -0400 |
commit | 2bd1eaef1b9ec848933a40049ec8866e2b83a47d (patch) | |
tree | 714a018bdc4b42aa1e9bcac5270190d068cf177b | |
parent | 07b3ae994d0e68b5c266f5aa212024d5064847c2 (diff) |
do not touch birb
-rw-r--r-- | include/components/aggro.hpp | 11 | ||||
-rw-r--r-- | include/systems/lua.hpp | 12 | ||||
-rw-r--r-- | src/systems/lua.cpp | 12 | ||||
-rw-r--r-- | src/systems/movement.cpp | 41 | ||||
-rw-r--r-- | src/world.cpp | 3 | ||||
-rw-r--r-- | xml/!town.xml | 2 | ||||
-rw-r--r-- | 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 <vector> using LuaVariable = std::tuple<std::string, float&>; +using LuaList = std::vector<LuaVariable>; +using LuaRetList = std::vector<double>; class LuaScript { private: @@ -14,7 +16,7 @@ private: std::string script; void setGlobal(const LuaVariable&) const; - void getReturns(std::vector<double>& 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<LuaVariable> vars) const; - void operator()(const std::string& func, std::vector<double>& rets, std::vector<LuaVariable> vars) const; - void operator()(std::vector<LuaVariable> vars) const; - void operator()(std::vector<double>& rets, std::vector<LuaVariable> 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<std::string>(nv).c_str()); } -void LuaScript::getReturns(std::vector<double>& 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<double>& rets) const lua_pop(state, count); } -void LuaScript::operator()(const std::string& func, std::vector<LuaVariable> 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<LuaVariable> var } } -void LuaScript::operator()(const std::string& func, std::vector<double>& rets, - std::vector<LuaVariable> 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<double>& rets, } } -void LuaScript::operator()(std::vector<LuaVariable> vars) const +void LuaScript::operator()(LuaList vars) const { for (auto& v : vars) setGlobal(v); @@ -49,7 +49,7 @@ void LuaScript::operator()(std::vector<LuaVariable> vars) const } } -void LuaScript::operator()(std::vector<double>& rets, std::vector<LuaVariable> 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<Solid>()->width) { - if (entity.has_component<Aggro>()) { + /*if (entity.has_component<Aggro>()) { //auto dim = entity.component<Solid>(); //ev.emit<AttackEvent>(vec2(position.x + dim->width, position.y + dim->height), ATTACKKKKKK, false); - /*auto& h = entity.component<Health>()->health; + auto& h = entity.component<Health>()->health; if (h > 0) { fight = true; toFight = entity; h = 0; - }*/ - } else if (entity.has_component<Trigger>()) { + } + } else*/ if (entity.has_component<Trigger>()) { 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>(AttackEvent(pos, + attack, false)); + return 0; + }; + // make the entity wander // TODO initialX and range? if (entity.has_component<Wander>()) { - entity.component<Wander>()->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<Aggro>(); + if (hasAggro) + aggro = entity.component<Aggro>()->yes ? 1 : 0; + if (aggro) + entity.component<Wander>()->script.addFunction("attack", doAttack); + entity.component<Wander>()->script(aggro ? "hostile" : "update", vars); + if (hasAggro) + entity.component<Aggro>()->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<entityx::Entity::Id> savedEntities; void WorldSystem::fight(entityx::Entity entity) { + UserError("fights unimplemented?"); std::string exit = currentXMLFile; savedEntities.emplace_back(entity.id()); - load(entity.component<Aggro>()->arena); + //load(entity.component<Aggro>()->arena); savedEntities.clear(); entity.component<Health>()->health = entity.component<Health>()->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 @@ <structure type="1" position="300.0,100.0"/> <structure inside="bobshouse.xml" type="1" position="10.0,100.0"/> <skirl /> - <birb /> + <birb position="-300.0,100.0" /> </World> <Dialog name="Bob"> 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 </Wander> - <!--<Aggro arena="arena.xml" /> - <Trigger />--> + <Aggro/> + <!--<Trigger />--> <Drop> <item name="Arrow" min="3" max="5" /> </Drop> @@ -108,8 +112,18 @@ <Name value="birb" /> <Wander> 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 </Wander> + <Aggro/> </birb> <structure> |