]> code.bitgloo.com Git - clyne/gamedev.git/commitdiff
better arrow collision
authorClyne Sullivan <tullivan99@gmail.com>
Tue, 28 Mar 2017 14:21:04 +0000 (10:21 -0400)
committerClyne Sullivan <tullivan99@gmail.com>
Tue, 28 Mar 2017 14:21:04 +0000 (10:21 -0400)
include/attack.hpp
src/attack.cpp
src/world.cpp
xml/!town.xml

index 027b93f8143540bdf7e9c93ca80a40bd04e6a052..ef67a7990fb73d0cdd270bf8ccfbeaf4b5b05c23 100644 (file)
@@ -10,7 +10,6 @@
 enum class AttackType : char {
        ShortSlash,
        LongSlash,
-       SmallBoom
 };
 
 struct AttackEvent {
index 3dc159452aa74e21ebee5defb42dc56f86892367..6fb6cecdb7f460beea5b8c713bc9b0f06a8734ef 100644 (file)
@@ -1,7 +1,10 @@
 #include <attack.hpp>
+
 #include <components.hpp>
 #include <engine.hpp>
 #include <particle.hpp>
+#include <player.hpp>
+#include <world.hpp>
 
 constexpr int shortSlashLength = 20;
 constexpr int longSlashLength = 40;
@@ -20,6 +23,11 @@ bool inrange(float point, float left, float right, float range)
                (point > left && point < right);
 }
 
+bool inrange(float point, float left, float right)
+{
+       return point > left && point < right;
+}
+
 void AttackSystem::receive(const AttackEvent& ae)
 {
        attacks.emplace_front(ae);
@@ -31,6 +39,26 @@ void AttackSystem::update(entityx::EntityManager& en, entityx::EventManager& ev,
        (void)ev;
        (void)dt;
 
+       auto pid = game::engine.getSystem<PlayerSystem>()->getId();
+
+       // handle attacking entities
+       en.each<Hit, Position>([&](entityx::Entity p, Hit& hit, Position& ppos) {
+               bool die = false;
+               en.each<Health, Position, Solid>([&](entityx::Entity e, Health& health, Position& pos, Solid& dim) {
+                       if (e.id() != pid && inrange(ppos.x, pos.x, pos.x + dim.width) && inrange(ppos.y, pos.y - 2, pos.y + dim.height)) {
+                               health.health -= hit.damage;
+                               game::engine.getSystem<ParticleSystem>()->addMultiple(15, ParticleType::SmallBlast,
+                                       [&](){ return vec2(pos.x + dim.width / 2, pos.y + dim.height / 2); }, 300, 7);
+                               die = true;
+                       } else if (game::engine.getSystem<WorldSystem>()->isAboveGround(vec2(ppos.x, ppos.y)))
+                               die = true;
+               });
+
+               if (die)
+                       p.destroy();
+       }); 
+
+       // handle emitted attacks
        for (const auto& a : attacks) {
                switch (a.type) {
                case AttackType::ShortSlash:
@@ -41,7 +69,8 @@ void AttackSystem::update(entityx::EntityManager& en, entityx::EventManager& ev,
                                        if (e.has_component<Player>())
                                                return;
 
-                                       if (inrange(a.pos.x, pos.x, pos.x + dim.width, HLINES(shortSlashLength))) {
+                                       if (inrange(a.pos.x, pos.x, pos.x + dim.width, HLINES(shortSlashLength)) &&
+                                               inrange(a.pos.y, pos.y, pos.y + dim.height)) {
                                                h.health -= a.power;
                                                game::engine.getSystem<ParticleSystem>()->addMultiple(15, ParticleType::DownSlash,
                                                        [&](){ return vec2(pos.x + dim.width / 2, pos.y + dim.height / 2); }, 300, 7);
@@ -49,21 +78,6 @@ void AttackSystem::update(entityx::EntityManager& en, entityx::EventManager& ev,
                                }
                        );
                        break;
-               case AttackType::SmallBoom:
-                       en.each<Position, Solid, Health>(
-                               [&a](entityx::Entity e, Position& pos, Solid& dim, Health& h) {
-                                       (void)e;
-                                       if (e.has_component<Player>())
-                                               return;
-
-                                       if (inrange(a.pos.x, pos.x, pos.x + dim.width, 0)) {
-                                               h.health -= a.power;
-                                               game::engine.getSystem<ParticleSystem>()->addMultiple(15, ParticleType::SmallBlast,
-                                                       [&](){ return vec2(pos.x + dim.width / 2, pos.y + dim.height / 2); }, 300, 7);
-                                       }
-                               }
-                       );
-                       break;
                default:
                        break;
                }
index 353f4d9128d48914e558879034d83c940ca1b697..9a475766295a2396e12d9621c8abb4f1d3b522d9 100644 (file)
@@ -14,7 +14,6 @@ using namespace std::literals::chrono_literals;
 using namespace tinyxml2;
 
 // game headers
-#include <attack.hpp>
 #include <common.hpp>
 #include <components.hpp>
 #include <debug.hpp>
@@ -1141,11 +1140,6 @@ void WorldSystem::detect(entityx::TimeDelta dt)
                        } else {
                                loc.y = data[line].groundHeight - 0.001f * dt;
                                vel.y = 0;
-                               if (e.has_component<Hit>()) {
-                                       game::events.emit<AttackEvent>(vec2(loc.x, loc.y),
-                                               AttackType::SmallBoom, e.component<Hit>()->damage);
-                                       e.destroy();
-                               }
                                if (!vel.grounded) {
                                        vel.grounded = true;
                                        game::engine.getSystem<ParticleSystem>()->addMultiple(20, ParticleType::SmallPoof,
index c50932bdd758076276f32fc8ac8bcc0c776d2f32..7506f700c8cdff3875f8053de60924a959db746d 100644 (file)
@@ -4,7 +4,7 @@
 <World>
     <style background="0" bgm="assets/music/embark.wav" folder="assets/style/classic/"/>
     <generation width="320"/>
-       <weather>Sunny</weather>
+       <weather>Snowy</weather>
     <link right="!town2.xml"/>
     <spawnx>-300</spawnx>
        <time>8000</time>