diff --git a/core/src/com/saltosion/gladiator/components/CRenderedObject.java b/core/src/com/saltosion/gladiator/components/CRenderedObject.java index def9b08..8d79248 100644 --- a/core/src/com/saltosion/gladiator/components/CRenderedObject.java +++ b/core/src/com/saltosion/gladiator/components/CRenderedObject.java @@ -1,6 +1,8 @@ package com.saltosion.gladiator.components; +import java.util.ArrayList; import java.util.HashMap; +import java.util.Set; import com.badlogic.ashley.core.Component; import com.badlogic.gdx.graphics.g2d.Sprite; @@ -8,10 +10,13 @@ import com.saltosion.gladiator.util.SpriteSequence; public class CRenderedObject extends Component { private HashMap spritesequences = new HashMap(); - private String currentSequence = ""; - private float currentframe = 0; + private ArrayList channels = new ArrayList(); + private HashMap currentSequences = new HashMap(); + private HashMap currentFrames = new HashMap(); - public CRenderedObject() {} + public CRenderedObject() { + addChannel("default"); + } /** * Can be used if the Rendered Object is a single static image always. @@ -19,46 +24,145 @@ public class CRenderedObject extends Component { */ public CRenderedObject(Sprite sprite) { spritesequences.put("Idle", new SpriteSequence(sprite)); - currentSequence = "Idle"; + addChannel("default"); } public void addSequence(String key, SpriteSequence sequence) { spritesequences.put(key, sequence); } - public void setCurrentSequence(String sequence) { - this.currentSequence = 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 + */ + 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) { - this.currentframe = 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); } - public void playAnimation(String key, int startingframe) { - if (spritesequences.containsKey(key)) { - currentSequence = key; - currentframe = startingframe; + /** + * 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(); - currentSequence = s[0]; - currentframe = 0; + 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); + } + + public void addChannel(String channelName) { + Set sequences = spritesequences.keySet(); + String sequence = ""; + if (sequences.size() > 0) { + sequence = (String) sequences.toArray()[0]; + } + this.channels.add(channelName); + 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. + */ + public boolean setChannelName(String oldName, String newName) { + if ( !this.channels.contains(oldName) || this.channels.contains(newName) ) { + return false; + } + this.channels.remove(oldName); + this.channels.add(newName); + this.currentFrames.put(newName, this.currentFrames.remove(oldName)); + 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 currentframe; + 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 currentSequence; + 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/systems/RenderingSystem.java b/core/src/com/saltosion/gladiator/systems/RenderingSystem.java index c46839f..586f61c 100644 --- a/core/src/com/saltosion/gladiator/systems/RenderingSystem.java +++ b/core/src/com/saltosion/gladiator/systems/RenderingSystem.java @@ -99,21 +99,23 @@ public class RenderingSystem extends EntitySystem { batch.begin(); for (int i = 0; i < entities.size(); i++) { CRenderedObject renderedObject = rom.get(entities.get(i)); - SpriteSequence currSequence = renderedObject.getSequence(renderedObject.getCurrentSequence()); - int currFrame = (int) Math.floor(renderedObject.getCurrentFrame()); - Sprite currSprite = currSequence.getSprite(currFrame); - - CPhysics physics = pm.get(entities.get(i)); - - int spriteHeight = currSprite.getRegionHeight(); - int spriteWidth = currSprite.getRegionWidth(); - - currSprite.setPosition(physics.getPosition().x - spriteWidth / 2, - physics.getPosition().y - spriteHeight / 2); - currSprite.draw(batch); - - float nextFrame = renderedObject.getCurrentFrame() + deltaTime * currSequence.getPlayspeed(); - renderedObject.setCurrentFrame(nextFrame % currSequence.frameCount()); + for (String channel : renderedObject.getChannels()) { + SpriteSequence currSequence = renderedObject.getSequence(renderedObject.getCurrentSequence(channel)); + int currFrame = (int) Math.floor(renderedObject.getCurrentFrame(channel)); + Sprite currSprite = currSequence.getSprite(currFrame); + + CPhysics physics = pm.get(entities.get(i)); + + int spriteHeight = currSprite.getRegionHeight(); + int spriteWidth = currSprite.getRegionWidth(); + + currSprite.setPosition(physics.getPosition().x - spriteWidth / 2, + physics.getPosition().y - spriteHeight / 2); + currSprite.draw(batch); + + float nextFrame = renderedObject.getCurrentFrame() + deltaTime * currSequence.getPlayspeed(); + renderedObject.setCurrentFrame(nextFrame % currSequence.frameCount()); + } } batch.end(); }