]> code.bitgloo.com Git - clyne/gamedev.git/commitdiff
do not touch birb
authorClyne Sullivan <tullivan99@gmail.com>
Mon, 16 Oct 2017 22:43:06 +0000 (18:43 -0400)
committerClyne Sullivan <tullivan99@gmail.com>
Mon, 16 Oct 2017 22:43:06 +0000 (18:43 -0400)
include/components/aggro.hpp
include/systems/lua.hpp
src/systems/lua.cpp
src/systems/movement.cpp
src/world.cpp
xml/!town.xml
xml/entities.xml

index 17d646c4fbeacf256af0b4a48825472e0648af8f..f559e6fe8989d656788438391ea55d503218b926 100644 (file)
@@ -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
        }
 };
 
index 1137308d5cebb2e656c46fa210cd66ef8dd2fa03..cb859ad73466e844935a9926b0678ad4adc2ee19 100644 (file)
@@ -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 {
index e412334f043844fb554315b1b946fe763d9cc3e5..18d2ef1af9fc3e2310abf360bb5696db0cf29434 100644 (file)
@@ -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);
index 9948e0348fddc16ceb9a0e76062f85813ac1faa2..73e5113ca0912937802be0f2e9985fdd37b03c2f 100644 (file)
@@ -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;
                        }
                }
        });
index 6a8dad84e5e8b1e39155e5eee65789d550e34412..3455b0e3f05da344ad88a5af94e1d8aa12807fe4 100644 (file)
@@ -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;
index 5c66a256146a8ff17f67ebf43724ec2bd2060827..30adc94546d3761aadb096c30054c48d28dfad73 100644 (file)
@@ -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">
index fda45b40ca59b3490790144d7a55e42f7ceaf423..67f4fc274f84ee27206d7cd29dee7f7092275a15 100644 (file)
                        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>
        <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) &lt;= 150) then
+                               aggro = 1
+                       end
+
                        if (countdown == 0) then
                                countdown = math.random(3000, 5000)
                                if (velx &gt;= 0) then
                        end
                        countdown = countdown - 1
                end
+
+               dipping = 0
+               oldy = 0
+
+               hostile = function()
+                       if (dipping == 1) then
+                               if (selfy &gt;= oldy) then
+                                       dipping = 0
+                                       aggro = 0
+                                       vely = 0
+                                       onetime = 0
+                               else
+                                       vely = vely + 0.0006
+                                       if (math.abs(vely) &lt; 0.01) then
+                                               attack(selfx, selfy)
+                                       end
+                               end
+                       else
+                               if (selfy - playery &gt; 200) then
+                                       if (playerx &gt;= selfx) then
+                                               velx = 0.01
+                                       else
+                                               velx = -0.01
+                                       end
+                                       dipping = 1
+                                       oldy = selfy
+                                       vely = -.2
+                               end
+                       end
+               end
        </Wander>
+       <Aggro/>
 </birb>
 
 <structure>