Added debug rendering. Toggle with F2.
This commit is contained in:
parent
6e1387662d
commit
ae5300f3fd
26
core/src/com/saltosion/gladiator/input/IRDebugToggle.java
Normal file
26
core/src/com/saltosion/gladiator/input/IRDebugToggle.java
Normal 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;
|
||||
}
|
||||
|
||||
}
|
@ -7,7 +7,7 @@ import com.badlogic.gdx.InputProcessor;
|
||||
import com.saltosion.gladiator.util.Name;
|
||||
|
||||
public class InputHandler implements InputProcessor {
|
||||
|
||||
|
||||
public HashMap<Integer, String> keys = new HashMap<Integer, String>();
|
||||
|
||||
public InputHandler() {
|
||||
@ -18,8 +18,9 @@ public class InputHandler implements InputProcessor {
|
||||
keys.put(Keys.RIGHT, Name.SWING_RIGHT);
|
||||
keys.put(Keys.UP, Name.SWING_UP);
|
||||
keys.put(Keys.DOWN, Name.SWING_DOWN);
|
||||
keys.put(Keys.F2, Name.DEBUG);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean keyDown(int keycode) {
|
||||
if (!keys.containsKey(keycode)) {
|
||||
|
@ -1,6 +1,15 @@
|
||||
package com.saltosion.gladiator.input;
|
||||
|
||||
public interface InputReceiver {
|
||||
|
||||
/**
|
||||
* @return Returns if the keypress was handled.
|
||||
*/
|
||||
public boolean pressed();
|
||||
|
||||
/**
|
||||
* @return Returns if the key's release was handled.
|
||||
*/
|
||||
public boolean released();
|
||||
|
||||
}
|
||||
|
@ -6,8 +6,9 @@ import com.saltosion.gladiator.util.Direction;
|
||||
import com.saltosion.gladiator.util.Name;
|
||||
|
||||
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 {
|
||||
inputreceivers.put(Name.MOVE_LEFT, new IRMoveLeft());
|
||||
inputreceivers.put(Name.MOVE_RIGHT, new IRMoveRight());
|
||||
@ -16,8 +17,9 @@ public class InputReceivers {
|
||||
inputreceivers.put(Name.SWING_RIGHT, new IRSwing(Direction.RIGHT));
|
||||
inputreceivers.put(Name.SWING_UP, new IRSwing(Direction.UP));
|
||||
inputreceivers.put(Name.SWING_DOWN, new IRSwing(Direction.DOWN));
|
||||
inputreceivers.put(Name.DEBUG, new IRDebugToggle());
|
||||
}
|
||||
|
||||
|
||||
public static InputReceiver getReceiver(String key) {
|
||||
return inputreceivers.get(key);
|
||||
}
|
||||
|
@ -7,10 +7,13 @@ 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.Color;
|
||||
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.badlogic.gdx.graphics.glutils.ShapeRenderer;
|
||||
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
|
||||
import com.saltosion.gladiator.components.CPhysics;
|
||||
import com.saltosion.gladiator.components.CRenderedObject;
|
||||
import com.saltosion.gladiator.util.AppUtil;
|
||||
@ -18,27 +21,32 @@ import com.saltosion.gladiator.util.SpriteSequence;
|
||||
|
||||
public class RenderingSystem extends EntitySystem {
|
||||
|
||||
private ComponentMapper<CRenderedObject> rom = ComponentMapper.getFor(CRenderedObject.class);
|
||||
private ComponentMapper<CPhysics> pm = ComponentMapper.getFor(CPhysics.class);
|
||||
private final ComponentMapper<CRenderedObject> rom = ComponentMapper.getFor(CRenderedObject.class);
|
||||
private final ComponentMapper<CPhysics> pm = ComponentMapper.getFor(CPhysics.class);
|
||||
private ImmutableArray<Entity> entities;
|
||||
|
||||
|
||||
private SpriteBatch batch;
|
||||
private ShapeRenderer debugRenderer;
|
||||
private OrthographicCamera camera;
|
||||
|
||||
|
||||
private boolean debug = true;
|
||||
private final Color debugColor = new Color(0, 1, 0, 1);
|
||||
|
||||
@Override
|
||||
public void addedToEngine(Engine engine) {
|
||||
|
||||
|
||||
updateEntities(engine);
|
||||
|
||||
|
||||
batch = new SpriteBatch();
|
||||
debugRenderer = new ShapeRenderer();
|
||||
camera = new OrthographicCamera();
|
||||
camera.setToOrtho(false, 1, 1);
|
||||
}
|
||||
|
||||
|
||||
public void setViewport(int width, int height) {
|
||||
camera.setToOrtho(false, width, height);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void update(float deltaTime) {
|
||||
CPhysics phys = pm.get(AppUtil.player);
|
||||
@ -49,29 +57,56 @@ public class RenderingSystem extends EntitySystem {
|
||||
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
|
||||
batch.setProjectionMatrix(camera.combined);
|
||||
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));
|
||||
SpriteSequence currSequence = renderedObject.getSequence(renderedObject.getCurrentSequence());
|
||||
int currFrame = (int) Math.floor(renderedObject.getCurrentFrame());
|
||||
Sprite currSprite = currSequence.getSprite(currFrame);
|
||||
|
||||
|
||||
CPhysics physics = pm.get(entities.get(i));
|
||||
|
||||
|
||||
int spriteHeight = currSprite.getRegionHeight();
|
||||
int spriteWidth = currSprite.getRegionWidth();
|
||||
|
||||
currSprite.setPosition(physics.getPosition().x-spriteWidth/2,
|
||||
physics.getPosition().y-spriteHeight/2);
|
||||
|
||||
currSprite.setPosition(physics.getPosition().x - spriteWidth / 2,
|
||||
physics.getPosition().y - spriteHeight / 2);
|
||||
currSprite.draw(batch);
|
||||
|
||||
float nextFrame = renderedObject.getCurrentFrame() + deltaTime*currSequence.getPlayspeed();
|
||||
renderedObject.setCurrentFrame(nextFrame%currSequence.frameCount());
|
||||
}
|
||||
|
||||
float nextFrame = renderedObject.getCurrentFrame() + deltaTime * currSequence.getPlayspeed();
|
||||
renderedObject.setCurrentFrame(nextFrame % currSequence.frameCount());
|
||||
}
|
||||
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) {
|
||||
entities = engine.getEntitiesFor(Family.getFor(CRenderedObject.class, CPhysics.class));
|
||||
}
|
||||
|
||||
public boolean getDebug() {
|
||||
return this.debug;
|
||||
}
|
||||
|
||||
public void setDebug(boolean debug) {
|
||||
this.debug = debug;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -2,19 +2,21 @@ package com.saltosion.gladiator.util;
|
||||
|
||||
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 PLAYERIMG = "PLAYERIMG";
|
||||
public static final String GROUNDIMG = "GROUNDIMG";
|
||||
public static final String WALLIMG = "WALLIMG";
|
||||
public static final String DEBUG = "DEBUG";
|
||||
|
||||
public static final String MOVE_LEFT = "MOVE_LEFT";
|
||||
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";
|
||||
public static final String STATICPLAYER = "STATICPLAYER";
|
||||
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 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";
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user