From 0409e91265f5d9dd5ee792915f33ccdc04462547 Mon Sep 17 00:00:00 2001 From: Jeasonfire Date: Sat, 16 May 2015 21:28:45 +0300 Subject: [PATCH] Added player animations in code. --- .../gladiator/components/CPhysics.java | 5 +- .../gladiator/components/CRenderedObject.java | 116 +++++++++------- .../gladiator/level/EntityFactory.java | 130 +++++++++++++----- .../gladiator/systems/PhysicsSystem.java | 2 + .../gladiator/systems/RenderingSystem.java | 75 +++++++++- 5 files changed, 245 insertions(+), 83 deletions(-) diff --git a/core/src/com/saltosion/gladiator/components/CPhysics.java b/core/src/com/saltosion/gladiator/components/CPhysics.java index 5a8a7c4..8434250 100644 --- a/core/src/com/saltosion/gladiator/components/CPhysics.java +++ b/core/src/com/saltosion/gladiator/components/CPhysics.java @@ -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 diff --git a/core/src/com/saltosion/gladiator/components/CRenderedObject.java b/core/src/com/saltosion/gladiator/components/CRenderedObject.java index 36f4b8d..bd7a07e 100644 --- a/core/src/com/saltosion/gladiator/components/CRenderedObject.java +++ b/core/src/com/saltosion/gladiator/components/CRenderedObject.java @@ -6,98 +6,90 @@ 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 spritesequences = new HashMap(); - private ArrayList channels = new ArrayList(); - private HashMap currentSequences = new HashMap(); - private HashMap currentFrames = new HashMap(); - + + private final HashMap spritesequences = new HashMap(); + private final ArrayList channels = new ArrayList(); + private final HashMap currentSequences = new HashMap(); + private final HashMap currentFrames = new HashMap(); + public CRenderedObject() { addChannel("default"); } - + /** * Can be used if the Rendered Object is a single static image always. + * * @param sprite */ public CRenderedObject(Sprite sprite) { spritesequences.put("Idle", new SpriteSequence(sprite)); addChannel("default"); } - + public void addSequence(String key, SpriteSequence sequence) { spritesequences.put(key, sequence); } - + /** * Sets sequence for channelName + * * @param channelName * @param sequence */ public void setCurrentSequence(String channelName, String sequence) { this.currentSequences.put(channelName, sequence); } - + /** * Sets sequence for "default" channel. - * @param sequence + * + * @param sequence */ public void setCurrentSequence(String sequence) { setCurrentSequence("default", sequence); } - + /** * Sets frame for channelName + * * @param channelName * @param frame */ public void setCurrentFrame(String channelName, float frame) { this.currentFrames.put(channelName, frame); } - + /** * Sets frame for "default" channel. + * * @param frame */ public void setCurrentFrame(float frame) { setCurrentFrame("default", frame); } - + public SpriteSequence getSequence(String key) { return spritesequences.get(key); } - + /** * Plays animation on the "default" channel, starting in frame 0 + * * @param key */ public void playAnimation(String key) { playAnimation(key, 0); } - - /** - * Plays animation on channelName - * @param channelName - * @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 + * @param startingframe */ public void playAnimation(String key, int startingframe) { playAnimation("default", key, startingframe); @@ -105,15 +97,42 @@ public class CRenderedObject extends Component { /** * Plays animation on "channelName" starting on frame 0 + * * @param channelName channel name * @param key animation name */ public void playAnimation(String channelName, String key) { playAnimation(channelName, key, 0); } - - public void addChannel(String channelName) { - Set sequences = spritesequences.keySet(); + + /** + * 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 sequences = spritesequences.keySet(); String sequence = ""; if (sequences.size() > 0) { sequence = (String) sequences.toArray()[0]; @@ -122,15 +141,17 @@ public class CRenderedObject extends Component { this.currentSequences.put(channelName, sequence); this.currentFrames.put(channelName, 0f); } - + /** * Sets channel name + * * @param oldName Old name of the channel * @param newName New name of the channel to be set - * @return boolean, false if channel with oldName doesn't exist or a channel with newName already exists. + * @return boolean, 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) ) { + if (!this.channels.contains(oldName) || this.channels.contains(newName)) { return false; } this.channels.remove(oldName); @@ -139,39 +160,42 @@ public class CRenderedObject extends Component { this.currentSequences.put(newName, this.currentSequences.remove(oldName)); return true; } - + public ArrayList getChannels() { return channels; } - + /** * Returns the current frame on "default" channel. + * * @return */ public float getCurrentFrame() { return currentFrames.get("default"); } - + /** * Returns the frame on "channel". + * * @param channel * @return */ public float getCurrentFrame(String channel) { return currentFrames.get(channel); } - + public String getCurrentSequence() { return currentSequences.get("default"); } - + /** * Returns the current sequence on "channel" + * * @param channel * @return */ public String getCurrentSequence(String channel) { return currentSequences.get(channel); } - -} \ No newline at end of file + +} diff --git a/core/src/com/saltosion/gladiator/level/EntityFactory.java b/core/src/com/saltosion/gladiator/level/EntityFactory.java index 93a4042..a8ee9dd 100644 --- a/core/src/com/saltosion/gladiator/level/EntityFactory.java +++ b/core/src/com/saltosion/gladiator/level/EntityFactory.java @@ -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; + } + } diff --git a/core/src/com/saltosion/gladiator/systems/PhysicsSystem.java b/core/src/com/saltosion/gladiator/systems/PhysicsSystem.java index 2478549..85a7e1f 100644 --- a/core/src/com/saltosion/gladiator/systems/PhysicsSystem.java +++ b/core/src/com/saltosion/gladiator/systems/PhysicsSystem.java @@ -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) { diff --git a/core/src/com/saltosion/gladiator/systems/RenderingSystem.java b/core/src/com/saltosion/gladiator/systems/RenderingSystem.java index b1a1297..8e8a7e7 100644 --- a/core/src/com/saltosion/gladiator/systems/RenderingSystem.java +++ b/core/src/com/saltosion/gladiator/systems/RenderingSystem.java @@ -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 rom = ComponentMapper.getFor(CRenderedObject.class); private final ComponentMapper pm = ComponentMapper.getFor(CPhysics.class); + private final ComponentMapper cm = ComponentMapper.getFor(CCombat.class); private ImmutableArray 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() {