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

View File

@ -18,13 +18,18 @@ public class SpriteSequence {
defaultPlayspeed = 0; defaultPlayspeed = 0;
} }
public SpriteSequence(float playspeed, ArrayList<Sprite> sprites) { public SpriteSequence(float playspeed) {
this.defaultPlayspeed = playspeed; this.defaultPlayspeed = playspeed;
if (sprites != null) { if (sprites != null) {
this.sprites = sprites; this.sprites = sprites;
} }
} }
public SpriteSequence addSprite(Sprite s) {
sprites.add(s);
return this;
}
public Sprite getSprite(int index) { public Sprite getSprite(int index) {
return sprites.get(index); return sprites.get(index);
} }
@ -32,5 +37,8 @@ public class SpriteSequence {
public float getPlayspeed() { public float getPlayspeed() {
return defaultPlayspeed; 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; import com.saltosion.gladiator.SpriteSequence;
public class CRenderedObject extends Component { public class CRenderedObject extends Component {
public HashMap<String, SpriteSequence> spritesequences = new HashMap<String, SpriteSequence>(); private HashMap<String, SpriteSequence> spritesequences = new HashMap<String, SpriteSequence>();
public String currentSequence = ""; private String currentSequence = "";
public Sprite staticSprite = null; // If this is set, only this image will be shown as the entity private float currentframe = 0;
public 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.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.saltosion.gladiator.SpriteSequence; import com.saltosion.gladiator.SpriteSequence;
import com.saltosion.gladiator.components.CPosition;
import com.saltosion.gladiator.components.CRenderedObject; import com.saltosion.gladiator.components.CRenderedObject;
public class RenderingSystem extends EntitySystem { public class RenderingSystem extends EntitySystem {
@ -39,17 +40,23 @@ public class RenderingSystem extends EntitySystem {
for (int i=0; i<entities.size(); i++) { for (int i=0; i<entities.size(); i++) {
CRenderedObject renderedObject = entities.get(i).getComponent(CRenderedObject.class); CRenderedObject renderedObject = entities.get(i).getComponent(CRenderedObject.class);
SpriteSequence currSequence = renderedObject.spritesequences.get(renderedObject.currentSequence); SpriteSequence currSequence = renderedObject.getSequence(renderedObject.getCurrentSequence());
int currFrame = (int) Math.floor(renderedObject.currentframe); int currFrame = (int) Math.floor(renderedObject.getCurrentFrame());
Sprite currSprite = currSequence.getSprite(currFrame); 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(); batch.end();
} }
public void updateEntities(Engine engine) { 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 class GlobalStrings {
public static final String STATICPLAYER = "STATICPLAYER"; public static final String STATICPLAYER = "STATICPLAYER";
public static final String PLAYERIMG = "PLAYERIMG";
public static final String GAME_NAME = "Gladiator Brawl"; public static final String GAME_NAME = "Gladiator Brawl";

View File

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