Implemented moving Character
This commit is contained in:
@@ -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();
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user