Every entity with "CRenderedObject" is now drawn at 0, 0

This commit is contained in:
Allexit 2015-04-08 20:15:14 +03:00
parent 54e7238f3a
commit ac68498548
6 changed files with 152 additions and 16 deletions

View File

@ -1,41 +1,73 @@
package com.saltosion.gladiator;
import com.badlogic.ashley.core.Engine;
import com.badlogic.ashley.core.Entity;
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.Texture;
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.CRenderedObject;
import com.saltosion.gladiator.systems.RenderingSystem;
import com.saltosion.gladiator.util.GlobalStrings;
import com.saltosion.gladiator.util.SpriteLoader;
public class GladiatorBrawler extends ApplicationAdapter {
private Batch batch;
private Sprite staticplayer;
private OrthographicCamera camera;
private Engine engine;
private Entity player;
@Override
public void create () {
// Initialize the Engine
engine = new Engine();
camera = new OrthographicCamera();
camera.setToOrtho(false, 384, 216);
batch = new SpriteBatch();
staticplayer = SpriteLoader.loadSprite(GlobalStrings.STATICPLAYER);
engine.addSystem(new RenderingSystem());
engine.addEntityListener(Family.getFor(CRenderedObject.class),
new EntityListener() {
@Override
public void entityRemoved(Entity entity) {
RenderingSystem rs = engine.getSystem(RenderingSystem.class);
rs.updateEntities(engine);
}
@Override
public void entityAdded(Entity entity) {
RenderingSystem rs = engine.getSystem(RenderingSystem.class);
rs.updateEntities(engine);
}
});
// Initialize player
initializePlayer();
}
@Override
public void render () {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.setProjectionMatrix(camera.combined);
batch.begin();
batch.draw(staticplayer, 0, 0);
batch.end();
engine.update(Gdx.graphics.getDeltaTime());
}
public void initializePlayer() {
player = new Entity();
CRenderedObject renderedObject = new CRenderedObject();
Sprite staticplayer = SpriteLoader.loadSprite(GlobalStrings.STATICPLAYER);
renderedObject.spritesequences.put("Idle", new SpriteSequence(staticplayer));
renderedObject.currentSequence = "Idle";
player.add(renderedObject);
engine.addEntity(player);
}
}

View File

@ -0,0 +1,36 @@
package com.saltosion.gladiator;
import java.util.ArrayList;
import com.badlogic.gdx.graphics.g2d.Sprite;
public class SpriteSequence {
private ArrayList<Sprite> sprites = new ArrayList<Sprite>();
private float defaultPlayspeed = 1;
/**
* A static single image.
* @param sprite
*/
public SpriteSequence(Sprite sprite) {
sprites.add(sprite);
defaultPlayspeed = 0;
}
public SpriteSequence(float playspeed, ArrayList<Sprite> sprites) {
this.defaultPlayspeed = playspeed;
if (sprites != null) {
this.sprites = sprites;
}
}
public Sprite getSprite(int index) {
return sprites.get(index);
}
public float getPlayspeed() {
return defaultPlayspeed;
}
}

View File

@ -0,0 +1,14 @@
package com.saltosion.gladiator.components;
import java.util.HashMap;
import com.badlogic.ashley.core.Component;
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;
}

View File

@ -0,0 +1,55 @@
package com.saltosion.gladiator.systems;
import com.badlogic.ashley.core.Engine;
import com.badlogic.ashley.core.Entity;
import com.badlogic.ashley.core.EntitySystem;
import com.badlogic.ashley.core.Family;
import com.badlogic.ashley.utils.ImmutableArray;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
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.CRenderedObject;
public class RenderingSystem extends EntitySystem {
private ImmutableArray<Entity> entities;
private SpriteBatch batch;
private OrthographicCamera camera;
@Override
public void addedToEngine(Engine engine) {
updateEntities(engine);
batch = new SpriteBatch();
camera = new OrthographicCamera();
camera.setToOrtho(false, 384, 216);
}
@Override
public void update(float deltaTime) {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.setProjectionMatrix(camera.combined);
batch.begin();
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);
Sprite currSprite = currSequence.getSprite(currFrame);
batch.draw(currSprite, 0, 0);
}
batch.end();
}
public void updateEntities(Engine engine) {
entities = engine.getEntitiesFor(Family.getFor(CRenderedObject.class));
}
}

View File

@ -1,4 +1,4 @@
package com.saltosion.gladiator.util;
package com.saltosion.gladiator.util;
public class GlobalStrings {

View File

@ -1,6 +1,5 @@
package com.saltosion.gladiator.util;
import java.util.ArrayList;
import java.util.HashMap;
import com.badlogic.gdx.Gdx;