From 4f06d6af740de471faec235e76351720476923ed Mon Sep 17 00:00:00 2001 From: Allexit Date: Sun, 10 May 2015 22:05:46 +0300 Subject: [PATCH 1/3] Hitbox is now more appropriate in size, to be changed later --- core/assets/sprites/swinghitbox.png | Bin 0 -> 2374 bytes .../saltosion/gladiator/components/CCombat.java | 2 +- .../gladiator/systems/CombatSystem.java | 10 +++++----- core/src/com/saltosion/gladiator/util/Name.java | 1 + .../saltosion/gladiator/util/SpriteLoader.java | 1 + 5 files changed, 8 insertions(+), 6 deletions(-) create mode 100644 core/assets/sprites/swinghitbox.png diff --git a/core/assets/sprites/swinghitbox.png b/core/assets/sprites/swinghitbox.png new file mode 100644 index 0000000000000000000000000000000000000000..59e08fd6e24628d0cca26df2ef2c8b0d647aab11 GIT binary patch literal 2374 zcmV-M3Ay%(P)?VkVu03B&m zSad^gZEa<4bN~PV002XBWnpw>WFU8GbZ8()Nlj2>E@cM*00^o{L_t(&-tAd?P?Xmd z|K0oTvWo=PDY}h-ilU2lK%8iXX0kRw)PzxNq@p2{#7WeV`kTOBhsRny5fDn65l zPGW2&Mp|v~NhE@%){0Y5G9o^rJS~bYBD>4(ckk&R%eLzlDH>A^?wR>!zB%{Yz4!jk zx#yhyeGH&PJjTNDn2R3(KLCCJ`~W;uAwm%miXJHdmMs%QYHMk^%|>}H7aeMCr5zV8 z&@bP4N5piu2w=W_z$c$b^DD2gWt=nC;RJ@HZhZwNOki(v&e+wfbaK%mF(WKYeK8?H zq<;FTeEZrpD)06=phpjc1Hfyq$=_Kl%Eg8TjkCB|vA+B=KVmepEN|OBebfM#iw+4v zhfO9m(4)6nX)_UutXBHaqcfS*Kq2U`%S8wK^icyk%K;u;WTdJ)f1a}2+Ni+cpxtF< zG=njDM`TW&D#B`OX@%2C`y3A1U0F$4k&&vdtGVUB0I=Ip4*<)Tv+-uLFmyJ#p+V!c zT2aY4V;LDd8URL)RD*+qF%STPgAo-MCxQTA=1gwloH45vl?@FVXIH#aQlk87&>)r^ z5WsrqbS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^4)|4k(!VoQVR;C#pxuQ(a45>{yG2Ii4#i8 zwrykqfaGL0RjU!zp#)t0>H3gZkvn@{`bmC`b%&y>$h_!8=aTOMh6G8 zzGY?jV|KQ3=ANp+-Lm?FE^BMo$}`?RShr4=zy3N$U?BPcKu{1O0Kj6Qhp4+$c%!dMi)9I$8ROm*&ZnS>}81$vBfq4P3QK4xcp1 zO=e!Ye zfD(;H*Vx8Jd7ADbjg9gjp`orZNlEHNheKM4h;nnqV$V9ec8TRgM0UHpX*R1bb#$ih zECOcDlIv?~sHClpT0GamR}vG&%Jb)GpVLV#E*ClK>gcQK)8)JqCzKYz{b3{~Ml3vb zOm1!`X`QM{ZOzSe>DV#(;n1OCQB#vf{pch93!RP`u3x8$>C^eY6J}Z(if-JXbM04Q so}Uo${}=EB;0M4DfFA%q0C&j00JJ;0!<1@wsQ>@~07*qoM6N<$f^E!g!~g&Q literal 0 HcmV?d00001 diff --git a/core/src/com/saltosion/gladiator/components/CCombat.java b/core/src/com/saltosion/gladiator/components/CCombat.java index 3288a60..0274eac 100644 --- a/core/src/com/saltosion/gladiator/components/CCombat.java +++ b/core/src/com/saltosion/gladiator/components/CCombat.java @@ -11,7 +11,7 @@ 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 Vector2 swinging = new Vector2(); private float swingCd = 0; diff --git a/core/src/com/saltosion/gladiator/systems/CombatSystem.java b/core/src/com/saltosion/gladiator/systems/CombatSystem.java index 6dcfef2..f9052a6 100644 --- a/core/src/com/saltosion/gladiator/systems/CombatSystem.java +++ b/core/src/com/saltosion/gladiator/systems/CombatSystem.java @@ -57,13 +57,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 +75,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())); diff --git a/core/src/com/saltosion/gladiator/util/Name.java b/core/src/com/saltosion/gladiator/util/Name.java index 5921a05..4643e4c 100644 --- a/core/src/com/saltosion/gladiator/util/Name.java +++ b/core/src/com/saltosion/gladiator/util/Name.java @@ -8,6 +8,7 @@ public class Name { 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"); } /** From c7588629064ff1f507cc04e54a4f7743f39f2c6f Mon Sep 17 00:00:00 2001 From: Allexit Date: Sun, 10 May 2015 22:26:12 +0300 Subject: [PATCH 2/3] renamed listeners package, added test dummy to kill --- .../com/saltosion/gladiator/GladiatorBrawler.java | 15 +++++++++++++++ .../saltosion/gladiator/components/CPhysics.java | 2 +- .../CollisionListener.java | 2 +- .../SwingHitboxListener.java | 9 +++++++-- .../saltosion/gladiator/systems/CombatSystem.java | 2 +- 5 files changed, 25 insertions(+), 5 deletions(-) rename core/src/com/saltosion/gladiator/{collisionlisteners => listeners}/CollisionListener.java (91%) rename core/src/com/saltosion/gladiator/{collisionlisteners => listeners}/SwingHitboxListener.java (76%) diff --git a/core/src/com/saltosion/gladiator/GladiatorBrawler.java b/core/src/com/saltosion/gladiator/GladiatorBrawler.java index 9f9bab5..e0e10a8 100644 --- a/core/src/com/saltosion/gladiator/GladiatorBrawler.java +++ b/core/src/com/saltosion/gladiator/GladiatorBrawler.java @@ -66,6 +66,7 @@ public class GladiatorBrawler extends ApplicationAdapter { // Initialize stuff in the world initializePlayer(); + initializeTestDummy(); initializeLevel(); // Initialize input @@ -94,6 +95,20 @@ 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)); + engine.addEntity(dummy); + } public void initializeLevel() { Entity ground = new Entity(); 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/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..e987803 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.util.AppUtil; import com.saltosion.gladiator.util.Direction; public class SwingHitboxListener implements CollisionListener { @@ -31,7 +32,11 @@ public class SwingHitboxListener implements CollisionListener { } int damage = cm.get(source).getDamage(); otherCombat.health -= damage; - System.out.println("Entity was hit for " + damage + " 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); + } } } diff --git a/core/src/com/saltosion/gladiator/systems/CombatSystem.java b/core/src/com/saltosion/gladiator/systems/CombatSystem.java index f9052a6..e89802c 100644 --- a/core/src/com/saltosion/gladiator/systems/CombatSystem.java +++ b/core/src/com/saltosion/gladiator/systems/CombatSystem.java @@ -8,11 +8,11 @@ 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.SwingHitboxListener; import com.saltosion.gladiator.util.AppUtil; import com.saltosion.gladiator.util.Direction; import com.saltosion.gladiator.util.Name; From 6d00222adb3079495d947b77ac53c4db65389be4 Mon Sep 17 00:00:00 2001 From: Allexit Date: Sun, 10 May 2015 23:01:38 +0300 Subject: [PATCH 3/3] Created CombatListeners, good for therapy after combat --- .../saltosion/gladiator/GladiatorBrawler.java | 15 +++++++- .../gladiator/components/CCombat.java | 11 ++++++ .../gladiator/listeners/CombatListener.java | 20 +++++++++++ .../listeners/SwingHitboxListener.java | 11 ++---- .../gladiator/systems/CombatSystem.java | 34 +++++++++++++++++++ 5 files changed, 82 insertions(+), 9 deletions(-) create mode 100644 core/src/com/saltosion/gladiator/listeners/CombatListener.java 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); + } }