Merge branch 'attacking_system'

Conflicts:
	core/src/com/saltosion/gladiator/util/Name.java
This commit is contained in:
Allexit 2015-05-10 23:08:19 +03:00
commit 82dba2bb39
10 changed files with 109 additions and 16 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.3 KiB

View File

@ -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;
@ -66,6 +67,7 @@ public class GladiatorBrawler extends ApplicationAdapter {
// Initialize stuff in the world // Initialize stuff in the world
initializePlayer(); initializePlayer();
initializeTestDummy();
initializeLevel(); initializeLevel();
// Initialize input // Initialize input
@ -94,6 +96,32 @@ public class GladiatorBrawler extends ApplicationAdapter {
AppUtil.player = player; 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() { public void initializeLevel() {
Entity ground = new Entity(); Entity ground = new Entity();

View File

@ -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 {
@ -11,7 +12,8 @@ public class CCombat extends Component {
public int health = 0; public int health = 0;
private int maxHealth = 0; private int maxHealth = 0;
private int damage = 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 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;
}
} }

View File

@ -2,7 +2,7 @@ package com.saltosion.gladiator.components;
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.collisionlisteners.CollisionListener; import com.saltosion.gladiator.listeners.CollisionListener;
public class CPhysics extends Component { public class CPhysics extends Component {

View File

@ -1,4 +1,4 @@
package com.saltosion.gladiator.collisionlisteners; package com.saltosion.gladiator.listeners;
import com.badlogic.ashley.core.Entity; import com.badlogic.ashley.core.Entity;
import com.saltosion.gladiator.util.Direction; import com.saltosion.gladiator.util.Direction;

View File

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

View File

@ -1,10 +1,11 @@
package com.saltosion.gladiator.collisionlisteners; package com.saltosion.gladiator.listeners;
import java.util.ArrayList; 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.Direction; import com.saltosion.gladiator.util.Direction;
public class SwingHitboxListener implements CollisionListener { public class SwingHitboxListener implements CollisionListener {
@ -26,12 +27,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("Entity was hit for " + damage + " damage!");
} }
} }

View File

@ -8,11 +8,12 @@ import com.badlogic.ashley.core.Family;
import com.badlogic.ashley.utils.ImmutableArray; import com.badlogic.ashley.utils.ImmutableArray;
import com.badlogic.gdx.graphics.g2d.Sprite; import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.math.Vector2;
import com.saltosion.gladiator.collisionlisteners.SwingHitboxListener;
import com.saltosion.gladiator.components.CCombat; 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.util.AppUtil; import com.saltosion.gladiator.util.AppUtil;
import com.saltosion.gladiator.util.Direction; import com.saltosion.gladiator.util.Direction;
import com.saltosion.gladiator.util.Name; import com.saltosion.gladiator.util.Name;
@ -57,13 +58,13 @@ public class CombatSystem extends EntitySystem {
Vector2 pos = obj.getPosition().cpy(); Vector2 pos = obj.getPosition().cpy();
if (combat.getSwingDirection() == Direction.LEFT) { if (combat.getSwingDirection() == Direction.LEFT) {
pos.add(-2, 0); pos.add(-combat.getSwingSize().x/2, 0);
} else if (combat.getSwingDirection() == Direction.RIGHT) { } else if (combat.getSwingDirection() == Direction.RIGHT) {
pos.add(2, 0); pos.add(combat.getSwingSize().x/2, 0);
} else if (combat.getSwingDirection() == Direction.UP) { } else if (combat.getSwingDirection() == Direction.UP) {
pos.add(0, 2); pos.add(0, combat.getSwingSize().y);
} else if (combat.getSwingDirection() == Direction.DOWN) { } else if (combat.getSwingDirection() == Direction.DOWN) {
pos.add(0, -2); pos.add(0, -combat.getSwingSize().y/3*2);
} }
createSwingHitbox(e, pos); createSwingHitbox(e, pos);
@ -75,7 +76,7 @@ public class CombatSystem extends EntitySystem {
public void createSwingHitbox(Entity source, Vector2 position) { public void createSwingHitbox(Entity source, Vector2 position) {
Entity e = new Entity(); Entity e = new Entity();
CCombat combat = cm.get(source); 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 CRenderedObject(s));
e.add(new CPhysics().setGhost(true).setGravityApplied(false).setMovable(false) e.add(new CPhysics().setGhost(true).setGravityApplied(false).setMovable(false)
.setSize(combat.getSwingSize())); .setSize(combat.getSwingSize()));
@ -88,5 +89,38 @@ public class CombatSystem extends EntitySystem {
public void updateEntities(Engine engine) { public void updateEntities(Engine engine) {
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);
}
} }

View File

@ -3,13 +3,13 @@ package com.saltosion.gladiator.util;
public class Name { public class Name {
public static final String GAME_NAME = "Gladiator Brawl"; public static final String GAME_NAME = "Gladiator Brawl";
public static final String DEBUG = "DEBUG"; public static final String DEBUG = "DEBUG";
public static final String STATICPLAYER = "STATICPLAYER"; public static final String STATICPLAYER = "STATICPLAYER";
public static final String PLAYERIMG = "PLAYERIMG"; public static final String PLAYERIMG = "PLAYERIMG";
public static final String GROUNDIMG = "GROUNDIMG"; public static final String GROUNDIMG = "GROUNDIMG";
public static final String WALLIMG = "WALLIMG"; 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_LEFT = "MOVE_LEFT";
public static final String MOVE_RIGHT = "MOVE_RIGHT"; public static final String MOVE_RIGHT = "MOVE_RIGHT";

View File

@ -16,6 +16,7 @@ public class SpriteLoader {
loadTexture(Name.PLAYERIMG, "sprites/player/player.png"); loadTexture(Name.PLAYERIMG, "sprites/player/player.png");
loadTexture(Name.GROUNDIMG, "sprites/ground.png"); loadTexture(Name.GROUNDIMG, "sprites/ground.png");
loadTexture(Name.WALLIMG, "sprites/wall.png"); loadTexture(Name.WALLIMG, "sprites/wall.png");
loadTexture(Name.SWINGHITBOXIMG, "sprites/swinghitbox.png");
} }
/** /**