Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
Tags

libGDX

Finally, let's modify the GameScreen !

Here is the GameScreen.java :

public GameScreen(final MyGdxGame game){
        this.game= game;

        camera = new OrthographicCamera();
        camera.setToOrtho(false, GameConstants.SCREEN_WIDTH, GameConstants.SCREEN_HEIGHT);
        camera.update();  
        
        world = new World(new Vector2(0, GameConstants.GRAVITY), true);
        World.setVelocityThreshold(0.0f);
        debugRenderer = new Box2DDebugRenderer();
        
        tiledMap = new TmxMapLoader().load("Levels/Level 1.tmx");
        tiledMapRenderer = new OrthogonalTiledMapRendererWithSprites(tiledMap, GameConstants.MPP, game.batch);

        mapReader = new TiledMapReader(game, tiledMap, world, camera); 
    }

    @Override
    public void render(float delta) {  
        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        camera.update();
        
        world.step(GameConstants.BOX_STEP, GameConstants.BOX_VELOCITY_ITERATIONS, GameConstants.BOX_POSITION_ITERATIONS); 
        debugRenderer.render(world, camera.combined);    

        mapReader.hero.displacement();
    }
}
  • In the constructor I create the camera, with the size I define in the GameConstants.java, then I create the World. The line World.setVelocityThreshold(0.0f); allows object to move very slowly. This is not mandatory, but I like it that way. Then I create a debugRenderer, because I still have no graphics, so the debugRenderer is the only way to visualize our work. Finally, I create, the tiledMap, the tiledMapRenderer and the TiledMapReader.

Notice that the tiledMapRenderer is an OrthogonalTiledMapRendererWithSprites that I modified to take into account the Box2D conversion factor. The code will follow next.

  • In the render(), we need to clear the screen and update the camera, then run the Box2D simulation. The we run the debugRenderer to display the Box2D bodies, and finally we run the displacement() method to control the hero with the keyboard.

Here is the result when we run the game ! Pretty fun hu ?


Here is the OrthogonalTiledMapRendererWithSprites.java :

public class OrthogonalTiledMapRendererWithSprites  extends OrthogonalTiledMapRenderer{
    float unitScale = 1;
    
    public OrthogonalTiledMapRendererWithSprites(TiledMap map, Batch batch) {
        super(map, batch);
    }

    //Constructor that takes into account the Box2D scale (MPP) to render the sprites at the same size as the BOx2D bodies
    public OrthogonalTiledMapRendererWithSprites (TiledMap map, float unitScale, Batch batch) {
        super(map, unitScale, batch);
        
        this.unitScale = unitScale;
    }

    //Proceduraly draw the sprites we'll add to an object layer in our tiled map
    @Override
    public void renderObject(MapObject object) {
        if(object instanceof TextureMapObject) {
            TextureMapObject textureObj = (TextureMapObject) object;
            
            batch.draw(textureObj.getTextureRegion(), 
                        textureObj.getX(), 
                        textureObj.getY(), 
                        textureObj.getTextureRegion().getRegionWidth() * unitScale, 
                        textureObj.getTextureRegion().getRegionHeight() * unitScale);
        }
    }
}

That's all for today !