From ac6849854811a210d768100a804009dd1c73ffec Mon Sep 17 00:00:00 2001 From: Allexit Date: Wed, 8 Apr 2015 20:15:14 +0300 Subject: [PATCH] Every entity with "CRenderedObject" is now drawn at 0, 0 --- .../saltosion/gladiator/GladiatorBrawler.java | 60 ++++++++++++++----- .../saltosion/gladiator/SpriteSequence.java | 36 +++++++++++ .../gladiator/components/CRenderedObject.java | 14 +++++ .../gladiator/systems/RenderingSystem.java | 55 +++++++++++++++++ .../gladiator/util/GlobalStrings.java | 2 +- .../gladiator/util/SpriteLoader.java | 1 - 6 files changed, 152 insertions(+), 16 deletions(-) create mode 100644 core/src/com/saltosion/gladiator/SpriteSequence.java create mode 100644 core/src/com/saltosion/gladiator/components/CRenderedObject.java create mode 100644 core/src/com/saltosion/gladiator/systems/RenderingSystem.java diff --git a/core/src/com/saltosion/gladiator/GladiatorBrawler.java b/core/src/com/saltosion/gladiator/GladiatorBrawler.java index 5ca5169..1869ed6 100644 --- a/core/src/com/saltosion/gladiator/GladiatorBrawler.java +++ b/core/src/com/saltosion/gladiator/GladiatorBrawler.java @@ -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); } } diff --git a/core/src/com/saltosion/gladiator/SpriteSequence.java b/core/src/com/saltosion/gladiator/SpriteSequence.java new file mode 100644 index 0000000..764d6f9 --- /dev/null +++ b/core/src/com/saltosion/gladiator/SpriteSequence.java @@ -0,0 +1,36 @@ +package com.saltosion.gladiator; + +import java.util.ArrayList; + +import com.badlogic.gdx.graphics.g2d.Sprite; + +public class SpriteSequence { + + private ArrayList sprites = new ArrayList(); + private float defaultPlayspeed = 1; + + /** + * A static single image. + * @param sprite + */ + public SpriteSequence(Sprite sprite) { + sprites.add(sprite); + defaultPlayspeed = 0; + } + + public SpriteSequence(float playspeed, ArrayList sprites) { + this.defaultPlayspeed = playspeed; + if (sprites != null) { + this.sprites = sprites; + } + } + + public Sprite getSprite(int index) { + return sprites.get(index); + } + + public float getPlayspeed() { + return defaultPlayspeed; + } + +} diff --git a/core/src/com/saltosion/gladiator/components/CRenderedObject.java b/core/src/com/saltosion/gladiator/components/CRenderedObject.java new file mode 100644 index 0000000..d7ee945 --- /dev/null +++ b/core/src/com/saltosion/gladiator/components/CRenderedObject.java @@ -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 spritesequences = new HashMap(); + public String currentSequence = ""; + public Sprite staticSprite = null; // If this is set, only this image will be shown as the entity + public float currentframe = 0; +} diff --git a/core/src/com/saltosion/gladiator/systems/RenderingSystem.java b/core/src/com/saltosion/gladiator/systems/RenderingSystem.java new file mode 100644 index 0000000..03aba2f --- /dev/null +++ b/core/src/com/saltosion/gladiator/systems/RenderingSystem.java @@ -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 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