From 1a2f541390a2632488ba52a779882c4b70f63cc0 Mon Sep 17 00:00:00 2001 From: Allexit Date: Sun, 10 May 2015 21:07:37 +0300 Subject: [PATCH] Added CollisionListener for Swings --- .../CollisionListener.java | 3 +- .../SwingHitboxListener.java | 37 +++++++++++++++++++ .../gladiator/components/CPhysics.java | 2 +- .../gladiator/systems/CombatSystem.java | 5 +-- 4 files changed, 42 insertions(+), 5 deletions(-) rename core/src/com/saltosion/gladiator/{util => collisionlisteners}/CollisionListener.java (84%) create mode 100644 core/src/com/saltosion/gladiator/collisionlisteners/SwingHitboxListener.java diff --git a/core/src/com/saltosion/gladiator/util/CollisionListener.java b/core/src/com/saltosion/gladiator/collisionlisteners/CollisionListener.java similarity index 84% rename from core/src/com/saltosion/gladiator/util/CollisionListener.java rename to core/src/com/saltosion/gladiator/collisionlisteners/CollisionListener.java index 45b945f..7364acc 100644 --- a/core/src/com/saltosion/gladiator/util/CollisionListener.java +++ b/core/src/com/saltosion/gladiator/collisionlisteners/CollisionListener.java @@ -1,6 +1,7 @@ -package com.saltosion.gladiator.util; +package com.saltosion.gladiator.collisionlisteners; import com.badlogic.ashley.core.Entity; +import com.saltosion.gladiator.util.Direction; /** * diff --git a/core/src/com/saltosion/gladiator/collisionlisteners/SwingHitboxListener.java b/core/src/com/saltosion/gladiator/collisionlisteners/SwingHitboxListener.java new file mode 100644 index 0000000..43c07ce --- /dev/null +++ b/core/src/com/saltosion/gladiator/collisionlisteners/SwingHitboxListener.java @@ -0,0 +1,37 @@ +package com.saltosion.gladiator.collisionlisteners; + +import java.util.ArrayList; + +import com.badlogic.ashley.core.ComponentMapper; +import com.badlogic.ashley.core.Entity; +import com.saltosion.gladiator.components.CCombat; +import com.saltosion.gladiator.util.Direction; + +public class SwingHitboxListener implements CollisionListener { + + private ArrayList hitEntities = new ArrayList(); + private ComponentMapper cm = ComponentMapper.getFor(CCombat.class); + private Entity source; + + public SwingHitboxListener(Entity source) { + this.source = source; + } + + @Override + public void collision(Direction side, Entity host, Entity other) { + if (other.equals(source) || hitEntities.contains(other)) { + return; // These entities don't need to take damage + } + hitEntities.add(other); + + CCombat otherCombat = cm.get(other); + if (otherCombat == null) { + System.out.println("This entity doesn't have combat!"); + return; + } + int damage = cm.get(source).getDamage(); + otherCombat.health -= damage; + System.out.println("Entity was hit for " + damage + " damage!"); + } + +} diff --git a/core/src/com/saltosion/gladiator/components/CPhysics.java b/core/src/com/saltosion/gladiator/components/CPhysics.java index f20c462..4270793 100644 --- a/core/src/com/saltosion/gladiator/components/CPhysics.java +++ b/core/src/com/saltosion/gladiator/components/CPhysics.java @@ -2,7 +2,7 @@ package com.saltosion.gladiator.components; import com.badlogic.ashley.core.Component; import com.badlogic.gdx.math.Vector2; -import com.saltosion.gladiator.util.CollisionListener; +import com.saltosion.gladiator.collisionlisteners.CollisionListener; public class CPhysics extends Component { diff --git a/core/src/com/saltosion/gladiator/systems/CombatSystem.java b/core/src/com/saltosion/gladiator/systems/CombatSystem.java index 9501fad..6dcfef2 100644 --- a/core/src/com/saltosion/gladiator/systems/CombatSystem.java +++ b/core/src/com/saltosion/gladiator/systems/CombatSystem.java @@ -8,6 +8,7 @@ import com.badlogic.ashley.core.Family; import com.badlogic.ashley.utils.ImmutableArray; import com.badlogic.gdx.graphics.g2d.Sprite; import com.badlogic.gdx.math.Vector2; +import com.saltosion.gladiator.collisionlisteners.SwingHitboxListener; import com.saltosion.gladiator.components.CCombat; import com.saltosion.gladiator.components.CDestructive; import com.saltosion.gladiator.components.CPhysics; @@ -40,7 +41,6 @@ public class CombatSystem extends EntitySystem { } if (combat.swingCdCounter > 0) { return; } // Ready to swing ! - System.out.println("Ready to swing!"); combat.getSwing().setZero(); if (combat.inputs.get(Direction.LEFT)) { @@ -67,7 +67,6 @@ public class CombatSystem extends EntitySystem { } createSwingHitbox(e, pos); - System.out.println("Swing to " + combat.getSwingDirection().toString()); combat.swingCdCounter = combat.getSwingCD(); } } @@ -80,7 +79,7 @@ public class CombatSystem extends EntitySystem { e.add(new CRenderedObject(s)); e.add(new CPhysics().setGhost(true).setGravityApplied(false).setMovable(false) .setSize(combat.getSwingSize())); - e.getComponent(CPhysics.class).setPosition(position); + e.getComponent(CPhysics.class).setPosition(position).setCollisionListener(new SwingHitboxListener(source)); e.add(new CDestructive(.1f)); AppUtil.engine.addEntity(e);