Merge branch 'animations'

This commit is contained in:
Jeasonfire 2015-05-16 21:29:13 +03:00
commit 27457bc102
5 changed files with 245 additions and 83 deletions

View File

@ -9,7 +9,7 @@ public class CPhysics extends Component {
private final Vector2 position = new Vector2();
private final Vector2 velocity = new Vector2();
private final Vector2 size = new Vector2();
private float movespeed = 7.5f, jumpForce = 35f, gravity = 100f;
private float movespeed = 15f, jumpForce = 35f, gravity = 100f;
private CollisionListener collisionListener = null;
private boolean movable = true;
@ -23,6 +23,9 @@ public class CPhysics extends Component {
public boolean movingRight = false;
public boolean jumping = false;
// Stores information about the direction last time moved in
public boolean movedLeftLast = false;
/**
* @param movable Toggles if the entity can move by itself
* @return Returns the instance this methdod was called from

View File

@ -6,13 +6,15 @@ import java.util.Set;
import com.badlogic.ashley.core.Component;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.saltosion.gladiator.util.Log;
import com.saltosion.gladiator.util.SpriteSequence;
public class CRenderedObject extends Component {
private HashMap<String, SpriteSequence> spritesequences = new HashMap<String, SpriteSequence>();
private ArrayList<String> channels = new ArrayList<String>();
private HashMap<String, String> currentSequences = new HashMap<String, String>();
private HashMap<String, Float> currentFrames = new HashMap<String, Float>();
private final HashMap<String, SpriteSequence> spritesequences = new HashMap<String, SpriteSequence>();
private final ArrayList<String> channels = new ArrayList<String>();
private final HashMap<String, String> currentSequences = new HashMap<String, String>();
private final HashMap<String, Float> currentFrames = new HashMap<String, Float>();
public CRenderedObject() {
addChannel("default");
@ -20,6 +22,7 @@ public class CRenderedObject extends Component {
/**
* Can be used if the Rendered Object is a single static image always.
*
* @param sprite
*/
public CRenderedObject(Sprite sprite) {
@ -33,6 +36,7 @@ public class CRenderedObject extends Component {
/**
* Sets sequence for channelName
*
* @param channelName
* @param sequence
*/
@ -42,6 +46,7 @@ public class CRenderedObject extends Component {
/**
* Sets sequence for "default" channel.
*
* @param sequence
*/
public void setCurrentSequence(String sequence) {
@ -50,6 +55,7 @@ public class CRenderedObject extends Component {
/**
* Sets frame for channelName
*
* @param channelName
* @param frame
*/
@ -59,6 +65,7 @@ public class CRenderedObject extends Component {
/**
* Sets frame for "default" channel.
*
* @param frame
*/
public void setCurrentFrame(float frame) {
@ -71,6 +78,7 @@ public class CRenderedObject extends Component {
/**
* Plays animation on the "default" channel, starting in frame 0
*
* @param key
*/
public void playAnimation(String key) {
@ -78,33 +86,18 @@ public class CRenderedObject extends Component {
}
/**
* Plays animation on channelName
* @param channelName
* Plays animation on the "default" channel.
*
* @param key animation name
* @param startingframe
*/
public void playAnimation(String channelName, String key, int startingframe) {
if (spritesequences.containsKey(key) && channels.contains(channelName)) {
setCurrentSequence(channelName, key);
setCurrentFrame(channelName, startingframe);
return;
}
String[] s = (String[]) spritesequences.keySet().toArray();
setCurrentSequence(channels.get(0), s[0]);
setCurrentFrame(channels.get(0), 0f);
}
/**
* Plays animation on the "default" channel.
* @param key animation name
* @param startingFrame
*/
public void playAnimation(String key, int startingframe) {
playAnimation("default", key, startingframe);
}
/**
* Plays animation on "channelName" starting on frame 0
*
* @param channelName channel name
* @param key animation name
*/
@ -112,7 +105,33 @@ public class CRenderedObject extends Component {
playAnimation(channelName, key, 0);
}
public void addChannel(String channelName) {
/**
* Plays animation on channelName
*
* @param channelName
* @param key animation name
* @param startingframe
*/
public void playAnimation(String channelName, String key, int startingframe) {
if (getCurrentSequence(channelName).equals(key)) {
return;
}
if (spritesequences.containsKey(key) && channels.contains(channelName)) {
setCurrentSequence(channelName, key);
setCurrentFrame(channelName, startingframe);
return;
}
Log.info("Channel: " + channelName);
Log.info("Key: " + key);
String[] s = (String[]) spritesequences.keySet().toArray();
setCurrentSequence(channels.get(0), s[0]);
setCurrentFrame(channels.get(0), 0f);
}
public final void addChannel(String channelName) {
Set<String> sequences = spritesequences.keySet();
String sequence = "";
if (sequences.size() > 0) {
@ -125,9 +144,11 @@ public class CRenderedObject extends Component {
/**
* Sets channel name
*
* @param oldName Old name of the channel
* @param newName New name of the channel to be set
* @return <b>boolean</b>, false if channel with oldName doesn't exist or a channel with newName already exists.
* @return <b>boolean</b>, false if channel with oldName doesn't exist or a
* channel with newName already exists.
*/
public boolean setChannelName(String oldName, String newName) {
if (!this.channels.contains(oldName) || this.channels.contains(newName)) {
@ -146,6 +167,7 @@ public class CRenderedObject extends Component {
/**
* Returns the current frame on "default" channel.
*
* @return
*/
public float getCurrentFrame() {
@ -154,6 +176,7 @@ public class CRenderedObject extends Component {
/**
* Returns the frame on "channel".
*
* @param channel
* @return
*/
@ -167,6 +190,7 @@ public class CRenderedObject extends Component {
/**
* Returns the current sequence on "channel"
*
* @param channel
* @return
*/

View File

@ -11,55 +11,40 @@ import com.saltosion.gladiator.listeners.CombatListener;
import com.saltosion.gladiator.listeners.ai.DummyAI;
import com.saltosion.gladiator.util.AppUtil;
import com.saltosion.gladiator.util.Direction;
import com.saltosion.gladiator.util.Log;
import com.saltosion.gladiator.util.Name;
import com.saltosion.gladiator.util.SpriteLoader;
import com.saltosion.gladiator.util.SpriteSequence;
public class EntityFactory {
private final static int IDLE_ANIMATION_SPEED = 1, RUN_ANIMATION_SPEED = 15, SWING_ANIMATION_SPEED = 10;
public void createPlayer(Vector2 pos) {
Entity player = new Entity();
CRenderedObject renderedObject = new CRenderedObject();
Sprite playertorso1 = SpriteLoader.loadSprite(Name.PLAYERIMG, 0, 0, 128, 112);
Sprite playertorso2 = SpriteLoader.loadSprite(Name.PLAYERIMG, 0, 1, 128, 112);
Sprite playerlegs1 = SpriteLoader.loadSprite(Name.PLAYERIMG, 1, 0, 128, 112);
Sprite playerlegs2 = SpriteLoader.loadSprite(Name.PLAYERIMG, 1, 1, 128, 112);
SpriteSequence torsosequence = new SpriteSequence(1).addSprite(playertorso1).addSprite(playertorso2);
SpriteSequence legsequence = new SpriteSequence(1).addSprite(playerlegs1).addSprite(playerlegs2);
renderedObject.setChannelName("default", "torso");
renderedObject.addChannel("legs");
renderedObject.addSequence("Torso-Idle", torsosequence);
renderedObject.addSequence("Legs-Idle", legsequence);
renderedObject.playAnimation("torso", "Torso-Idle");
renderedObject.playAnimation("legs", "Legs-Idle");
player.add(renderedObject);
player.add(new CPhysics().setSize(2, 4).setPosition(pos.x, pos.y));
player.add(new CCombat().setBaseDamage(100).setHealth(1000));
AppUtil.engine.addEntity(player);
// Graphics
player.add(createPlayerRenderedObject());
Log.info("Created player!");
// Physics
player.add(new CPhysics().setSize(1.5f, 3.299f).setPosition(pos.x, pos.y));
// Combat
player.add(new CCombat().setBaseDamage(100).setHealth(1000));
AppUtil.engine.addEntity(player);
AppUtil.player = player;
}
public void createDummy(Vector2 pos) {
Entity dummy = new Entity();
CRenderedObject renderedObject = new CRenderedObject();
Sprite playertorso1 = SpriteLoader.loadSprite(Name.PLAYERIMG, 0, 0, 128, 112);
Sprite playertorso2 = SpriteLoader.loadSprite(Name.PLAYERIMG, 0, 1, 128, 112);
Sprite playerlegs1 = SpriteLoader.loadSprite(Name.PLAYERIMG, 1, 0, 128, 112);
Sprite playerlegs2 = SpriteLoader.loadSprite(Name.PLAYERIMG, 1, 1, 128, 112);
SpriteSequence torsosequence = new SpriteSequence(1).addSprite(playertorso1).addSprite(playertorso2);
SpriteSequence legsequence = new SpriteSequence(1).addSprite(playerlegs1).addSprite(playerlegs2);
renderedObject.setChannelName("default", "torso");
renderedObject.addChannel("legs");
renderedObject.addSequence("Torso-Idle", torsosequence);
renderedObject.addSequence("Legs-Idle", legsequence);
renderedObject.playAnimation("torso", "Torso-Idle");
renderedObject.playAnimation("legs", "Legs-Idle");
dummy.add(renderedObject);
dummy.add(new CPhysics().setSize(2, 4).setPosition(pos.x, pos.y));
// Graphics
dummy.add(createPlayerRenderedObject());
// Physics
dummy.add(new CPhysics().setSize(1.5f, 3.299f).setPosition(pos.x, pos.y));
// Combat
dummy.add(new CCombat().setBaseDamage(100).setHealth(1000).setSwingCD(.5f).setCombatListener(
new CombatListener() {
@Override
@ -74,8 +59,85 @@ public class EntityFactory {
}));
dummy.add(new CAI().setReactDistance(5).setAIListener(new DummyAI()));
AppUtil.engine.addEntity(dummy);
dummy.getComponent(CCombat.class).inputs.put(Direction.UP, true);
}
private CRenderedObject createPlayerRenderedObject() {
CRenderedObject renderedObject = new CRenderedObject();
// Sprite[x][y][flip]
Sprite[][][] playerSprites = new Sprite[2][19][2];
for (int x = 0; x < 2; x++) {
for (int y = 0; y < 19; y++) {
Sprite noFlip = SpriteLoader.loadSprite(Name.PLAYERIMG, x, y, 128, 112);
Sprite flip = new Sprite(noFlip);
flip.flip(true, false);
playerSprites[x][y][0] = noFlip;
playerSprites[x][y][1] = flip;
}
}
renderedObject.setChannelName("default", "legs");
renderedObject.addChannel("torso");
// Idle animations
SpriteSequence torsoIdleRightSequence = new SpriteSequence(IDLE_ANIMATION_SPEED).addSprite(playerSprites[0][1][0]);
renderedObject.addSequence("Torso-Idle-Right", torsoIdleRightSequence);
SpriteSequence legsIdleRightSquence = new SpriteSequence(IDLE_ANIMATION_SPEED).addSprite(playerSprites[1][0][0]).addSprite(playerSprites[1][1][0]);
renderedObject.addSequence("Legs-Idle-Right", legsIdleRightSquence);
SpriteSequence torsoIdleLeftSequence = new SpriteSequence(IDLE_ANIMATION_SPEED).addSprite(playerSprites[0][1][1]);
renderedObject.addSequence("Torso-Idle-Left", torsoIdleLeftSequence);
SpriteSequence legsIdleLeftSquence = new SpriteSequence(IDLE_ANIMATION_SPEED).addSprite(playerSprites[1][0][1]).addSprite(playerSprites[1][1][1]);
renderedObject.addSequence("Legs-Idle-Left", legsIdleLeftSquence);
// Running animations
SpriteSequence torsoRunRightSequence = new SpriteSequence(RUN_ANIMATION_SPEED).addSprite(playerSprites[0][2][0])
.addSprite(playerSprites[0][3][0]).addSprite(playerSprites[0][4][0]).addSprite(playerSprites[0][5][0])
.addSprite(playerSprites[0][6][0]).addSprite(playerSprites[0][5][0]).addSprite(playerSprites[0][4][0])
.addSprite(playerSprites[0][3][0]);
renderedObject.addSequence("Torso-Run-Right", torsoRunRightSequence);
SpriteSequence legsRunRightSequence = new SpriteSequence(RUN_ANIMATION_SPEED).addSprite(playerSprites[1][2][0])
.addSprite(playerSprites[1][3][0]).addSprite(playerSprites[1][4][0]).addSprite(playerSprites[1][5][0])
.addSprite(playerSprites[1][6][0]).addSprite(playerSprites[1][7][0]).addSprite(playerSprites[1][8][0])
.addSprite(playerSprites[1][9][0]);
renderedObject.addSequence("Legs-Run-Right", legsRunRightSequence);
SpriteSequence torsoRunLeftSequence = new SpriteSequence(RUN_ANIMATION_SPEED).addSprite(playerSprites[0][2][1])
.addSprite(playerSprites[0][3][1]).addSprite(playerSprites[0][4][1]).addSprite(playerSprites[0][5][1])
.addSprite(playerSprites[0][6][1]).addSprite(playerSprites[0][5][1]).addSprite(playerSprites[0][4][1])
.addSprite(playerSprites[0][3][1]);
renderedObject.addSequence("Torso-Run-Left", torsoRunLeftSequence);
SpriteSequence legsRunLeftSequence = new SpriteSequence(RUN_ANIMATION_SPEED).addSprite(playerSprites[1][2][1])
.addSprite(playerSprites[1][3][1]).addSprite(playerSprites[1][4][1]).addSprite(playerSprites[1][5][1])
.addSprite(playerSprites[1][6][1]).addSprite(playerSprites[1][7][1]).addSprite(playerSprites[1][8][1])
.addSprite(playerSprites[1][9][1]);
renderedObject.addSequence("Legs-Run-Left", legsRunLeftSequence);
// Combat animations
SpriteSequence torsoCombatRightSequence = new SpriteSequence(SWING_ANIMATION_SPEED).addSprite(playerSprites[0][7][0])
.addSprite(playerSprites[0][8][0]).addSprite(playerSprites[0][9][0]).addSprite(playerSprites[0][10][0]);
renderedObject.addSequence("Torso-Combat-Right", torsoCombatRightSequence);
SpriteSequence torsoCombatLeftSequence = new SpriteSequence(SWING_ANIMATION_SPEED).addSprite(playerSprites[0][7][1])
.addSprite(playerSprites[0][8][1]).addSprite(playerSprites[0][9][1]).addSprite(playerSprites[0][10][1]);
renderedObject.addSequence("Torso-Combat-Left", torsoCombatLeftSequence);
SpriteSequence torsoCombatRightDownSequence = new SpriteSequence(SWING_ANIMATION_SPEED).addSprite(playerSprites[0][11][0])
.addSprite(playerSprites[0][12][0]).addSprite(playerSprites[0][13][0]).addSprite(playerSprites[0][14][0]);
renderedObject.addSequence("Torso-Combat-Right-Down", torsoCombatRightDownSequence);
SpriteSequence torsoCombatRightUpSequence = new SpriteSequence(SWING_ANIMATION_SPEED).addSprite(playerSprites[0][15][0])
.addSprite(playerSprites[0][16][0]).addSprite(playerSprites[0][17][0]).addSprite(playerSprites[0][18][0]);
renderedObject.addSequence("Torso-Combat-Right-Up", torsoCombatRightUpSequence);
SpriteSequence torsoCombatLeftDownSequence = new SpriteSequence(SWING_ANIMATION_SPEED).addSprite(playerSprites[0][11][1])
.addSprite(playerSprites[0][12][1]).addSprite(playerSprites[0][13][1]).addSprite(playerSprites[0][14][1]);
renderedObject.addSequence("Torso-Combat-Left-Down", torsoCombatLeftDownSequence);
SpriteSequence torsoCombatLeftUpSequence = new SpriteSequence(SWING_ANIMATION_SPEED).addSprite(playerSprites[0][15][1])
.addSprite(playerSprites[0][16][1]).addSprite(playerSprites[0][17][1]).addSprite(playerSprites[0][18][1]);
renderedObject.addSequence("Torso-Combat-Left-Up", torsoCombatLeftUpSequence);
renderedObject.playAnimation("torso", "Torso-Idle-Right");
renderedObject.playAnimation("legs", "Legs-Idle-Right");
return renderedObject;
}
}

View File

@ -42,9 +42,11 @@ public class PhysicsSystem extends EntitySystem {
float move = 0;
if (obj.movingLeft) {
move--;
obj.movedLeftLast = true;
}
if (obj.movingRight) {
move++;
obj.movedLeftLast = false;
}
obj.getVelocity().x = move * obj.getMovespeed();
if (combat != null) {

View File

@ -17,6 +17,7 @@ import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
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.gui.nodes.GUINode;
@ -25,6 +26,7 @@ import com.saltosion.gladiator.gui.nodes.TextNode;
import com.saltosion.gladiator.gui.properties.TextProperty;
import com.saltosion.gladiator.util.AppUtil;
import com.saltosion.gladiator.util.Global;
import com.saltosion.gladiator.util.Log;
import com.saltosion.gladiator.util.SpriteLoader;
import com.saltosion.gladiator.util.SpriteSequence;
import java.util.ArrayList;
@ -34,6 +36,7 @@ public class RenderingSystem extends EntitySystem {
private final ComponentMapper<CRenderedObject> rom = ComponentMapper.getFor(CRenderedObject.class);
private final ComponentMapper<CPhysics> pm = ComponentMapper.getFor(CPhysics.class);
private final ComponentMapper<CCombat> cm = ComponentMapper.getFor(CCombat.class);
private ImmutableArray<Entity> entities;
private SpriteBatch batch;
@ -93,6 +96,7 @@ public class RenderingSystem extends EntitySystem {
Gdx.gl.glClearColor(0, 0, 0, 0);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
updateEntityAnimations();
renderEntities(deltaTime);
renderGUI(new Vector2(0, 0));
renderDebug(camera);
@ -124,6 +128,71 @@ public class RenderingSystem extends EntitySystem {
return deltaString;
}
private void updateEntityAnimations() {
for (int i = 0; i < entities.size(); i++) {
updateEntityAnimation(entities.get(i));
}
}
private void updateEntityAnimation(Entity entity) {
CRenderedObject ro = rom.get(entity);
CPhysics po = pm.get(entity);
CCombat co = cm.get(entity);
if (ro == null || po == null || co == null) {
return;
}
boolean moving = false, combat = false;
String dirMove = po.movedLeftLast ? "Left" : "Right";
String dirSwing = "";
if (po.movingLeft || po.movingRight) {
moving = true;
}
if (co.swingCdCounter > 0) {
combat = true;
switch (co.getSwingDirection()) {
default:
case RIGHT:
dirSwing += "Right";
break;
case LEFT:
dirSwing += "Left";
break;
case UP:
if (dirMove.equals("Left")) {
dirSwing = "Left-";
} else {
dirSwing = "Right-";
}
dirSwing += "Up";
break;
case DOWN:
if (dirMove.equals("Left")) {
dirSwing = "Left-";
} else {
dirSwing = "Right-";
}
dirSwing += "Down";
break;
}
}
// Play animations
if (moving && combat) {
ro.playAnimation("torso", "Torso-Combat-" + dirSwing);
ro.playAnimation("legs", "Legs-Run-" + dirMove);
} else if (combat) {
ro.playAnimation("torso", "Torso-Combat-" + dirSwing);
} else if (moving) {
ro.playAnimation("torso", "Torso-Run-" + dirMove);
ro.playAnimation("legs", "Legs-Run-" + dirMove);
} else {
ro.playAnimation("torso", "Torso-Idle-" + dirMove);
ro.playAnimation("legs", "Legs-Idle-" + dirMove);
}
}
private void renderEntities(float deltaTime) {
batch.setProjectionMatrix(camera.combined);
batch.begin();
@ -216,8 +285,10 @@ public class RenderingSystem extends EntitySystem {
drawableText.add(new TextObject(text, position));
}
public void updateEntities(Engine engine) {
entities = engine.getEntitiesFor(Family.getFor(CRenderedObject.class, CPhysics.class));
public
void updateEntities(Engine engine) {
entities = engine.getEntitiesFor(Family.getFor(CRenderedObject.class, CPhysics.class
));
}
public boolean getDebug() {