Player can now swing with arrow keys.

This commit is contained in:
Allexit 2015-05-10 20:34:55 +03:00
parent c263824405
commit 36e3214758
13 changed files with 284 additions and 26 deletions

View File

@ -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;

View File

@ -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<Direction, Boolean> inputs = new HashMap<Direction, Boolean>();
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;
}
}

View File

@ -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;
}

View File

@ -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;

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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

View File

@ -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) {

View File

@ -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<CCombat> cm = ComponentMapper.getFor(CCombat.class);
private ComponentMapper<CPhysics> pm = ComponentMapper.getFor(CPhysics.class);
private ImmutableArray<Entity> entities;
@Override
public void addedToEngine(Engine engine) {
updateEntities(engine);
}
@Override
public void update(float deltaTime) {
for (int i=0; i<entities.size(); i++) {
Entity e = entities.get(i);
CCombat combat = cm.get(e);
CPhysics obj = pm.get(e);
if (combat.swingCdCounter > 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));
}
}

View File

@ -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<CDestructive> dm = ComponentMapper.getFor(CDestructive.class);
private ImmutableArray<Entity> entities;
@Override
public void addedToEngine(Engine engine) {
updateEntities(engine);
}
@Override
public void update(float deltaTime) {
for (int i=0; i<entities.size(); i++) {
Entity e = entities.get(i);
CDestructive des = dm.get(e);
if (des.timeLeft > 0) {
des.timeLeft -= deltaTime;
} else {
AppUtil.engine.removeEntity(e);
}
}
}
public void updateEntities(Engine engine) {
entities = engine.getEntitiesFor(Family.getFor(CDestructive.class));
}
}

View File

@ -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;

View File

@ -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";
}