diff --git a/core/src/com/saltosion/gladiator/GladiatorBrawler.java b/core/src/com/saltosion/gladiator/GladiatorBrawler.java
index e0e10a8..a55e9a5 100644
--- a/core/src/com/saltosion/gladiator/GladiatorBrawler.java
+++ b/core/src/com/saltosion/gladiator/GladiatorBrawler.java
@@ -11,6 +11,7 @@ import com.saltosion.gladiator.components.CCombat;
import com.saltosion.gladiator.components.CPhysics;
import com.saltosion.gladiator.components.CRenderedObject;
import com.saltosion.gladiator.input.InputHandler;
+import com.saltosion.gladiator.listeners.CombatListener;
import com.saltosion.gladiator.systems.CombatSystem;
import com.saltosion.gladiator.systems.MiscManagerSystem;
import com.saltosion.gladiator.systems.PhysicsSystem;
@@ -106,7 +107,19 @@ public class GladiatorBrawler extends ApplicationAdapter {
renderedObject.playAnimation("Idle");
dummy.add(renderedObject);
dummy.add(new CPhysics().setSize(2, 4).setPosition(-6, 5));
- dummy.add(new CCombat().setBaseDamage(100).setHealth(1000).setSwingCD(.5f));
+ dummy.add(new CCombat().setBaseDamage(100).setHealth(1000).setSwingCD(.5f).setCombatListener(
+ new CombatListener() {
+ @Override
+ public void died(Entity source, int damageTaken) {
+ System.out.println("Nooooo! I died! I will revenge this!");
+ }
+
+ @Override
+ public void damageTaken(Entity source, int damageTaken) {
+ System.out.println(String.format("I took %d damage! Damnit!", damageTaken));
+ }
+
+ }));
engine.addEntity(dummy);
}
diff --git a/core/src/com/saltosion/gladiator/components/CCombat.java b/core/src/com/saltosion/gladiator/components/CCombat.java
index 0274eac..ebcd4a0 100644
--- a/core/src/com/saltosion/gladiator/components/CCombat.java
+++ b/core/src/com/saltosion/gladiator/components/CCombat.java
@@ -4,6 +4,7 @@ import java.util.HashMap;
import com.badlogic.ashley.core.Component;
import com.badlogic.gdx.math.Vector2;
+import com.saltosion.gladiator.listeners.CombatListener;
import com.saltosion.gladiator.util.Direction;
public class CCombat extends Component {
@@ -12,6 +13,7 @@ public class CCombat extends Component {
private int maxHealth = 0;
private int damage = 0;
private Vector2 swingsize = new Vector2(4, 3);
+ private CombatListener combatListener;
private Vector2 swinging = new Vector2();
private float swingCd = 0;
@@ -62,6 +64,11 @@ public class CCombat extends Component {
return this;
}
+ public CCombat setCombatListener(CombatListener listener) {
+ this.combatListener = listener;
+ return this;
+ }
+
public int getMaxHealth() {
return this.maxHealth;
}
@@ -100,4 +107,8 @@ public class CCombat extends Component {
public Vector2 getSwingSize() {
return this.swingsize;
}
+
+ public CombatListener getCombatListener() {
+ return this.combatListener;
+ }
}
diff --git a/core/src/com/saltosion/gladiator/listeners/CombatListener.java b/core/src/com/saltosion/gladiator/listeners/CombatListener.java
new file mode 100644
index 0000000..6035394
--- /dev/null
+++ b/core/src/com/saltosion/gladiator/listeners/CombatListener.java
@@ -0,0 +1,20 @@
+package com.saltosion.gladiator.listeners;
+
+import com.badlogic.ashley.core.Entity;
+
+public interface CombatListener {
+
+ /**
+ * This is called when the entity dies.
+ * @param source The Entity that dealt the damage. Null if called without source.
+ * @param damageTaken The damage dealt, that killed the entity.
+ */
+ public void died(Entity source, int damageTaken);
+
+ /**
+ * This is called when the entity takes damage.
+ * @param source Source of the damage. Null if called without source.
+ * @param damageTaken Amout of damage taken.
+ */
+ public void damageTaken(Entity source, int damageTaken);
+}
diff --git a/core/src/com/saltosion/gladiator/listeners/SwingHitboxListener.java b/core/src/com/saltosion/gladiator/listeners/SwingHitboxListener.java
index e987803..03253ae 100644
--- a/core/src/com/saltosion/gladiator/listeners/SwingHitboxListener.java
+++ b/core/src/com/saltosion/gladiator/listeners/SwingHitboxListener.java
@@ -5,6 +5,7 @@ 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.systems.CombatSystem;
import com.saltosion.gladiator.util.AppUtil;
import com.saltosion.gladiator.util.Direction;
@@ -27,16 +28,10 @@ public class SwingHitboxListener implements CollisionListener {
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(String.format("Entity was hit for %d damage, it now has only %d health left!",
- damage, otherCombat.health));
- if (otherCombat.health <= 0) {
- AppUtil.engine.removeEntity(other);
- }
+ int damage = cm.get(source).getDamage();
+ CombatSystem.dealDamage(source, other, damage);
}
}
diff --git a/core/src/com/saltosion/gladiator/systems/CombatSystem.java b/core/src/com/saltosion/gladiator/systems/CombatSystem.java
index e89802c..b488763 100644
--- a/core/src/com/saltosion/gladiator/systems/CombatSystem.java
+++ b/core/src/com/saltosion/gladiator/systems/CombatSystem.java
@@ -12,6 +12,7 @@ import com.saltosion.gladiator.components.CCombat;
import com.saltosion.gladiator.components.CDestructive;
import com.saltosion.gladiator.components.CPhysics;
import com.saltosion.gladiator.components.CRenderedObject;
+import com.saltosion.gladiator.listeners.CombatListener;
import com.saltosion.gladiator.listeners.SwingHitboxListener;
import com.saltosion.gladiator.util.AppUtil;
import com.saltosion.gladiator.util.Direction;
@@ -88,5 +89,38 @@ public class CombatSystem extends EntitySystem {
public void updateEntities(Engine engine) {
entities = engine.getEntitiesFor(Family.getFor(CCombat.class));
}
+
+ /**
+ * Deal damage to target. Source is optional, leave null if none.
+ * @param source Source of the damage.
+ * @param target Target to kill.
+ * @param damage Damage taken, that was dealth to the target.
+ */
+ public static void dealDamage(Entity source, Entity target, int damage) {
+ CCombat combat = target.getComponent(CCombat.class);
+ CombatListener listener = combat.getCombatListener();
+ if (listener != null) {
+ listener.damageTaken(source, damage);
+ }
+ combat.health -= damage;
+ if (combat.health <= 0) {
+ killEntity(source, target, damage);
+ }
+ }
+
+ /**
+ * Straight off kill the target.
+ * @param source Source of the damage.
+ * @param target Target to kill.
+ * @param damage Damage taken, that killed target.
+ */
+ public static void killEntity(Entity source, Entity target, int damage) {
+ CCombat combat = target.getComponent(CCombat.class);
+ CombatListener listener = combat.getCombatListener();
+ if (listener != null) {
+ listener.died(source, damage);
+ }
+ AppUtil.engine.removeEntity(target);
+ }
}