Animation now renders, and rendered entities now must have position

This commit is contained in:
Allexit 2015-04-08 20:50:13 +03:00
parent ac68498548
commit 962dde9db2
9 changed files with 83 additions and 21 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 755 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 742 B

After

Width:  |  Height:  |  Size: 742 B

View File

@ -6,12 +6,8 @@ import com.badlogic.ashley.core.EntityListener;
import com.badlogic.ashley.core.Family;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.utils.TimeUtils;
import com.saltosion.gladiator.components.CPosition;
import com.saltosion.gladiator.components.CRenderedObject;
import com.saltosion.gladiator.systems.RenderingSystem;
import com.saltosion.gladiator.util.GlobalStrings;
@ -31,7 +27,7 @@ public class GladiatorBrawler extends ApplicationAdapter {
engine.addSystem(new RenderingSystem());
engine.addEntityListener(Family.getFor(CRenderedObject.class),
engine.addEntityListener(Family.getFor(CRenderedObject.class, CPosition.class),
new EntityListener() {
@Override
public void entityRemoved(Entity entity) {
@ -62,11 +58,15 @@ public class GladiatorBrawler extends ApplicationAdapter {
player = new Entity();
CRenderedObject renderedObject = new CRenderedObject();
Sprite staticplayer = SpriteLoader.loadSprite(GlobalStrings.STATICPLAYER);
renderedObject.spritesequences.put("Idle", new SpriteSequence(staticplayer));
renderedObject.currentSequence = "Idle";
Sprite player1 = SpriteLoader.loadSprite(GlobalStrings.PLAYERIMG, 0, 0, 64, 64);
Sprite player2 = SpriteLoader.loadSprite(GlobalStrings.PLAYERIMG, 1, 0, 64, 64);
SpriteSequence sequence = new SpriteSequence(1).addSprite(player1).addSprite(player2);
renderedObject.addSequence("Idle", sequence);
renderedObject.setCurrentSequence("Idle");
player.add(renderedObject);
player.add(new CPosition());
player.getComponent(CPosition.class).x = 50;
player.getComponent(CPosition.class).y = 50;
engine.addEntity(player);
}

View File

@ -18,13 +18,18 @@ public class SpriteSequence {
defaultPlayspeed = 0;
}
public SpriteSequence(float playspeed, ArrayList<Sprite> sprites) {
public SpriteSequence(float playspeed) {
this.defaultPlayspeed = playspeed;
if (sprites != null) {
this.sprites = sprites;
}
}
public SpriteSequence addSprite(Sprite s) {
sprites.add(s);
return this;
}
public Sprite getSprite(int index) {
return sprites.get(index);
}
@ -32,5 +37,8 @@ public class SpriteSequence {
public float getPlayspeed() {
return defaultPlayspeed;
}
public int frameCount() {
return sprites.size();
}
}

View File

@ -0,0 +1,10 @@
package com.saltosion.gladiator.components;
import com.badlogic.ashley.core.Component;
public class CPosition extends Component {
public int x = 0;
public int y = 0;
}

View File

@ -7,8 +7,43 @@ import com.badlogic.gdx.graphics.g2d.Sprite;
import com.saltosion.gladiator.SpriteSequence;
public class CRenderedObject extends Component {
public HashMap<String, SpriteSequence> spritesequences = new HashMap<String, SpriteSequence>();
public String currentSequence = "";
public Sprite staticSprite = null; // If this is set, only this image will be shown as the entity
public float currentframe = 0;
private HashMap<String, SpriteSequence> spritesequences = new HashMap<String, SpriteSequence>();
private String currentSequence = "";
private float currentframe = 0;
public CRenderedObject() {}
/**
* 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));
currentSequence = "Idle";
}
public void addSequence(String key, SpriteSequence sequence) {
spritesequences.put(key, sequence);
}
public void setCurrentSequence(String sequence) {
this.currentSequence = sequence;
}
public void setCurrentFrame(float frame) {
this.currentframe = frame;
}
public SpriteSequence getSequence(String key) {
return spritesequences.get(key);
}
public float getCurrentFrame() {
return currentframe;
}
public String getCurrentSequence() {
return currentSequence;
}
}

View File

@ -11,6 +11,7 @@ import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.saltosion.gladiator.SpriteSequence;
import com.saltosion.gladiator.components.CPosition;
import com.saltosion.gladiator.components.CRenderedObject;
public class RenderingSystem extends EntitySystem {
@ -39,17 +40,23 @@ public class RenderingSystem extends EntitySystem {
for (int i=0; i<entities.size(); i++) {
CRenderedObject renderedObject = entities.get(i).getComponent(CRenderedObject.class);
SpriteSequence currSequence = renderedObject.spritesequences.get(renderedObject.currentSequence);
int currFrame = (int) Math.floor(renderedObject.currentframe);
SpriteSequence currSequence = renderedObject.getSequence(renderedObject.getCurrentSequence());
int currFrame = (int) Math.floor(renderedObject.getCurrentFrame());
Sprite currSprite = currSequence.getSprite(currFrame);
batch.draw(currSprite, 0, 0);
CPosition position = entities.get(i).getComponent(CPosition.class);
batch.draw(currSprite, position.x, position.y);
float nextFrame = renderedObject.getCurrentFrame() + deltaTime*currSequence.getPlayspeed();
renderedObject.setCurrentFrame(nextFrame%currSequence.frameCount());
}
batch.end();
}
public void updateEntities(Engine engine) {
entities = engine.getEntitiesFor(Family.getFor(CRenderedObject.class));
entities = engine.getEntitiesFor(Family.getFor(CRenderedObject.class, CPosition.class));
}
}

View File

@ -3,6 +3,7 @@
public class GlobalStrings {
public static final String STATICPLAYER = "STATICPLAYER";
public static final String PLAYERIMG = "PLAYERIMG";
public static final String GAME_NAME = "Gladiator Brawl";

View File

@ -13,6 +13,7 @@ public class SpriteLoader {
static {
loadTexture(GlobalStrings.STATICPLAYER, "sprites/staticplayer.png");
loadTexture(GlobalStrings.PLAYERIMG, "sprites/player/player.png");
}
/**
@ -25,7 +26,7 @@ public class SpriteLoader {
* @return
*/
public static Sprite loadSprite(String texKey, int x, int y, int width, int height) {
TextureRegion tr = new TextureRegion(textures.get(texKey), x, y, width, height);
TextureRegion tr = new TextureRegion(textures.get(texKey), x*width, y*height, width, height);
return new Sprite(tr);
}