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.math.Vector2; import com.badlogic.gdx.math.Matrix4; import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer; import com.badlogic.gdx.physics.box2d.World; import com.badlogic.gdx.utils.TimeUtils; 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; 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 controllable Entity found engine.addEntity(com.darkrp2d.entities.Character.create(this)); //Sets inputs Gdx.input.setInputProcessor(new InputHandler(this)); } //GameLoop and debug private float accumulator = 0; private float skippedFrames = 0; private static final int UPDATES_PER_SECOND = 60; private static final float MS_PER_UPDATE = 1f / UPDATES_PER_SECOND; private static final int MAX_FRAMESKIP = 5; // Make 5 updates mostly without a render @Override // GameLoop public void render () { accumulator += Gdx.graphics.getDeltaTime(); //Seconds to milliseconds //Process Input Command currentCommand = CommandStream.pollCommand(); if(currentCommand != null) { currentCommand.execute(); } skippedFrames = 0; //Update Stuff while(accumulator >= MS_PER_UPDATE && skippedFrames <= MAX_FRAMESKIP) { accumulator -= MS_PER_UPDATE; update(MS_PER_UPDATE); skippedFrames++; } trueRender(); } public void update(float deltaTime) { world.step(deltaTime, 6, 2); engine.update(deltaTime); } public void trueRender() { Gdx.gl.glClearColor(0.f, 0.f, 0.f, 1.f); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); //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(); } }