From 36e3214758ef97308bdbeed7b938fc623c5b459a Mon Sep 17 00:00:00 2001 From: Allexit Date: Sun, 10 May 2015 20:34:55 +0300 Subject: [PATCH] Player can now swing with arrow keys. --- .../saltosion/gladiator/GladiatorBrawler.java | 16 +++- .../gladiator/components/CCombat.java | 89 +++++++++++++----- .../gladiator/components/CDestructive.java | 13 +++ .../gladiator/components/CPhysics.java | 10 ++ .../saltosion/gladiator/input/IRMoveLeft.java | 2 +- .../gladiator/input/IRMoveRight.java | 2 +- .../saltosion/gladiator/input/IRSwing.java | 30 ++++++ .../gladiator/input/InputHandler.java | 4 + .../gladiator/input/InputReceivers.java | 5 + .../gladiator/systems/CombatSystem.java | 93 +++++++++++++++++++ .../gladiator/systems/MiscManagerSystem.java | 40 ++++++++ .../com/saltosion/gladiator/util/AppUtil.java | 2 + .../com/saltosion/gladiator/util/Name.java | 4 + 13 files changed, 284 insertions(+), 26 deletions(-) create mode 100644 core/src/com/saltosion/gladiator/components/CDestructive.java create mode 100644 core/src/com/saltosion/gladiator/input/IRSwing.java create mode 100644 core/src/com/saltosion/gladiator/systems/CombatSystem.java create mode 100644 core/src/com/saltosion/gladiator/systems/MiscManagerSystem.java diff --git a/core/src/com/saltosion/gladiator/GladiatorBrawler.java b/core/src/com/saltosion/gladiator/GladiatorBrawler.java index f53d6a1..9542e30 100644 --- a/core/src/com/saltosion/gladiator/GladiatorBrawler.java +++ b/core/src/com/saltosion/gladiator/GladiatorBrawler.java @@ -7,9 +7,12 @@ import com.badlogic.gdx.ApplicationAdapter; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.g2d.Sprite; import com.badlogic.gdx.math.Vector2; +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.systems.CombatSystem; +import com.saltosion.gladiator.systems.MiscManagerSystem; import com.saltosion.gladiator.systems.PhysicsSystem; import com.saltosion.gladiator.systems.RenderingSystem; import com.saltosion.gladiator.util.AppUtil; @@ -29,9 +32,12 @@ public class GladiatorBrawler extends ApplicationAdapter { public void create() { // Initialize the Engine engine = new Engine(); + AppUtil.engine = engine; engine.addSystem(new PhysicsSystem()); engine.addSystem(new RenderingSystem()); + engine.addSystem(new CombatSystem()); + engine.addSystem(new MiscManagerSystem()); engine.addEntityListener(new EntityListener() { @Override public void entityAdded(Entity entity) { @@ -39,6 +45,10 @@ public class GladiatorBrawler extends ApplicationAdapter { ps.updateEntities(engine); RenderingSystem rs = engine.getSystem(RenderingSystem.class); rs.updateEntities(engine); + CombatSystem cs = engine.getSystem(CombatSystem.class); + cs.updateEntities(engine); + MiscManagerSystem mms = engine.getSystem(MiscManagerSystem.class); + mms.updateEntities(engine); } @Override @@ -47,6 +57,10 @@ public class GladiatorBrawler extends ApplicationAdapter { ps.updateEntities(engine); RenderingSystem rs = engine.getSystem(RenderingSystem.class); rs.updateEntities(engine); + CombatSystem cs = engine.getSystem(CombatSystem.class); + cs.updateEntities(engine); + MiscManagerSystem mms = engine.getSystem(MiscManagerSystem.class); + mms.updateEntities(engine); } }); @@ -75,7 +89,7 @@ public class GladiatorBrawler extends ApplicationAdapter { renderedObject.playAnimation("Idle"); player.add(renderedObject); player.add(new CPhysics().setSize(2, 4).setPosition(0, 5)); - + player.add(new CCombat().setBaseDamage(100).setHealth(1000).setSwingCD(.5f)); engine.addEntity(player); AppUtil.player = player; diff --git a/core/src/com/saltosion/gladiator/components/CCombat.java b/core/src/com/saltosion/gladiator/components/CCombat.java index 22c81db..3288a60 100644 --- a/core/src/com/saltosion/gladiator/components/CCombat.java +++ b/core/src/com/saltosion/gladiator/components/CCombat.java @@ -1,19 +1,65 @@ package com.saltosion.gladiator.components; +import java.util.HashMap; + import com.badlogic.ashley.core.Component; +import com.badlogic.gdx.math.Vector2; +import com.saltosion.gladiator.util.Direction; public class CCombat extends Component { - private int health = 0; + public int health = 0; private int maxHealth = 0; private int damage = 0; + private Vector2 swingsize = new Vector2(4, 4); - public boolean swinging = false; - private float swingCdCounter = 0; + private Vector2 swinging = new Vector2(); private float swingCd = 0; + public float swingCdCounter = 0; + + public HashMap inputs = new HashMap(); - public int getHealth() { - return this.health; + public CCombat() { + this.inputs.put(Direction.UP, false); + this.inputs.put(Direction.DOWN, false); + this.inputs.put(Direction.LEFT, false); + this.inputs.put(Direction.RIGHT, false); + } + + public CCombat setBaseDamage(int basedmg) { + this.damage = basedmg; + return this; + } + + /** + * Sets max health for entity and replenishes health. + * @param health + * @return + */ + public CCombat setHealth(int health) { + this.health = health; + this.maxHealth = health; + return this; + } + + public CCombat setMaxHealth(int maxHealth) { + this.maxHealth = maxHealth; + return this; + } + + public CCombat setSwingCD(float cd) { + this.swingCd = cd; + return this; + } + + public CCombat setSwinging(Vector2 swingdir) { + this.swinging = swingdir; + return this; + } + + public CCombat setSwingSize(Vector2 swingsize) { + this.swingsize = swingsize; + return this; } public int getMaxHealth() { @@ -30,31 +76,28 @@ public class CCombat extends Component { return randomdamage; } - public float getSwingCDCounter() { - return this.swingCdCounter; - } - public float getSwingCD() { return this.swingCd; } - public CCombat setBaseDamage(int basedmg) { - this.damage = basedmg; - return this; + public Vector2 getSwing() { + return this.swinging; } - public CCombat setCurrHealth(int health) { - this.health = health; - return this; + public Direction getSwingDirection() { + if (swinging.x > 0) { + return Direction.RIGHT; + } else if (swinging.x < 0) { + return Direction.LEFT; + } else if (swinging.y > 0) { + return Direction.UP; + } else if (swinging.y < 0) { + return Direction.DOWN; + } + return null; } - public CCombat setMaxHealth(int maxHealth) { - this.maxHealth = maxHealth; - return this; - } - - public CCombat setSwingCD(float cd) { - this.swingCd = cd; - return this; + public Vector2 getSwingSize() { + return this.swingsize; } } diff --git a/core/src/com/saltosion/gladiator/components/CDestructive.java b/core/src/com/saltosion/gladiator/components/CDestructive.java new file mode 100644 index 0000000..7f5763b --- /dev/null +++ b/core/src/com/saltosion/gladiator/components/CDestructive.java @@ -0,0 +1,13 @@ +package com.saltosion.gladiator.components; + +import com.badlogic.ashley.core.Component; + +public class CDestructive extends Component { + + public CDestructive(float time) { + this.timeLeft = time; + } + + public float timeLeft = 0; + +} diff --git a/core/src/com/saltosion/gladiator/components/CPhysics.java b/core/src/com/saltosion/gladiator/components/CPhysics.java index cf6840b..51571b0 100644 --- a/core/src/com/saltosion/gladiator/components/CPhysics.java +++ b/core/src/com/saltosion/gladiator/components/CPhysics.java @@ -64,11 +64,21 @@ public class CPhysics extends Component { this.size.set(w, h); return this; } + + public CPhysics setSize(Vector2 size) { + this.size.set(size); + return this; + } public CPhysics setPosition(float x, float y) { this.position.set(x, y); return this; } + + public CPhysics setPosition(Vector2 pos) { + this.position.set(pos); + return this; + } public CPhysics setCollisionListener(CollisionListener collisionListener) { this.collisionListener = collisionListener; diff --git a/core/src/com/saltosion/gladiator/input/IRMoveLeft.java b/core/src/com/saltosion/gladiator/input/IRMoveLeft.java index 790fa7f..c81af6b 100644 --- a/core/src/com/saltosion/gladiator/input/IRMoveLeft.java +++ b/core/src/com/saltosion/gladiator/input/IRMoveLeft.java @@ -18,6 +18,6 @@ public class IRMoveLeft implements InputReceiver { public boolean released() { CPhysics physics = AppUtil.player.getComponent(CPhysics.class); physics.movingLeft = false; - return false; + return true; } } diff --git a/core/src/com/saltosion/gladiator/input/IRMoveRight.java b/core/src/com/saltosion/gladiator/input/IRMoveRight.java index 2ac1029..0bf02ff 100644 --- a/core/src/com/saltosion/gladiator/input/IRMoveRight.java +++ b/core/src/com/saltosion/gladiator/input/IRMoveRight.java @@ -18,6 +18,6 @@ public class IRMoveRight implements InputReceiver { public boolean released() { CPhysics physics = AppUtil.player.getComponent(CPhysics.class); physics.movingRight = false; - return false; + return true; } } diff --git a/core/src/com/saltosion/gladiator/input/IRSwing.java b/core/src/com/saltosion/gladiator/input/IRSwing.java new file mode 100644 index 0000000..43e6116 --- /dev/null +++ b/core/src/com/saltosion/gladiator/input/IRSwing.java @@ -0,0 +1,30 @@ +package com.saltosion.gladiator.input; + +import com.saltosion.gladiator.components.CCombat; +import com.saltosion.gladiator.util.AppUtil; +import com.saltosion.gladiator.util.Direction; +import com.saltosion.gladiator.util.Global; + +public class IRSwing implements InputReceiver { + + private Direction dir; + + public IRSwing(Direction dir) { + this.dir = dir; + } + + @Override + public boolean pressed() { + CCombat combat = AppUtil.player.getComponent(CCombat.class); + combat.inputs.put(dir, true); + return true; + } + + @Override + public boolean released() { + CCombat combat = AppUtil.player.getComponent(CCombat.class); + combat.inputs.put(dir, false); + return true; + } + +} diff --git a/core/src/com/saltosion/gladiator/input/InputHandler.java b/core/src/com/saltosion/gladiator/input/InputHandler.java index 72a683a..ef208ae 100644 --- a/core/src/com/saltosion/gladiator/input/InputHandler.java +++ b/core/src/com/saltosion/gladiator/input/InputHandler.java @@ -14,6 +14,10 @@ public class InputHandler implements InputProcessor { keys.put(Keys.A, Name.MOVE_LEFT); keys.put(Keys.D, Name.MOVE_RIGHT); keys.put(Keys.SPACE, Name.JUMP); + keys.put(Keys.LEFT, Name.SWING_LEFT); + keys.put(Keys.RIGHT, Name.SWING_RIGHT); + keys.put(Keys.UP, Name.SWING_UP); + keys.put(Keys.DOWN, Name.SWING_DOWN); } @Override diff --git a/core/src/com/saltosion/gladiator/input/InputReceivers.java b/core/src/com/saltosion/gladiator/input/InputReceivers.java index fdb0f5c..d220069 100644 --- a/core/src/com/saltosion/gladiator/input/InputReceivers.java +++ b/core/src/com/saltosion/gladiator/input/InputReceivers.java @@ -2,6 +2,7 @@ package com.saltosion.gladiator.input; import java.util.HashMap; +import com.saltosion.gladiator.util.Direction; import com.saltosion.gladiator.util.Name; public class InputReceivers { @@ -11,6 +12,10 @@ public class InputReceivers { inputreceivers.put(Name.MOVE_LEFT, new IRMoveLeft()); inputreceivers.put(Name.MOVE_RIGHT, new IRMoveRight()); inputreceivers.put(Name.JUMP, new IRJump()); + inputreceivers.put(Name.SWING_LEFT, new IRSwing(Direction.LEFT)); + inputreceivers.put(Name.SWING_RIGHT, new IRSwing(Direction.RIGHT)); + inputreceivers.put(Name.SWING_UP, new IRSwing(Direction.UP)); + inputreceivers.put(Name.SWING_DOWN, new IRSwing(Direction.DOWN)); } public static InputReceiver getReceiver(String key) { diff --git a/core/src/com/saltosion/gladiator/systems/CombatSystem.java b/core/src/com/saltosion/gladiator/systems/CombatSystem.java new file mode 100644 index 0000000..9501fad --- /dev/null +++ b/core/src/com/saltosion/gladiator/systems/CombatSystem.java @@ -0,0 +1,93 @@ +package com.saltosion.gladiator.systems; + +import com.badlogic.ashley.core.ComponentMapper; +import com.badlogic.ashley.core.Engine; +import com.badlogic.ashley.core.Entity; +import com.badlogic.ashley.core.EntitySystem; +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.components.CCombat; +import com.saltosion.gladiator.components.CDestructive; +import com.saltosion.gladiator.components.CPhysics; +import com.saltosion.gladiator.components.CRenderedObject; +import com.saltosion.gladiator.util.AppUtil; +import com.saltosion.gladiator.util.Direction; +import com.saltosion.gladiator.util.Name; +import com.saltosion.gladiator.util.SpriteLoader; + +public class CombatSystem extends EntitySystem { + + private ComponentMapper cm = ComponentMapper.getFor(CCombat.class); + private ComponentMapper pm = ComponentMapper.getFor(CPhysics.class); + private ImmutableArray entities; + + @Override + public void addedToEngine(Engine engine) { + updateEntities(engine); + } + + @Override + public void update(float deltaTime) { + for (int i=0; i 0) { + combat.swingCdCounter -= deltaTime; + } if (combat.swingCdCounter > 0) { return; } + + // Ready to swing ! + System.out.println("Ready to swing!"); + + combat.getSwing().setZero(); + if (combat.inputs.get(Direction.LEFT)) { + combat.getSwing().add(-1, 0); + } if (combat.inputs.get(Direction.RIGHT)) { + combat.getSwing().add(1, 0); + } if (combat.inputs.get(Direction.UP)) { + combat.getSwing().add(0, 1); + } if (combat.inputs.get(Direction.DOWN)) { + combat.getSwing().add(0, -1); + } + + if (!combat.getSwing().isZero() && combat.swingCdCounter <= 0) { + Vector2 pos = obj.getPosition().cpy(); + + if (combat.getSwingDirection() == Direction.LEFT) { + pos.add(-2, 0); + } else if (combat.getSwingDirection() == Direction.RIGHT) { + pos.add(2, 0); + } else if (combat.getSwingDirection() == Direction.UP) { + pos.add(0, 2); + } else if (combat.getSwingDirection() == Direction.DOWN) { + pos.add(0, -2); + } + createSwingHitbox(e, pos); + + System.out.println("Swing to " + combat.getSwingDirection().toString()); + combat.swingCdCounter = combat.getSwingCD(); + } + } + } + + public void createSwingHitbox(Entity source, Vector2 position) { + Entity e = new Entity(); + CCombat combat = cm.get(source); + Sprite s = SpriteLoader.loadSprite(Name.WALLIMG); + 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.add(new CDestructive(.1f)); + AppUtil.engine.addEntity(e); + + } + + public void updateEntities(Engine engine) { + entities = engine.getEntitiesFor(Family.getFor(CCombat.class)); + } + +} diff --git a/core/src/com/saltosion/gladiator/systems/MiscManagerSystem.java b/core/src/com/saltosion/gladiator/systems/MiscManagerSystem.java new file mode 100644 index 0000000..910eb79 --- /dev/null +++ b/core/src/com/saltosion/gladiator/systems/MiscManagerSystem.java @@ -0,0 +1,40 @@ +package com.saltosion.gladiator.systems; + +import com.badlogic.ashley.core.ComponentMapper; +import com.badlogic.ashley.core.Engine; +import com.badlogic.ashley.core.Entity; +import com.badlogic.ashley.core.EntitySystem; +import com.badlogic.ashley.core.Family; +import com.badlogic.ashley.utils.ImmutableArray; +import com.saltosion.gladiator.components.CDestructive; +import com.saltosion.gladiator.util.AppUtil; + +public class MiscManagerSystem extends EntitySystem { + + private ComponentMapper dm = ComponentMapper.getFor(CDestructive.class); + private ImmutableArray entities; + + @Override + public void addedToEngine(Engine engine) { + updateEntities(engine); + } + + @Override + public void update(float deltaTime) { + for (int i=0; i 0) { + des.timeLeft -= deltaTime; + } else { + AppUtil.engine.removeEntity(e); + } + } + } + + public void updateEntities(Engine engine) { + entities = engine.getEntitiesFor(Family.getFor(CDestructive.class)); + } + +} diff --git a/core/src/com/saltosion/gladiator/util/AppUtil.java b/core/src/com/saltosion/gladiator/util/AppUtil.java index 1bd25a3..6112ddc 100644 --- a/core/src/com/saltosion/gladiator/util/AppUtil.java +++ b/core/src/com/saltosion/gladiator/util/AppUtil.java @@ -1,11 +1,13 @@ package com.saltosion.gladiator.util; +import com.badlogic.ashley.core.Engine; import com.badlogic.ashley.core.Entity; import com.badlogic.gdx.math.Vector2; public class AppUtil { public static Entity player; + public static Engine engine; public static final int VPHEIGHT_CONST = 24; diff --git a/core/src/com/saltosion/gladiator/util/Name.java b/core/src/com/saltosion/gladiator/util/Name.java index 11892db..5921a05 100644 --- a/core/src/com/saltosion/gladiator/util/Name.java +++ b/core/src/com/saltosion/gladiator/util/Name.java @@ -12,5 +12,9 @@ public class Name { public static final String MOVE_LEFT = "MOVE_LEFT"; public static final String MOVE_RIGHT = "MOVE_RIGHT"; public static final String JUMP = "JUMP"; + public static final String SWING_LEFT = "SWING_LEFT"; + public static final String SWING_RIGHT = "SWING_RIGHT"; + public static final String SWING_UP = "SWING_UP"; + public static final String SWING_DOWN = "SWING_DOWN"; }