diff --git a/core/assets/sprites/swinghitbox.png b/core/assets/sprites/swinghitbox.png new file mode 100644 index 0000000..59e08fd Binary files /dev/null and b/core/assets/sprites/swinghitbox.png differ diff --git a/core/src/com/saltosion/gladiator/GladiatorBrawler.java b/core/src/com/saltosion/gladiator/GladiatorBrawler.java index 9f9bab5..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; @@ -66,6 +67,7 @@ public class GladiatorBrawler extends ApplicationAdapter { // Initialize stuff in the world initializePlayer(); + initializeTestDummy(); initializeLevel(); // Initialize input @@ -94,6 +96,32 @@ public class GladiatorBrawler extends ApplicationAdapter { AppUtil.player = player; } + + public void initializeTestDummy() { + Entity dummy = new Entity(); + CRenderedObject renderedObject = new CRenderedObject(); + Sprite player1 = SpriteLoader.loadSprite(Name.PLAYERIMG, 0, 0, 64, 64); + Sprite player2 = SpriteLoader.loadSprite(Name.PLAYERIMG, 1, 0, 64, 64); + SpriteSequence sequence = new SpriteSequence(1).addSprite(player1).addSprite(player2); + renderedObject.addSequence("Idle", sequence); + 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).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); + } public void initializeLevel() { Entity ground = new Entity(); diff --git a/core/src/com/saltosion/gladiator/components/CCombat.java b/core/src/com/saltosion/gladiator/components/CCombat.java index 3288a60..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 { @@ -11,7 +12,8 @@ public class CCombat extends Component { public int health = 0; private int maxHealth = 0; private int damage = 0; - private Vector2 swingsize = new Vector2(4, 4); + 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/components/CPhysics.java b/core/src/com/saltosion/gladiator/components/CPhysics.java index 4270793..0279400 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.collisionlisteners.CollisionListener; +import com.saltosion.gladiator.listeners.CollisionListener; public class CPhysics extends Component { diff --git a/core/src/com/saltosion/gladiator/collisionlisteners/CollisionListener.java b/core/src/com/saltosion/gladiator/listeners/CollisionListener.java similarity index 91% rename from core/src/com/saltosion/gladiator/collisionlisteners/CollisionListener.java rename to core/src/com/saltosion/gladiator/listeners/CollisionListener.java index 7364acc..ce747e9 100644 --- a/core/src/com/saltosion/gladiator/collisionlisteners/CollisionListener.java +++ b/core/src/com/saltosion/gladiator/listeners/CollisionListener.java @@ -1,4 +1,4 @@ -package com.saltosion.gladiator.collisionlisteners; +package com.saltosion.gladiator.listeners; import com.badlogic.ashley.core.Entity; import com.saltosion.gladiator.util.Direction; 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/collisionlisteners/SwingHitboxListener.java b/core/src/com/saltosion/gladiator/listeners/SwingHitboxListener.java similarity index 76% rename from core/src/com/saltosion/gladiator/collisionlisteners/SwingHitboxListener.java rename to core/src/com/saltosion/gladiator/listeners/SwingHitboxListener.java index 43c07ce..b6c7d1f 100644 --- a/core/src/com/saltosion/gladiator/collisionlisteners/SwingHitboxListener.java +++ b/core/src/com/saltosion/gladiator/listeners/SwingHitboxListener.java @@ -1,10 +1,11 @@ -package com.saltosion.gladiator.collisionlisteners; +package com.saltosion.gladiator.listeners; 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.Direction; public class SwingHitboxListener implements CollisionListener { @@ -26,12 +27,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("Entity was hit for " + damage + " damage!"); + 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 6dcfef2..b488763 100644 --- a/core/src/com/saltosion/gladiator/systems/CombatSystem.java +++ b/core/src/com/saltosion/gladiator/systems/CombatSystem.java @@ -8,11 +8,12 @@ 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; 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; import com.saltosion.gladiator.util.Name; @@ -57,13 +58,13 @@ public class CombatSystem extends EntitySystem { Vector2 pos = obj.getPosition().cpy(); if (combat.getSwingDirection() == Direction.LEFT) { - pos.add(-2, 0); + pos.add(-combat.getSwingSize().x/2, 0); } else if (combat.getSwingDirection() == Direction.RIGHT) { - pos.add(2, 0); + pos.add(combat.getSwingSize().x/2, 0); } else if (combat.getSwingDirection() == Direction.UP) { - pos.add(0, 2); + pos.add(0, combat.getSwingSize().y); } else if (combat.getSwingDirection() == Direction.DOWN) { - pos.add(0, -2); + pos.add(0, -combat.getSwingSize().y/3*2); } createSwingHitbox(e, pos); @@ -75,7 +76,7 @@ public class CombatSystem extends EntitySystem { public void createSwingHitbox(Entity source, Vector2 position) { Entity e = new Entity(); CCombat combat = cm.get(source); - Sprite s = SpriteLoader.loadSprite(Name.WALLIMG); + Sprite s = SpriteLoader.loadSprite(Name.SWINGHITBOXIMG); e.add(new CRenderedObject(s)); e.add(new CPhysics().setGhost(true).setGravityApplied(false).setMovable(false) .setSize(combat.getSwingSize())); @@ -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); + } } diff --git a/core/src/com/saltosion/gladiator/util/Name.java b/core/src/com/saltosion/gladiator/util/Name.java index c1c3423..1ee162e 100644 --- a/core/src/com/saltosion/gladiator/util/Name.java +++ b/core/src/com/saltosion/gladiator/util/Name.java @@ -3,13 +3,13 @@ package com.saltosion.gladiator.util; public class Name { public static final String GAME_NAME = "Gladiator Brawl"; - public static final String DEBUG = "DEBUG"; - + public static final String STATICPLAYER = "STATICPLAYER"; public static final String PLAYERIMG = "PLAYERIMG"; public static final String GROUNDIMG = "GROUNDIMG"; public static final String WALLIMG = "WALLIMG"; + public static final String SWINGHITBOXIMG = "SWINGHITBOXIMG"; public static final String MOVE_LEFT = "MOVE_LEFT"; public static final String MOVE_RIGHT = "MOVE_RIGHT"; diff --git a/core/src/com/saltosion/gladiator/util/SpriteLoader.java b/core/src/com/saltosion/gladiator/util/SpriteLoader.java index c0f89c3..a4efa07 100644 --- a/core/src/com/saltosion/gladiator/util/SpriteLoader.java +++ b/core/src/com/saltosion/gladiator/util/SpriteLoader.java @@ -16,6 +16,7 @@ public class SpriteLoader { loadTexture(Name.PLAYERIMG, "sprites/player/player.png"); loadTexture(Name.GROUNDIMG, "sprites/ground.png"); loadTexture(Name.WALLIMG, "sprites/wall.png"); + loadTexture(Name.SWINGHITBOXIMG, "sprites/swinghitbox.png"); } /**