Created CombatListeners, good for therapy after combat
This commit is contained in:
parent
c758862906
commit
6d00222adb
@ -11,6 +11,7 @@ import com.saltosion.gladiator.components.CCombat;
|
|||||||
import com.saltosion.gladiator.components.CPhysics;
|
import com.saltosion.gladiator.components.CPhysics;
|
||||||
import com.saltosion.gladiator.components.CRenderedObject;
|
import com.saltosion.gladiator.components.CRenderedObject;
|
||||||
import com.saltosion.gladiator.input.InputHandler;
|
import com.saltosion.gladiator.input.InputHandler;
|
||||||
|
import com.saltosion.gladiator.listeners.CombatListener;
|
||||||
import com.saltosion.gladiator.systems.CombatSystem;
|
import com.saltosion.gladiator.systems.CombatSystem;
|
||||||
import com.saltosion.gladiator.systems.MiscManagerSystem;
|
import com.saltosion.gladiator.systems.MiscManagerSystem;
|
||||||
import com.saltosion.gladiator.systems.PhysicsSystem;
|
import com.saltosion.gladiator.systems.PhysicsSystem;
|
||||||
@ -106,7 +107,19 @@ public class GladiatorBrawler extends ApplicationAdapter {
|
|||||||
renderedObject.playAnimation("Idle");
|
renderedObject.playAnimation("Idle");
|
||||||
dummy.add(renderedObject);
|
dummy.add(renderedObject);
|
||||||
dummy.add(new CPhysics().setSize(2, 4).setPosition(-6, 5));
|
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);
|
engine.addEntity(dummy);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,6 +4,7 @@ import java.util.HashMap;
|
|||||||
|
|
||||||
import com.badlogic.ashley.core.Component;
|
import com.badlogic.ashley.core.Component;
|
||||||
import com.badlogic.gdx.math.Vector2;
|
import com.badlogic.gdx.math.Vector2;
|
||||||
|
import com.saltosion.gladiator.listeners.CombatListener;
|
||||||
import com.saltosion.gladiator.util.Direction;
|
import com.saltosion.gladiator.util.Direction;
|
||||||
|
|
||||||
public class CCombat extends Component {
|
public class CCombat extends Component {
|
||||||
@ -12,6 +13,7 @@ public class CCombat extends Component {
|
|||||||
private int maxHealth = 0;
|
private int maxHealth = 0;
|
||||||
private int damage = 0;
|
private int damage = 0;
|
||||||
private Vector2 swingsize = new Vector2(4, 3);
|
private Vector2 swingsize = new Vector2(4, 3);
|
||||||
|
private CombatListener combatListener;
|
||||||
|
|
||||||
private Vector2 swinging = new Vector2();
|
private Vector2 swinging = new Vector2();
|
||||||
private float swingCd = 0;
|
private float swingCd = 0;
|
||||||
@ -62,6 +64,11 @@ public class CCombat extends Component {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public CCombat setCombatListener(CombatListener listener) {
|
||||||
|
this.combatListener = listener;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
public int getMaxHealth() {
|
public int getMaxHealth() {
|
||||||
return this.maxHealth;
|
return this.maxHealth;
|
||||||
}
|
}
|
||||||
@ -100,4 +107,8 @@ public class CCombat extends Component {
|
|||||||
public Vector2 getSwingSize() {
|
public Vector2 getSwingSize() {
|
||||||
return this.swingsize;
|
return this.swingsize;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public CombatListener getCombatListener() {
|
||||||
|
return this.combatListener;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -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);
|
||||||
|
}
|
@ -5,6 +5,7 @@ import java.util.ArrayList;
|
|||||||
import com.badlogic.ashley.core.ComponentMapper;
|
import com.badlogic.ashley.core.ComponentMapper;
|
||||||
import com.badlogic.ashley.core.Entity;
|
import com.badlogic.ashley.core.Entity;
|
||||||
import com.saltosion.gladiator.components.CCombat;
|
import com.saltosion.gladiator.components.CCombat;
|
||||||
|
import com.saltosion.gladiator.systems.CombatSystem;
|
||||||
import com.saltosion.gladiator.util.AppUtil;
|
import com.saltosion.gladiator.util.AppUtil;
|
||||||
import com.saltosion.gladiator.util.Direction;
|
import com.saltosion.gladiator.util.Direction;
|
||||||
|
|
||||||
@ -27,16 +28,10 @@ public class SwingHitboxListener implements CollisionListener {
|
|||||||
|
|
||||||
CCombat otherCombat = cm.get(other);
|
CCombat otherCombat = cm.get(other);
|
||||||
if (otherCombat == null) {
|
if (otherCombat == null) {
|
||||||
System.out.println("This entity doesn't have combat!");
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
int damage = cm.get(source).getDamage();
|
int damage = cm.get(source).getDamage();
|
||||||
otherCombat.health -= damage;
|
CombatSystem.dealDamage(source, other, 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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -12,6 +12,7 @@ import com.saltosion.gladiator.components.CCombat;
|
|||||||
import com.saltosion.gladiator.components.CDestructive;
|
import com.saltosion.gladiator.components.CDestructive;
|
||||||
import com.saltosion.gladiator.components.CPhysics;
|
import com.saltosion.gladiator.components.CPhysics;
|
||||||
import com.saltosion.gladiator.components.CRenderedObject;
|
import com.saltosion.gladiator.components.CRenderedObject;
|
||||||
|
import com.saltosion.gladiator.listeners.CombatListener;
|
||||||
import com.saltosion.gladiator.listeners.SwingHitboxListener;
|
import com.saltosion.gladiator.listeners.SwingHitboxListener;
|
||||||
import com.saltosion.gladiator.util.AppUtil;
|
import com.saltosion.gladiator.util.AppUtil;
|
||||||
import com.saltosion.gladiator.util.Direction;
|
import com.saltosion.gladiator.util.Direction;
|
||||||
@ -89,4 +90,37 @@ public class CombatSystem extends EntitySystem {
|
|||||||
entities = engine.getEntitiesFor(Family.getFor(CCombat.class));
|
entities = engine.getEntitiesFor(Family.getFor(CCombat.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Deal <b>damage</b> to <b>target</b>. Source is optional, leave null if none.
|
||||||
|
* @param source Source of the <b>damage</b>.
|
||||||
|
* @param target Target to kill.
|
||||||
|
* @param damage Damage taken, that was dealth to the <b>target</b>.
|
||||||
|
*/
|
||||||
|
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 <b>target</b>.
|
||||||
|
* @param source Source of the <b/>damage</b>.
|
||||||
|
* @param target Target to kill.
|
||||||
|
* @param damage Damage taken, that killed <b>target</b>.
|
||||||
|
*/
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user