Added debug rendering. Toggle with F2.

This commit is contained in:
Jeasonfire 2015-05-10 22:04:20 +03:00
parent 6e1387662d
commit ae5300f3fd
6 changed files with 111 additions and 36 deletions

View File

@ -0,0 +1,26 @@
package com.saltosion.gladiator.input;
import com.saltosion.gladiator.systems.RenderingSystem;
import com.saltosion.gladiator.util.AppUtil;
/**
*
* @author Jens "Jeasonfire" Pitkänen <jeasonfire@gmail.com>
*/
public class IRDebugToggle implements InputReceiver {
@Override
public boolean pressed() {
RenderingSystem renderingSystem = AppUtil.engine.getSystem(RenderingSystem.class);
if (renderingSystem != null) {
renderingSystem.setDebug(!renderingSystem.getDebug());
}
return true;
}
@Override
public boolean released() {
return false;
}
}

View File

@ -18,6 +18,7 @@ public class InputHandler implements InputProcessor {
keys.put(Keys.RIGHT, Name.SWING_RIGHT); keys.put(Keys.RIGHT, Name.SWING_RIGHT);
keys.put(Keys.UP, Name.SWING_UP); keys.put(Keys.UP, Name.SWING_UP);
keys.put(Keys.DOWN, Name.SWING_DOWN); keys.put(Keys.DOWN, Name.SWING_DOWN);
keys.put(Keys.F2, Name.DEBUG);
} }
@Override @Override

View File

@ -1,6 +1,15 @@
package com.saltosion.gladiator.input; package com.saltosion.gladiator.input;
public interface InputReceiver { public interface InputReceiver {
/**
* @return Returns if the keypress was handled.
*/
public boolean pressed(); public boolean pressed();
/**
* @return Returns if the key's release was handled.
*/
public boolean released(); public boolean released();
} }

View File

