Implemented moving Character

This commit is contained in:
Gulum
2017-10-18 01:38:21 +02:00
parent ffb5a784d6
commit 722f594633
26 changed files with 569 additions and 36 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.2 KiB

After

Width:  |  Height:  |  Size: 1.3 KiB

BIN
core/assets/logo.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 196 B

View File

@@ -1,44 +1,70 @@
package com.darkrp2d;
import com.darkrp2d.entitySystems.RenderSystem;
import com.badlogic.ashley.core.Engine;
import com.badlogic.ashley.core.Family;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.GlyphLayout;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.utils.TimeUtils;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Matrix4;
import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;
import com.badlogic.gdx.physics.box2d.World;
import com.darkrp2d.entities.components.TextureComponent;
import com.darkrp2d.entities.entityListeners.DisposeListener;
import com.darkrp2d.entitySystems.MovementSystem;
import com.darkrp2d.input.CommandStream;
import com.darkrp2d.input.InputHandler;
import com.darkrp2d.input.commands.Command;
import com.darkrp2d.physics.Box2DUtils;
public class DarkRP2D extends Game {
public BitmapFont font;
public SpriteBatch batch;
private Engine engine;
public Engine engine;
public World world;
private OrthographicCamera camera;
private Box2DDebugRenderer b2dbr;
private Matrix4 debugMatrix;
@Override
public void create () {
font = new BitmapFont();
batch = new SpriteBatch();
world = new World(new Vector2(0.f,0.f),true);
engine = new Engine();
engine.addSystem(new RenderSystem());
engine.addSystem(new MovementSystem());
engine.addEntityListener(Family.all(TextureComponent.class).get(), 0, new DisposeListener());
camera = new OrthographicCamera();
camera.setToOrtho(false, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
b2dbr = new Box2DDebugRenderer();
//must be before input registration or no character found
engine.addEntity(com.darkrp2d.entities.Character.create(this));
//Sets inputs
Gdx.input.setInputProcessor(new InputHandler(this));
}
//GameLoop and debug
private double lag = 0.;
private static final double MS_PER_UPDATE = 16.66;
private int ups = 0;
private double timer = 0.;
private static final int UPDATES_PER_SECOND = 60;
private static final double MS_PER_UPDATE = 1e3/UPDATES_PER_SECOND;
@Override
// GameLoop
public void render () {
lag += Gdx.graphics.getDeltaTime() * 1e3; //Seconds to milliseconds
timer += Gdx.graphics.getDeltaTime();
lag += Gdx.graphics.getRawDeltaTime() * 1e3; //Seconds to milliseconds
//Process Input
Command currentCommand = CommandStream.pollCommand();
@@ -48,22 +74,16 @@ public class DarkRP2D extends Game {
//Update Stuff
while(lag >= MS_PER_UPDATE) {
update();
ups++;
update(1.f/UPDATES_PER_SECOND);
lag -= MS_PER_UPDATE;
}
if(timer >= 1) {
System.out.println(ups);
ups = 0;
timer = 0.;
}
trueRender();
}
public void update() {
public void update(float deltaTime) {
world.step(deltaTime, 6, 2);
engine.update(deltaTime);
}
public void trueRender() {
@@ -73,14 +93,36 @@ public class DarkRP2D extends Game {
//Render current Screen
super.render();
//Debug
camera.update();
batch.setProjectionMatrix(camera.combined);
// Scale down the sprite batches projection matrix to box2D size
debugMatrix = batch.getProjectionMatrix().cpy().scale(Box2DUtils.PIXEL_PER_METER,
Box2DUtils.PIXEL_PER_METER, 0);
b2dbr.render(world, debugMatrix);
//TODO: will be later moved to Gamescreen
engine.getSystem(RenderSystem.class).render(batch);
//some testdrawing
batch.begin();
GlyphLayout layout = new GlyphLayout(font, "Hello World");
font.draw(batch, layout, Gdx.graphics.getWidth()/2-layout.width/2, Gdx.graphics.getHeight()/2-layout.height/2);
batch.end();
}
@Override
public void resize (int width, int height) {
//camera.setToOrtho(false, width, height);
}
@Override
public void dispose () {
b2dbr.dispose();
engine.removeAllEntities();
font.dispose();
batch.dispose();
world.dispose();
}

View File

@@ -6,7 +6,16 @@
package com.darkrp2d.entities;
import com.badlogic.ashley.core.Entity;
import com.darkrp2d.entities.components.PositionComponent;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.darkrp2d.DarkRP2D;
import com.darkrp2d.entities.components.ControllableComponent;
import com.darkrp2d.entities.components.PhysicComponent;
import com.darkrp2d.entities.components.TextureComponent;
import com.darkrp2d.physics.Box2DUtils;
/**
*
@@ -16,10 +25,25 @@ public class Character {
//Singletone
private static Entity character;
public static Entity create() {
public static Entity create(DarkRP2D game) {
if(character == null) {
float movementSpeed = 10.f;
character = new Entity();
character.add(new PositionComponent());
BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyDef.BodyType.DynamicBody;
bodyDef.position.set(0 , 0);
PolygonShape shape = new PolygonShape();
shape.setAsBox(Box2DUtils.screenToWorld(32.f), Box2DUtils.screenToWorld(32.f));
FixtureDef fixtureDef = new FixtureDef();
fixtureDef.shape = shape;
fixtureDef.density = 1f;
character.add(new PhysicComponent(game,bodyDef, fixtureDef));
shape.dispose();
character.add(new TextureComponent(new Texture(Gdx.files.internal("police_officer.png"))));
character.add(new ControllableComponent(movementSpeed));
}
return character;
}

View File

@@ -0,0 +1,26 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.darkrp2d.entities.components;
import com.badlogic.ashley.core.Component;
/**
*
* @author Julian
*/
public class ControllableComponent implements Component {
public boolean up = false;
public boolean down = false;
public boolean right = false;
public boolean left = false;
public float speed;
public ControllableComponent(float speed) {
this.speed = speed;
}
}

View File

@@ -0,0 +1,20 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.darkrp2d.entities.components;
import com.badlogic.ashley.core.ComponentMapper;
/**
*
* @author Julian
*/
public class Mappers {
public static final ComponentMapper<PhysicComponent> physicComponentMapper = ComponentMapper.getFor(PhysicComponent.class);
public static final ComponentMapper<TextureComponent> textureComponentMapper = ComponentMapper.getFor(TextureComponent.class);
public static final ComponentMapper<ControllableComponent> controllableComponentMapper = ComponentMapper.getFor(ControllableComponent.class);
}

View File

@@ -0,0 +1,33 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.darkrp2d.entities.components;
import com.badlogic.ashley.core.Component;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.darkrp2d.DarkRP2D;
/**
*
* @author Julian
*/
public class PhysicComponent implements Component {
public BodyDef bodyDef;
public FixtureDef fixtureDef;
public Body body;
public PhysicComponent(DarkRP2D game, BodyDef bodyDef, FixtureDef fixtureDef) {
this.bodyDef = bodyDef;
this.fixtureDef = fixtureDef;
body = game.world.createBody(bodyDef);
body.createFixture(fixtureDef);
}
public void dispose() {
}
}

View File

@@ -4,14 +4,23 @@
* and open the template in the editor.
*/
package com.darkrp2d.entities.components;
import com.badlogic.ashley.core.Component;
import com.badlogic.gdx.graphics.Texture;
/**
*
* @author Julian
*/
public class PositionComponent implements Component {
public float x = 0.f;
public float y = 0.f;
public class TextureComponent implements Component {
public Texture tex;
public TextureComponent(Texture tex) {
this.tex = tex;
}
public void dispose() {
tex.dispose();
}
}

View File

@@ -0,0 +1,27 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.darkrp2d.entities.entityListeners;
import com.badlogic.ashley.core.Entity;
import com.badlogic.ashley.core.EntityListener;
import com.darkrp2d.entities.components.Mappers;
/**
*
* @author Julian
*/
public class DisposeListener implements EntityListener {
@Override
public void entityAdded(Entity entity) {
}
@Override
public void entityRemoved(Entity entity) {
Mappers.textureComponentMapper.get(entity).dispose();
}
}

View File

@@ -0,0 +1,61 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.darkrp2d.entitySystems;
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.darkrp2d.entities.components.ControllableComponent;
import com.darkrp2d.entities.components.Mappers;
import com.darkrp2d.entities.components.PhysicComponent;
/**
*
* @author Julian
*/
public class MovementSystem extends EntitySystem {
private ImmutableArray<Entity> entities;
public MovementSystem() {
}
@Override
public void addedToEngine(Engine engine) {
entities = engine.getEntitiesFor(Family.all(PhysicComponent.class, ControllableComponent.class).get());
}
@Override
public void update(float deltaTime) {
for (int i = 0; i < entities.size(); ++i) {
Entity entity = entities.get(i);
ControllableComponent controller = Mappers.controllableComponentMapper.get(entity);
PhysicComponent physic = Mappers.physicComponentMapper.get(entity);
Vector2 dir = new Vector2(0,0);
if(controller.down) {
dir.y += -1;
}
if(controller.up) {
dir.y += 1;
}
if(controller.left) {
dir.x += -1;
}
if(controller.right) {
dir.x += 1;
}
physic.body.setLinearVelocity(dir.nor().setLength(controller.speed));
}
}
}

View File

@@ -0,0 +1,47 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.darkrp2d.entitySystems;
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.graphics.g2d.SpriteBatch;
import com.darkrp2d.entities.components.Mappers;
import com.darkrp2d.entities.components.PhysicComponent;
import com.darkrp2d.entities.components.TextureComponent;
import com.darkrp2d.physics.Box2DUtils;
/**
*
* @author Julian
*/
public class RenderSystem extends EntitySystem {
private ImmutableArray<Entity> entities;
public RenderSystem() {}
@Override
public void addedToEngine(Engine engine) {
entities = engine.getEntitiesFor(Family.all(TextureComponent.class, PhysicComponent.class).get());
}
public void render(SpriteBatch batch) {
batch.begin();
for (int i = 0; i < entities.size(); ++i) {
Entity entity = entities.get(i);
PhysicComponent physics = Mappers.physicComponentMapper.get(entity);
TextureComponent texture = Mappers.textureComponentMapper.get(entity);
batch.draw(texture.tex, Box2DUtils.worldToScreen(physics.body.getPosition().x) - texture.tex.getWidth() / 2, Box2DUtils.worldToScreen(physics.body.getPosition().y) - texture.tex.getHeight()/2);
}
batch.end();
}
}

View File

@@ -5,10 +5,15 @@
*/
package com.darkrp2d.input;
import com.badlogic.ashley.core.Entity;
import com.badlogic.ashley.core.Family;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.InputProcessor;
import com.darkrp2d.DarkRP2D;
import com.darkrp2d.entities.components.ControllableComponent;
import com.darkrp2d.entities.components.PhysicComponent;
import com.darkrp2d.input.commands.*;
import com.darkrp2d.input.commands.moveCommands.*;
import com.darkrp2d.scenes.menu.MenuScreen;
/**
@@ -20,24 +25,70 @@ public class InputHandler implements InputProcessor {
Command testCommand;
Command menuCommand;
//MovementCommands
Command upCommand;
Command downCommand;
Command leftCommand;
Command rightCommand;
public InputHandler(DarkRP2D game) {
testCommand = new TestCommand();
this.menuCommand = new MenuCommand(game, new MenuScreen(game));
//Inputs auf erstes bedienbares entity
Entity character = game.engine.getEntitiesFor(Family.all(ControllableComponent.class, PhysicComponent.class).get()).first();
this.upCommand = new UpCommand(character);
this.downCommand = new DownCommand(character);
this.leftCommand = new LeftCommand(character);
this.rightCommand = new RightCommand(character);
}
@Override
public boolean keyDown(int keycode) {
if(keycode == Input.Keys.Q) {
CommandStream.registerCommand(testCommand);
} else if(keycode == Input.Keys.C) {
CommandStream.registerCommand(menuCommand);
switch (keycode) {
case Input.Keys.Q:
CommandStream.registerCommand(testCommand);
break;
case Input.Keys.C:
CommandStream.registerCommand(menuCommand);
break;
case Input.Keys.W:
CommandStream.registerCommand(upCommand);
break;
case Input.Keys.S:
CommandStream.registerCommand(downCommand);
break;
case Input.Keys.A:
CommandStream.registerCommand(leftCommand);
break;
case Input.Keys.D:
CommandStream.registerCommand(rightCommand);
break;
default:
break;
}
return true;
}
@Override
public boolean keyUp(int keycode) {
return false;
switch (keycode) {
case Input.Keys.W:
CommandStream.registerCommand(upCommand);
break;
case Input.Keys.S:
CommandStream.registerCommand(downCommand);
break;
case Input.Keys.A:
CommandStream.registerCommand(leftCommand);
break;
case Input.Keys.D:
CommandStream.registerCommand(rightCommand);
break;
default:
break;
}
return true;
}
@Override

View File

@@ -0,0 +1,38 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.darkrp2d.input.commands.moveCommands;
import com.badlogic.ashley.core.Entity;
import com.darkrp2d.entities.components.ControllableComponent;
import com.darkrp2d.entities.components.Mappers;
import com.darkrp2d.input.commands.Command;
/**
*
* @author Julian
*/
public class DownCommand implements Command{
Entity ent;
private boolean toggle = false;
public DownCommand(Entity ent) {
this.ent = ent;
}
@Override
public void execute() {
ControllableComponent controller = Mappers.controllableComponentMapper.get(ent);
if(!toggle) {
controller.down = true;
toggle = true;
} else {
controller.down = false;
toggle = false;
}
}
}

View File

@@ -0,0 +1,39 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.darkrp2d.input.commands.moveCommands;
import com.badlogic.ashley.core.Entity;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.darkrp2d.entities.components.ControllableComponent;
import com.darkrp2d.entities.components.Mappers;
import com.darkrp2d.input.commands.Command;
/**
*
* @author Julian
*/
public class LeftCommand implements Command {
Entity ent;
private boolean toggle = false;
public LeftCommand(Entity ent) {
this.ent = ent;
}
@Override
public void execute() {
ControllableComponent controller = Mappers.controllableComponentMapper.get(ent);
if(!toggle) {
controller.left = true;
toggle = true;
} else {
controller.left = false;
toggle = false;
}
}
}

View File

@@ -0,0 +1,40 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.darkrp2d.input.commands.moveCommands;
import com.badlogic.ashley.core.Entity;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.darkrp2d.entities.components.ControllableComponent;
import com.darkrp2d.entities.components.Mappers;
import com.darkrp2d.input.commands.Command;
/**
*
* @author Julian
*/
public class RightCommand implements Command {
Entity ent;
private boolean toggle = false;
public RightCommand(Entity ent) {
this.ent = ent;
}
@Override
public void execute() {
ControllableComponent controller = Mappers.controllableComponentMapper.get(ent);
if(!toggle) {
controller.right = true;
toggle = true;
} else {
controller.right = false;
toggle = false;
}
}
}

View File

@@ -0,0 +1,41 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.darkrp2d.input.commands.moveCommands;
import com.badlogic.ashley.core.Entity;
import com.badlogic.gdx.physics.box2d.Body;
import com.darkrp2d.entities.components.Mappers;
import com.darkrp2d.input.commands.Command;
import com.badlogic.gdx.math.Vector2;
import com.darkrp2d.entities.components.ControllableComponent;
/**
*
* @author Julian
*/
public class UpCommand implements Command {
Entity ent;
private boolean toggle = false;
public UpCommand(Entity ent) {
this.ent = ent;
}
@Override
public void execute() {
ControllableComponent controller = Mappers.controllableComponentMapper.get(ent);
if(!toggle) {
controller.up = true;
toggle = true;
} else {
controller.up = false;
toggle = false;
}
}
}

View File

@@ -0,0 +1,25 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.darkrp2d.physics;
/**
*
* @author Julian
*/
public class Box2DUtils {
public static final float PIXEL_PER_METER = 32;
public static float screenToWorld(float value) {
return value / PIXEL_PER_METER;
}
public static float worldToScreen(float value) {
return value * PIXEL_PER_METER;
}
}

View File

@@ -5,10 +5,15 @@
*/
package com.darkrp2d.scenes.game;
import com.badlogic.gdx.graphics.OrthographicCamera;
/**
*
* @author Julian
*/
public class Camera {
public class Camera extends OrthographicCamera {
public Camera(int viewportWidth, int viewportHeight) {
new OrthographicCamera(960,540);
}
}

View File

@@ -20,7 +20,7 @@ public class MenuScreen implements Screen{
public MenuScreen(DarkRP2D game) {
this.game = game;
layout = new GlyphLayout(game.font, "Hello World");
layout = new GlyphLayout(game.font, "Menu Opened");
}
@Override