Implemented moving Character
This commit is contained in:
@@ -1 +1 @@
|
|||||||
#Fri Oct 13 19:03:57 CEST 2017
|
#Tue Oct 17 00:48:27 CEST 2017
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
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
BIN
core/assets/logo.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 196 B |
@@ -1,44 +1,70 @@
|
|||||||
package com.darkrp2d;
|
package com.darkrp2d;
|
||||||
|
|
||||||
|
import com.darkrp2d.entitySystems.RenderSystem;
|
||||||
import com.badlogic.ashley.core.Engine;
|
import com.badlogic.ashley.core.Engine;
|
||||||
|
import com.badlogic.ashley.core.Family;
|
||||||
|
|
||||||
import com.badlogic.gdx.Game;
|
import com.badlogic.gdx.Game;
|
||||||
import com.badlogic.gdx.Gdx;
|
import com.badlogic.gdx.Gdx;
|
||||||
import com.badlogic.gdx.graphics.GL20;
|
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.BitmapFont;
|
||||||
import com.badlogic.gdx.graphics.g2d.GlyphLayout;
|
import com.badlogic.gdx.graphics.g2d.GlyphLayout;
|
||||||
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
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.CommandStream;
|
||||||
import com.darkrp2d.input.InputHandler;
|
import com.darkrp2d.input.InputHandler;
|
||||||
import com.darkrp2d.input.commands.Command;
|
import com.darkrp2d.input.commands.Command;
|
||||||
|
import com.darkrp2d.physics.Box2DUtils;
|
||||||
|
|
||||||
|
|
||||||
public class DarkRP2D extends Game {
|
public class DarkRP2D extends Game {
|
||||||
|
|
||||||
public BitmapFont font;
|
public BitmapFont font;
|
||||||
public SpriteBatch batch;
|
public SpriteBatch batch;
|
||||||
private Engine engine;
|
public Engine engine;
|
||||||
|
public World world;
|
||||||
|
|
||||||
|
private OrthographicCamera camera;
|
||||||
|
private Box2DDebugRenderer b2dbr;
|
||||||
|
private Matrix4 debugMatrix;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void create () {
|
public void create () {
|
||||||
font = new BitmapFont();
|
font = new BitmapFont();
|
||||||
batch = new SpriteBatch();
|
batch = new SpriteBatch();
|
||||||
|
world = new World(new Vector2(0.f,0.f),true);
|
||||||
engine = new Engine();
|
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
|
//Sets inputs
|
||||||
Gdx.input.setInputProcessor(new InputHandler(this));
|
Gdx.input.setInputProcessor(new InputHandler(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//GameLoop and debug
|
||||||
private double lag = 0.;
|
private double lag = 0.;
|
||||||
private static final double MS_PER_UPDATE = 16.66;
|
private static final int UPDATES_PER_SECOND = 60;
|
||||||
|
private static final double MS_PER_UPDATE = 1e3/UPDATES_PER_SECOND;
|
||||||
private int ups = 0;
|
|
||||||
private double timer = 0.;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
// GameLoop
|
// GameLoop
|
||||||
public void render () {
|
public void render () {
|
||||||
lag += Gdx.graphics.getDeltaTime() * 1e3; //Seconds to milliseconds
|
lag += Gdx.graphics.getRawDeltaTime() * 1e3; //Seconds to milliseconds
|
||||||
timer += Gdx.graphics.getDeltaTime();
|
|
||||||
|
|
||||||
//Process Input
|
//Process Input
|
||||||
Command currentCommand = CommandStream.pollCommand();
|
Command currentCommand = CommandStream.pollCommand();
|
||||||
@@ -48,22 +74,16 @@ public class DarkRP2D extends Game {
|
|||||||
|
|
||||||
//Update Stuff
|
//Update Stuff
|
||||||
while(lag >= MS_PER_UPDATE) {
|
while(lag >= MS_PER_UPDATE) {
|
||||||
update();
|
update(1.f/UPDATES_PER_SECOND);
|
||||||
ups++;
|
|
||||||
lag -= MS_PER_UPDATE;
|
lag -= MS_PER_UPDATE;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(timer >= 1) {
|
|
||||||
System.out.println(ups);
|
|
||||||
ups = 0;
|
|
||||||
timer = 0.;
|
|
||||||
}
|
|
||||||
|
|
||||||
trueRender();
|
trueRender();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void update() {
|
public void update(float deltaTime) {
|
||||||
|
world.step(deltaTime, 6, 2);
|
||||||
|
engine.update(deltaTime);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void trueRender() {
|
public void trueRender() {
|
||||||
@@ -73,14 +93,36 @@ public class DarkRP2D extends Game {
|
|||||||
//Render current Screen
|
//Render current Screen
|
||||||
super.render();
|
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();
|
batch.begin();
|
||||||
GlyphLayout layout = new GlyphLayout(font, "Hello World");
|
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);
|
font.draw(batch, layout, Gdx.graphics.getWidth()/2-layout.width/2, Gdx.graphics.getHeight()/2-layout.height/2);
|
||||||
batch.end();
|
batch.end();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void resize (int width, int height) {
|
||||||
|
//camera.setToOrtho(false, width, height);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void dispose () {
|
public void dispose () {
|
||||||
|
b2dbr.dispose();
|
||||||
|
engine.removeAllEntities();
|
||||||
|
font.dispose();
|
||||||
|
batch.dispose();
|
||||||
|
world.dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,16 @@
|
|||||||
package com.darkrp2d.entities;
|
package com.darkrp2d.entities;
|
||||||
|
|
||||||
import com.badlogic.ashley.core.Entity;
|
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
|
//Singletone
|
||||||
private static Entity character;
|
private static Entity character;
|
||||||
public static Entity create() {
|
public static Entity create(DarkRP2D game) {
|
||||||
if(character == null) {
|
if(character == null) {
|
||||||
|
float movementSpeed = 10.f;
|
||||||
character = new Entity();
|
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;
|
return character;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
20
core/src/com/darkrp2d/entities/components/Mappers.java
Normal file
20
core/src/com/darkrp2d/entities/components/Mappers.java
Normal 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);
|
||||||
|
|
||||||
|
}
|
||||||
@@ -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() {
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,14 +4,23 @@
|
|||||||
* and open the template in the editor.
|
* and open the template in the editor.
|
||||||
*/
|
*/
|
||||||
package com.darkrp2d.entities.components;
|
package com.darkrp2d.entities.components;
|
||||||
|
|
||||||
import com.badlogic.ashley.core.Component;
|
import com.badlogic.ashley.core.Component;
|
||||||
|
import com.badlogic.gdx.graphics.Texture;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @author Julian
|
* @author Julian
|
||||||
*/
|
*/
|
||||||
public class PositionComponent implements Component {
|
public class TextureComponent implements Component {
|
||||||
public float x = 0.f;
|
|
||||||
public float y = 0.f;
|
public Texture tex;
|
||||||
|
|
||||||
|
public TextureComponent(Texture tex) {
|
||||||
|
this.tex = tex;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void dispose() {
|
||||||
|
tex.dispose();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
61
core/src/com/darkrp2d/entitySystems/MovementSystem.java
Normal file
61
core/src/com/darkrp2d/entitySystems/MovementSystem.java
Normal 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));
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
47
core/src/com/darkrp2d/entitySystems/RenderSystem.java
Normal file
47
core/src/com/darkrp2d/entitySystems/RenderSystem.java
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -5,10 +5,15 @@
|
|||||||
*/
|
*/
|
||||||
package com.darkrp2d.input;
|
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.Input;
|
||||||
import com.badlogic.gdx.InputProcessor;
|
import com.badlogic.gdx.InputProcessor;
|
||||||
import com.darkrp2d.DarkRP2D;
|
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.*;
|
||||||
|
import com.darkrp2d.input.commands.moveCommands.*;
|
||||||
import com.darkrp2d.scenes.menu.MenuScreen;
|
import com.darkrp2d.scenes.menu.MenuScreen;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -20,24 +25,70 @@ public class InputHandler implements InputProcessor {
|
|||||||
Command testCommand;
|
Command testCommand;
|
||||||
Command menuCommand;
|
Command menuCommand;
|
||||||
|
|
||||||
|
//MovementCommands
|
||||||
|
Command upCommand;
|
||||||
|
Command downCommand;
|
||||||
|
Command leftCommand;
|
||||||
|
Command rightCommand;
|
||||||
|
|
||||||
public InputHandler(DarkRP2D game) {
|
public InputHandler(DarkRP2D game) {
|
||||||
testCommand = new TestCommand();
|
testCommand = new TestCommand();
|
||||||
this.menuCommand = new MenuCommand(game, new MenuScreen(game));
|
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
|
@Override
|
||||||
public boolean keyDown(int keycode) {
|
public boolean keyDown(int keycode) {
|
||||||
if(keycode == Input.Keys.Q) {
|
switch (keycode) {
|
||||||
CommandStream.registerCommand(testCommand);
|
case Input.Keys.Q:
|
||||||
} else if(keycode == Input.Keys.C) {
|
CommandStream.registerCommand(testCommand);
|
||||||
CommandStream.registerCommand(menuCommand);
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean keyUp(int keycode) {
|
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
|
@Override
|
||||||
|
|||||||
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
25
core/src/com/darkrp2d/physics/Box2DUtils.java
Normal file
25
core/src/com/darkrp2d/physics/Box2DUtils.java
Normal 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -5,10 +5,15 @@
|
|||||||
*/
|
*/
|
||||||
package com.darkrp2d.scenes.game;
|
package com.darkrp2d.scenes.game;
|
||||||
|
|
||||||
|
import com.badlogic.gdx.graphics.OrthographicCamera;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @author Julian
|
* @author Julian
|
||||||
*/
|
*/
|
||||||
public class Camera {
|
public class Camera extends OrthographicCamera {
|
||||||
|
|
||||||
|
public Camera(int viewportWidth, int viewportHeight) {
|
||||||
|
new OrthographicCamera(960,540);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ public class MenuScreen implements Screen{
|
|||||||
|
|
||||||
public MenuScreen(DarkRP2D game) {
|
public MenuScreen(DarkRP2D game) {
|
||||||
this.game = game;
|
this.game = game;
|
||||||
layout = new GlyphLayout(game.font, "Hello World");
|
layout = new GlyphLayout(game.font, "Menu Opened");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package com.darkrp2d.desktop;
|
package com.darkrp2d.desktop;
|
||||||
|
|
||||||
import com.badlogic.gdx.Gdx;
|
import com.badlogic.gdx.Gdx;
|
||||||
|
import com.badlogic.gdx.Files;
|
||||||
import com.badlogic.gdx.Preferences;
|
import com.badlogic.gdx.Preferences;
|
||||||
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
|
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
|
||||||
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
|
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
|
||||||
@@ -17,8 +18,12 @@ public class DesktopLauncher {
|
|||||||
//Default conf
|
//Default conf
|
||||||
config.title = "DarkRP2D";
|
config.title = "DarkRP2D";
|
||||||
config.useGL30 = false;
|
config.useGL30 = false;
|
||||||
config.width = 800;
|
config.width = 960;
|
||||||
config.height = 600;
|
config.height = 540;
|
||||||
|
config.addIcon("logo.png", Files.FileType.Internal);
|
||||||
|
config.vSyncEnabled = false; // Setting to false disables vertical sync
|
||||||
|
config.foregroundFPS = 144; // Setting to 0 disables foreground fps throttling
|
||||||
|
config.backgroundFPS = 0; // Setting to 0 disables background fps throttling
|
||||||
|
|
||||||
loadUserPrefs(new LwjglApplication(new DarkRP2D(), config));
|
loadUserPrefs(new LwjglApplication(new DarkRP2D(), config));
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user