@ -6,7 +6,8 @@ import com.saltosion.gladiator.util.Direction;
import com.saltosion.gladiator.util.Name; import com.saltosion.gladiator.util.Name;
public class InputReceivers { public class InputReceivers {
public static HashMap<String, InputReceiver> inputreceivers = new HashMap<String, InputReceiver>();
public static final HashMap<String, InputReceiver> inputreceivers = new HashMap<String, InputReceiver>();
static { static {
inputreceivers.put(Name.MOVE_LEFT, new IRMoveLeft()); inputreceivers.put(Name.MOVE_LEFT, new IRMoveLeft());
@ -16,6 +17,7 @@ public class InputReceivers {
inputreceivers.put(Name.SWING_RIGHT, new IRSwing(Direction.RIGHT)); inputreceivers.put(Name.SWING_RIGHT, new IRSwing(Direction.RIGHT));
inputreceivers.put(Name.SWING_UP, new IRSwing(Direction.UP)); inputreceivers.put(Name.SWING_UP, new IRSwing(Direction.UP));
inputreceivers.put(Name.SWING_DOWN, new IRSwing(Direction.DOWN)); inputreceivers.put(Name.SWING_DOWN, new IRSwing(Direction.DOWN));
inputreceivers.put(Name.DEBUG, new IRDebugToggle());
} }
public static InputReceiver getReceiver(String key) { public static InputReceiver getReceiver(String key) {

View File

@ -7,10 +7,13 @@ import com.badlogic.ashley.core.EntitySystem;
import com.badlogic.ashley.core.Family; import com.badlogic.ashley.core.Family;
import com.badlogic.ashley.utils.ImmutableArray; import com.badlogic.ashley.utils.ImmutableArray;
import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20; import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera; 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.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.saltosion.gladiator.components.CPhysics; import com.saltosion.gladiator.components.CPhysics;
import com.saltosion.gladiator.components.CRenderedObject; import com.saltosion.gladiator.components.CRenderedObject;
import com.saltosion.gladiator.util.AppUtil; import com.saltosion.gladiator.util.AppUtil;
@ -18,19 +21,24 @@ import com.saltosion.gladiator.util.SpriteSequence;
public class RenderingSystem extends EntitySystem { public class RenderingSystem extends EntitySystem {
private ComponentMapper<CRenderedObject> rom = ComponentMapper.getFor(CRenderedObject.class); private final ComponentMapper<CRenderedObject> rom = ComponentMapper.getFor(CRenderedObject.class);
private ComponentMapper<CPhysics> pm = ComponentMapper.getFor(CPhysics.class); private final ComponentMapper<CPhysics> pm = ComponentMapper.getFor(CPhysics.class);
private ImmutableArray<Entity> entities; private ImmutableArray<Entity> entities;
private SpriteBatch batch; private SpriteBatch batch;
private ShapeRenderer debugRenderer;
private OrthographicCamera camera; private OrthographicCamera camera;
private boolean debug = true;
private final Color debugColor = new Color(0, 1, 0, 1);
@Override @Override
public void addedToEngine(Engine engine) { public void addedToEngine(Engine engine) {
updateEntities(engine); updateEntities(engine);
batch = new SpriteBatch(); batch = new SpriteBatch();
debugRenderer = new ShapeRenderer();
camera = new OrthographicCamera(); camera = new OrthographicCamera();
camera.setToOrtho(false, 1, 1); camera.setToOrtho(false, 1, 1);
} }
@ -49,7 +57,7 @@ public class RenderingSystem extends EntitySystem {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.setProjectionMatrix(camera.combined); batch.setProjectionMatrix(camera.combined);
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()); SpriteSequence currSequence = renderedObject.getSequence(renderedObject.getCurrentSequence());
int currFrame = (int) Math.floor(renderedObject.getCurrentFrame()); int currFrame = (int) Math.floor(renderedObject.getCurrentFrame());
@ -60,18 +68,45 @@ public class RenderingSystem extends EntitySystem {
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();
if (debug) {
debugRenderer.setProjectionMatrix(camera.combined);
debugRenderer.begin(ShapeType.Line);
debugRenderer.setColor(debugColor);
for (int i = 0; i < entities.size(); i++) {
CPhysics physics = pm.get(entities.get(i));
float x0 = physics.getPosition().x - physics.getSize().x / 2;
float x1 = physics.getPosition().x + physics.getSize().x / 2;
float y0 = physics.getPosition().y - physics.getSize().y / 2;
float y1 = physics.getPosition().y + physics.getSize().y / 2;
debugRenderer.line(x0, y0, x1, y0);
debugRenderer.line(x1, y0, x1, y1);
debugRenderer.line(x1, y1, x0, y1);
debugRenderer.line(x0, y1, x0, y0);
}
debugRenderer.end();
}
} }
public void updateEntities(Engine engine) { public void updateEntities(Engine engine) {
entities = engine.getEntitiesFor(Family.getFor(CRenderedObject.class, CPhysics.class)); entities = engine.getEntitiesFor(Family.getFor(CRenderedObject.class, CPhysics.class));
} }
public boolean getDebug() {
return this.debug;
}
public void setDebug(boolean debug) {
this.debug = debug;
}
} }

View File

@ -2,19 +2,21 @@ package com.saltosion.gladiator.util;
public class Name { public class Name {
public static final String GAME_NAME = "Gladiator Brawl"; public static final String GAME_NAME = "Gladiator Brawl";
public static final String STATICPLAYER = "STATICPLAYER"; public static final String DEBUG = "DEBUG";
public static final String PLAYERIMG = "PLAYERIMG";
public static final String GROUNDIMG = "GROUNDIMG";
public static final String WALLIMG = "WALLIMG";
public static final String MOVE_LEFT = "MOVE_LEFT"; public static final String STATICPLAYER = "STATICPLAYER";
public static final String MOVE_RIGHT = "MOVE_RIGHT"; public static final String PLAYERIMG = "PLAYERIMG";
public static final String JUMP = "JUMP"; public static final String GROUNDIMG = "GROUNDIMG";
public static final String SWING_LEFT = "SWING_LEFT"; public static final String WALLIMG = "WALLIMG";
public static final String SWING_RIGHT = "SWING_RIGHT";
public static final String SWING_UP = "SWING_UP"; public static final String MOVE_LEFT = "MOVE_LEFT";
public static final String SWING_DOWN = "SWING_DOWN"; public static final String MOVE_RIGHT = "MOVE_RIGHT";
public static final String JUMP = "JUMP";
public static final String SWING_LEFT = "SWING_LEFT";
public static final String SWING_RIGHT = "SWING_RIGHT";
public static final String SWING_UP = "SWING_UP";
public static final String SWING_DOWN = "SWING_DOWN";
} }