Added input handling, movement and camera now follows the player
This commit is contained in:
parent
16c21698b8
commit
ef4117672c
@ -3,7 +3,6 @@ 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.EntitySystem;
|
||||
import com.badlogic.ashley.core.Family;
|
||||
import com.badlogic.gdx.ApplicationAdapter;
|
||||
import com.badlogic.gdx.Gdx;
|
||||
@ -18,21 +17,26 @@ import com.badlogic.gdx.physics.box2d.PolygonShape;
|
||||
import com.badlogic.gdx.physics.box2d.World;
|
||||
import com.saltosion.gladiator.components.CPhysics;
|
||||
import com.saltosion.gladiator.components.CRenderedObject;
|
||||
import com.saltosion.gladiator.input.InputHandler;
|
||||
import com.saltosion.gladiator.systems.MovementSystem;
|
||||
import com.saltosion.gladiator.systems.RenderingSystem;
|
||||
import com.saltosion.gladiator.util.Global;
|
||||
import com.saltosion.gladiator.util.AppUtil;
|
||||
import com.saltosion.gladiator.util.Name;
|
||||
import com.saltosion.gladiator.util.SpriteLoader;
|
||||
import com.saltosion.gladiator.util.SpriteSequence;
|
||||
|
||||
public class GladiatorBrawler extends ApplicationAdapter {
|
||||
|
||||
private Engine engine;
|
||||
private World world;
|
||||
private float physics_accumulator = 0f;
|
||||
private InputHandler inputHandler;
|
||||
|
||||
private float physics_accumulator = 0f;
|
||||
|
||||
private Entity player;
|
||||
|
||||
@Override
|
||||
public void create () {
|
||||
public void create () {
|
||||
// Initializing Physics
|
||||
|
||||
world = new World(new Vector2(0, -10), true);
|
||||
@ -42,19 +46,24 @@ public class GladiatorBrawler extends ApplicationAdapter {
|
||||
engine = new Engine();
|
||||
|
||||
engine.addSystem(new RenderingSystem(world));
|
||||
engine.addSystem(new MovementSystem());
|
||||
|
||||
engine.addEntityListener(Family.getFor(),
|
||||
new EntityListener() {
|
||||
@Override
|
||||
public void entityRemoved(Entity entity) {
|
||||
RenderingSystem rs = engine.getSystem(RenderingSystem.class);
|
||||
rs.updateEntities(engine);
|
||||
rs.updateEntities(engine);
|
||||
MovementSystem ms = engine.getSystem(MovementSystem.class);
|
||||
ms.updateEntities(engine);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void entityAdded(Entity entity) {
|
||||
RenderingSystem rs = engine.getSystem(RenderingSystem.class);
|
||||
rs.updateEntities(engine);
|
||||
MovementSystem ms = engine.getSystem(MovementSystem.class);
|
||||
ms.updateEntities(engine);
|
||||
|
||||
}
|
||||
});
|
||||
@ -65,6 +74,10 @@ public class GladiatorBrawler extends ApplicationAdapter {
|
||||
initializePlayer();
|
||||
initializeTerrain();
|
||||
|
||||
// Initialize input
|
||||
|
||||
inputHandler = new InputHandler();
|
||||
Gdx.input.setInputProcessor(inputHandler);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -76,9 +89,9 @@ public class GladiatorBrawler extends ApplicationAdapter {
|
||||
private void physicsStep(float deltaTime) {
|
||||
float frameTime = Math.max(deltaTime, 0.25f);
|
||||
physics_accumulator += frameTime;
|
||||
if (physics_accumulator >= Global.PHYSICS_TIMESTEP) {
|
||||
world.step(Global.PHYSICS_TIMESTEP, 6, 2);
|
||||
physics_accumulator -= Global.PHYSICS_TIMESTEP;
|
||||
if (physics_accumulator >= AppUtil.PHYSICS_TIMESTEP) {
|
||||
world.step(AppUtil.PHYSICS_TIMESTEP, 6, 2);
|
||||
physics_accumulator -= AppUtil.PHYSICS_TIMESTEP;
|
||||
}
|
||||
}
|
||||
|
||||
@ -86,24 +99,35 @@ public class GladiatorBrawler extends ApplicationAdapter {
|
||||
BodyDef bodyDef = new BodyDef();
|
||||
bodyDef.type = BodyType.DynamicBody;
|
||||
bodyDef.position.set(5, 5);
|
||||
bodyDef.fixedRotation = true;
|
||||
Body body = world.createBody(bodyDef);
|
||||
|
||||
PolygonShape bottomBox = new PolygonShape();
|
||||
bottomBox.setAsBox(1.95f, .5f, new Vector2(0, -1.5f), 0);
|
||||
|
||||
PolygonShape box = new PolygonShape();
|
||||
box.setAsBox(2, 2);
|
||||
FixtureDef bottomDef = new FixtureDef();
|
||||
bottomDef.shape = bottomBox;
|
||||
bottomDef.density = 75f;
|
||||
bottomDef.friction = 7f;
|
||||
Fixture bottom = body.createFixture(bottomDef);
|
||||
|
||||
FixtureDef fixtureDef = new FixtureDef();
|
||||
fixtureDef.shape = box;
|
||||
fixtureDef.density = 0.5f;
|
||||
fixtureDef.friction = 0.4f;
|
||||
Fixture fixture = body.createFixture(fixtureDef);
|
||||
PolygonShape topBox = new PolygonShape();
|
||||
topBox.setAsBox(2, 1.5f, new Vector2(0, .5f), 0);
|
||||
|
||||
box.dispose();
|
||||
FixtureDef topDef = new FixtureDef();
|
||||
topDef.shape = topBox;
|
||||
topDef.density = 75f;
|
||||
topDef.friction = 0f;
|
||||
Fixture top = body.createFixture(topDef);
|
||||
|
||||
bottomBox.dispose();
|
||||
topBox.dispose();
|
||||
|
||||
player = new Entity();
|
||||
|
||||
CRenderedObject renderedObject = new CRenderedObject();
|
||||
Sprite player1 = SpriteLoader.loadSprite(Global.PLAYERIMG, 0, 0, 64, 64);
|
||||
Sprite player2 = SpriteLoader.loadSprite(Global.PLAYERIMG, 1, 0, 64, 64);
|
||||
Sprite player1 = SpriteLoader.loadSprite(Name.PLAYERIMG, 0, 0, 64, 64);
|
||||
Sprite player2 = SpriteLoader.loadSprite(Name.PLAYERIMG, 1, 0, 64, 64);
|
||||
SpriteSequence sequence = new SpriteSequence(1).addSprite(player1).addSprite(player2);
|
||||
renderedObject.addSequence("Idle", sequence);
|
||||
renderedObject.setCurrentSequence("Idle");
|
||||
@ -112,6 +136,8 @@ public class GladiatorBrawler extends ApplicationAdapter {
|
||||
player.getComponent(CPhysics.class).body = body;
|
||||
|
||||
engine.addEntity(player);
|
||||
|
||||
AppUtil.player = player;
|
||||
}
|
||||
|
||||
public void initializeTerrain() {
|
||||
@ -128,6 +154,6 @@ public class GladiatorBrawler extends ApplicationAdapter {
|
||||
super.resize(width, height);
|
||||
RenderingSystem rs = engine.getSystem(RenderingSystem.class);
|
||||
float aspectratio = ((float)width)/((float)height);
|
||||
rs.setViewport((int)(Global.VPHEIGHT_CONST*aspectratio), Global.VPHEIGHT_CONST);
|
||||
rs.setViewport((int)(AppUtil.VPHEIGHT_CONST*aspectratio), AppUtil.VPHEIGHT_CONST);
|
||||
}
|
||||
}
|
||||
|
@ -7,6 +7,9 @@ import com.badlogic.gdx.physics.box2d.Body;
|
||||
public class CPhysics extends Component {
|
||||
|
||||
public Body body;
|
||||
|
||||
public Vector2 velocity = new Vector2(0, 0);
|
||||
|
||||
public boolean movingLeft = false;
|
||||
public boolean movingRight = false;
|
||||
|
||||
}
|
||||
|
@ -4,7 +4,7 @@ import java.util.HashMap;
|
||||
|
||||
import com.badlogic.ashley.core.Component;
|
||||
import com.badlogic.gdx.graphics.g2d.Sprite;
|
||||
import com.saltosion.gladiator.SpriteSequence;
|
||||
import com.saltosion.gladiator.util.SpriteSequence;
|
||||
|
||||
public class CRenderedObject extends Component {
|
||||
private HashMap<String, SpriteSequence> spritesequences = new HashMap<String, SpriteSequence>();
|
||||
|
23
core/src/com/saltosion/gladiator/input/IRMoveLeft.java
Normal file
23
core/src/com/saltosion/gladiator/input/IRMoveLeft.java
Normal file
@ -0,0 +1,23 @@
|
||||
package com.saltosion.gladiator.input;
|
||||
|
||||
import com.badlogic.ashley.core.Family;
|
||||
import com.badlogic.gdx.math.Vector2;
|
||||
import com.saltosion.gladiator.components.CPhysics;
|
||||
import com.saltosion.gladiator.util.AppUtil;
|
||||
|
||||
public class IRMoveLeft implements InputReceiver {
|
||||
|
||||
@Override
|
||||
public boolean pressed() {
|
||||
CPhysics physics = AppUtil.player.getComponent(CPhysics.class);
|
||||
physics.movingLeft = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean released() {
|
||||
CPhysics physics = AppUtil.player.getComponent(CPhysics.class);
|
||||
physics.movingLeft = false;
|
||||
return false;
|
||||
}
|
||||
}
|
23
core/src/com/saltosion/gladiator/input/IRMoveRight.java
Normal file
23
core/src/com/saltosion/gladiator/input/IRMoveRight.java
Normal file
@ -0,0 +1,23 @@
|
||||
package com.saltosion.gladiator.input;
|
||||
|
||||
import com.badlogic.ashley.core.Family;
|
||||
import com.badlogic.gdx.math.Vector2;
|
||||
import com.saltosion.gladiator.components.CPhysics;
|
||||
import com.saltosion.gladiator.util.AppUtil;
|
||||
|
||||
public class IRMoveRight implements InputReceiver {
|
||||
|
||||
@Override
|
||||
public boolean pressed() {
|
||||
CPhysics physics = AppUtil.player.getComponent(CPhysics.class);
|
||||
physics.movingRight = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean released() {
|
||||
CPhysics physics = AppUtil.player.getComponent(CPhysics.class);
|
||||
physics.movingRight = false;
|
||||
return false;
|
||||
}
|
||||
}
|
66
core/src/com/saltosion/gladiator/input/InputHandler.java
Normal file
66
core/src/com/saltosion/gladiator/input/InputHandler.java
Normal file
@ -0,0 +1,66 @@
|
||||
package com.saltosion.gladiator.input;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
import com.badlogic.gdx.Input.Keys;
|
||||
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() {
|
||||
keys.put(Keys.A, Name.MOVE_LEFT);
|
||||
keys.put(Keys.D, Name.MOVE_RIGHT);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean keyDown(int keycode) {
|
||||
if (!keys.containsKey(keycode)) {
|
||||
return false;
|
||||
}
|
||||
String actionName = keys.get(keycode);
|
||||
return InputReceivers.getReceiver(actionName).pressed();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean keyUp(int keycode) {
|
||||
if (!keys.containsKey(keycode)) {
|
||||
return false;
|
||||
}
|
||||
String actionName = keys.get(keycode);
|
||||
return InputReceivers.getReceiver(actionName).released();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean keyTyped(char character) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean touchDragged(int screenX, int screenY, int pointer) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean mouseMoved(int screenX, int screenY) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean scrolled(int amount) {
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,6 @@
|
||||
package com.saltosion.gladiator.input;
|
||||
|
||||
public interface InputReceiver {
|
||||
public boolean pressed();
|
||||
public boolean released();
|
||||
}
|
18
core/src/com/saltosion/gladiator/input/InputReceivers.java
Normal file
18
core/src/com/saltosion/gladiator/input/InputReceivers.java
Normal file
@ -0,0 +1,18 @@
|
||||
package com.saltosion.gladiator.input;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
import com.saltosion.gladiator.util.Name;
|
||||
|
||||
public class InputReceivers {
|
||||
public static HashMap<String, InputReceiver> inputreceivers = new HashMap<String, InputReceiver>();
|
||||
|
||||
static {
|
||||
inputreceivers.put(Name.MOVE_LEFT, new IRMoveLeft());
|
||||
inputreceivers.put(Name.MOVE_RIGHT, new IRMoveRight());
|
||||
}
|
||||
|
||||
public static InputReceiver getReceiver(String key) {
|
||||
return inputreceivers.get(key);
|
||||
}
|
||||
}
|
60
core/src/com/saltosion/gladiator/systems/MovementSystem.java
Normal file
60
core/src/com/saltosion/gladiator/systems/MovementSystem.java
Normal file
@ -0,0 +1,60 @@
|
||||
package com.saltosion.gladiator.systems;
|
||||
|
||||
import com.badlogic.ashley.core.ComponentMapper;
|
||||
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.math.Vector2;
|
||||
import com.saltosion.gladiator.components.CPhysics;
|
||||
|
||||
public class MovementSystem extends EntitySystem {
|
||||
|
||||
private ComponentMapper<CPhysics> pm = ComponentMapper.getFor(CPhysics.class);
|
||||
private ImmutableArray<Entity> entities;
|
||||
|
||||
private final Vector2 RIGHT = new Vector2(1, 0);
|
||||
private final float MOVEMENT_SPEED = 850;
|
||||
private final float MAX_MOVEMENT_SPEED = 10;
|
||||
|
||||
private Vector2 newVel = new Vector2(0, 0);
|
||||
private Vector2 bodyVel = new Vector2(0, 0);
|
||||
|
||||
@Override
|
||||
public void addedToEngine(Engine engine) {
|
||||
updateEntities(engine);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(float deltaTime) {
|
||||
for (int i=0; i<entities.size(); i++) {
|
||||
Entity e = entities.get(i);
|
||||
CPhysics physics = pm.get(e);
|
||||
|
||||
newVel.setZero();
|
||||
if (physics.movingRight) {
|
||||
newVel.add(RIGHT);
|
||||
}
|
||||
if (physics.movingLeft) {
|
||||
newVel.add(RIGHT).scl(-1);
|
||||
}
|
||||
newVel.scl(MOVEMENT_SPEED);
|
||||
bodyVel.set(physics.body.getLinearVelocity());
|
||||
bodyVel.y = 0;
|
||||
newVel.sub(bodyVel.scl(50));
|
||||
System.out.println(newVel);
|
||||
physics.body.applyLinearImpulse(newVel, physics.body.getPosition(), true);
|
||||
/**
|
||||
if (Math.abs(bodyVel.x) < MAX_MOVEMENT_SPEED) {
|
||||
physics.body.applyLinearImpulse(newVel, physics.body.getPosition(), true);
|
||||
}
|
||||
*/
|
||||
}
|
||||
}
|
||||
|
||||
public void updateEntities(Engine engine) {
|
||||
entities = engine.getEntitiesFor(Family.getFor(CPhysics.class));
|
||||
}
|
||||
|
||||
}
|
@ -14,11 +14,11 @@ import com.badlogic.gdx.graphics.g2d.Sprite;
|
||||
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
||||
import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;
|
||||
import com.badlogic.gdx.physics.box2d.World;
|
||||
import com.saltosion.gladiator.SpriteSequence;
|
||||
import com.saltosion.gladiator.components.CPhysics;
|
||||
import com.saltosion.gladiator.components.CRenderedObject;
|
||||
import com.saltosion.gladiator.util.Global;
|
||||
import com.saltosion.gladiator.util.AppUtil;
|
||||
import com.saltosion.gladiator.util.SpriteLoader;
|
||||
import com.saltosion.gladiator.util.SpriteSequence;
|
||||
|
||||
public class RenderingSystem extends EntitySystem {
|
||||
|
||||
@ -53,6 +53,9 @@ public class RenderingSystem extends EntitySystem {
|
||||
|
||||
@Override
|
||||
public void update(float deltaTime) {
|
||||
CPhysics phys = pm.get(AppUtil.player);
|
||||
camera.position.set(phys.body.getPosition().x, phys.body.getPosition().y, 0);
|
||||
camera.update();
|
||||
|
||||
Gdx.gl.glClearColor(0, 0, 0, 0);
|
||||
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
|
||||
|
12
core/src/com/saltosion/gladiator/util/AppUtil.java
Normal file
12
core/src/com/saltosion/gladiator/util/AppUtil.java
Normal file
@ -0,0 +1,12 @@
|
||||
package com.saltosion.gladiator.util;
|
||||
|
||||
import com.badlogic.ashley.core.Entity;
|
||||
|
||||
public class AppUtil {
|
||||
|
||||
public static Entity player;
|
||||
|
||||
public static final int VPHEIGHT_CONST = 14;
|
||||
public static final float PHYSICS_TIMESTEP = 1/45f;
|
||||
|
||||
}
|
13
core/src/com/saltosion/gladiator/util/Name.java
Normal file
13
core/src/com/saltosion/gladiator/util/Name.java
Normal file
@ -0,0 +1,13 @@
|
||||
package com.saltosion.gladiator.util;
|
||||
|
||||
public class Name {
|
||||
|
||||
public static final String GAME_NAME = "Gladiator Brawl";
|
||||
|
||||
public static final String STATICPLAYER = "STATICPLAYER";
|
||||
public static final String PLAYERIMG = "PLAYERIMG";
|
||||
|
||||
public static final String MOVE_LEFT = "MOVE_LEFT";
|
||||
public static final String MOVE_RIGHT = "MOVE_RIGHT";
|
||||
|
||||
}
|
@ -12,8 +12,8 @@ public class SpriteLoader {
|
||||
public static HashMap<String, Texture> textures = new HashMap<String, Texture>();
|
||||
|
||||
static {
|
||||
loadTexture(Global.STATICPLAYER, "sprites/staticplayer.png");
|
||||
loadTexture(Global.PLAYERIMG, "sprites/player/player.png");
|
||||
loadTexture(Name.STATICPLAYER, "sprites/staticplayer.png");
|
||||
loadTexture(Name.PLAYERIMG, "sprites/player/player.png");
|
||||
}
|
||||
|
||||
/**
|
||||
|
44
core/src/com/saltosion/gladiator/util/SpriteSequence.java
Normal file
44
core/src/com/saltosion/gladiator/util/SpriteSequence.java
Normal file
@ -0,0 +1,44 @@
|
||||
package com.saltosion.gladiator.util;
|
||||
|
||||
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) {
|
||||
this.defaultPlayspeed = playspeed;
|
||||
if (sprites != null) {
|
||||
this.sprites = sprites;
|
||||
}
|
||||
}
|
||||
|
||||
public SpriteSequence addSprite(Sprite s) {
|
||||
sprites.add(s);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Sprite getSprite(int index) {
|
||||
return sprites.get(index);
|
||||
}
|
||||
|
||||
public float getPlayspeed() {
|
||||
return defaultPlayspeed;
|
||||
}
|
||||
|
||||
public int frameCount() {
|
||||
return sprites.size();
|
||||
}
|
||||
}
|
@ -3,12 +3,12 @@ package com.saltosion.gladiator.desktop;
|
||||
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
|
||||
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
|
||||
import com.saltosion.gladiator.GladiatorBrawler;
|
||||
import com.saltosion.gladiator.util.Global;
|
||||
import com.saltosion.gladiator.util.Name;
|
||||
|
||||
public class DesktopLauncher {
|
||||
public static void main (String[] arg) {
|
||||
LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
|
||||
config.title = Global.GAME_NAME;
|
||||
config.title = Name.GAME_NAME;
|
||||
config.width = 1280;
|
||||
config.height = 720;
|
||||
new LwjglApplication(new GladiatorBrawler(), config);
|
||||
|
Loading…
Reference in New Issue
Block a user