Added multichannel support to entity rendering system
This commit is contained in:
parent
9fb15e572d
commit
f441b6c164
@ -1,6 +1,8 @@
|
|||||||
package com.saltosion.gladiator.components;
|
package com.saltosion.gladiator.components;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
import com.badlogic.ashley.core.Component;
|
import com.badlogic.ashley.core.Component;
|
||||||
import com.badlogic.gdx.graphics.g2d.Sprite;
|
import com.badlogic.gdx.graphics.g2d.Sprite;
|
||||||
@ -8,10 +10,13 @@ import com.saltosion.gladiator.util.SpriteSequence;
|
|||||||
|
|
||||||
public class CRenderedObject extends Component {
|
public class CRenderedObject extends Component {
|
||||||
private HashMap<String, SpriteSequence> spritesequences = new HashMap<String, SpriteSequence>();
|
private HashMap<String, SpriteSequence> spritesequences = new HashMap<String, SpriteSequence>();
|
||||||
private String currentSequence = "";
|
private ArrayList<String> channels = new ArrayList<String>();
|
||||||
private float currentframe = 0;
|
private HashMap<String, String> currentSequences = new HashMap<String, String>();
|
||||||
|
private HashMap<String, Float> currentFrames = new HashMap<String, Float>();
|
||||||
|
|
||||||
public CRenderedObject() {}
|
public CRenderedObject() {
|
||||||
|
addChannel("default");
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Can be used if the Rendered Object is a single static image always.
|
* 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) {
|
public CRenderedObject(Sprite sprite) {
|
||||||
spritesequences.put("Idle", new SpriteSequence(sprite));
|
spritesequences.put("Idle", new SpriteSequence(sprite));
|
||||||
currentSequence = "Idle";
|
addChannel("default");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addSequence(String key, SpriteSequence sequence) {
|
public void addSequence(String key, SpriteSequence sequence) {
|
||||||
spritesequences.put(key, 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) {
|
public void setCurrentFrame(float frame) {
|
||||||
this.currentframe = frame;
|
setCurrentFrame("default", frame);
|
||||||
}
|
}
|
||||||
|
|
||||||
public SpriteSequence getSequence(String key) {
|
public SpriteSequence getSequence(String key) {
|
||||||
return spritesequences.get(key);
|
return spritesequences.get(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Plays animation on the "default" channel, starting in frame 0
|
||||||
|
* @param key
|
||||||
|
*/
|
||||||
public void playAnimation(String key) {
|
public void playAnimation(String key) {
|
||||||
playAnimation(key, 0);
|
playAnimation(key, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void playAnimation(String key, int startingframe) {
|
/**
|
||||||
if (spritesequences.containsKey(key)) {
|
* Plays animation on channelName
|
||||||
currentSequence = key;
|
* @param channelName
|
||||||
currentframe = startingframe;
|
* @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;
|
return;
|
||||||
}
|
}
|
||||||
String[] s = (String[]) spritesequences.keySet().toArray();
|
String[] s = (String[]) spritesequences.keySet().toArray();
|
||||||
currentSequence = s[0];
|
setCurrentSequence(channels.get(0), s[0]);
|
||||||
currentframe = 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<String> 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 <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) ) {
|
||||||
|
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<String> getChannels() {
|
||||||
|
return channels;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the current frame on "default" channel.
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
public float getCurrentFrame() {
|
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() {
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -99,21 +99,23 @@ public class RenderingSystem extends EntitySystem {
|
|||||||
batch.begin();
|
batch.begin();
|
||||||
for (int i = 0; i < entities.size(); i++) {
|
for (int i = 0; i < entities.size(); i++) {
|
||||||
CRenderedObject renderedObject = rom.get(entities.get(i));
|
CRenderedObject renderedObject = rom.get(entities.get(i));
|
||||||
SpriteSequence currSequence = renderedObject.getSequence(renderedObject.getCurrentSequence());
|
for (String channel : renderedObject.getChannels()) {
|
||||||
int currFrame = (int) Math.floor(renderedObject.getCurrentFrame());
|
SpriteSequence currSequence = renderedObject.getSequence(renderedObject.getCurrentSequence(channel));
|
||||||
Sprite currSprite = currSequence.getSprite(currFrame);
|
int currFrame = (int) Math.floor(renderedObject.getCurrentFrame(channel));
|
||||||
|
Sprite currSprite = currSequence.getSprite(currFrame);
|
||||||
|
|
||||||
CPhysics physics = pm.get(entities.get(i));
|
CPhysics physics = pm.get(entities.get(i));
|
||||||
|
|
||||||
int spriteHeight = currSprite.getRegionHeight();
|
int spriteHeight = currSprite.getRegionHeight();
|
||||||
int spriteWidth = currSprite.getRegionWidth();
|
int spriteWidth = currSprite.getRegionWidth();
|
||||||
|
|
||||||
currSprite.setPosition(physics.getPosition().x - spriteWidth / 2,
|
currSprite.setPosition(physics.getPosition().x - spriteWidth / 2,
|
||||||
physics.getPosition().y - spriteHeight / 2);
|
physics.getPosition().y - spriteHeight / 2);
|
||||||
currSprite.draw(batch);
|
currSprite.draw(batch);
|
||||||
|
|
||||||
float nextFrame = renderedObject.getCurrentFrame() + deltaTime * currSequence.getPlayspeed();
|
float nextFrame = renderedObject.getCurrentFrame() + deltaTime * currSequence.getPlayspeed();
|
||||||
renderedObject.setCurrentFrame(nextFrame % currSequence.frameCount());
|
renderedObject.setCurrentFrame(nextFrame % currSequence.frameCount());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
batch.end();
|
batch.end();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user