Game reuse

Right before we dive into looking at tile maps and creating a wonderful world in which Pete will live, we need to set up our project.

Once you have created the project using the LibGDX setup tool—you should be good at that by now—we will need to create a GameScreen class as before, extending the ScreenAdapter class.

Remember, we will need the following objects in our GameScreen class:

  • ShapeRenderer
  • Viewport
  • Camera
  • SpriteBatch

As this is a platformer, we are going to want a view that is more landscape than we have had before. So, we will have a world size of 640 x 480:

private static final float WORLD_WIDTH = 640;
private static final float WORLD_HEIGHT = 480;

Then, we will keep the core functionality for updating and rendering. So, hopefully, your GameScreen class looks like the following:

public class GameScreen extends ScreenAdapter {
  private static final float WORLD_WIDTH = 640;
  private static final float WORLD_HEIGHT = 480;
  private ShapeRenderer shapeRenderer;
  private Viewport viewport;
  private Camera camera;
  private SpriteBatch batch;
  private final PeteGame peteGame;
  public GameScreen(PeteGame peteGame) {
    this.peteGame = peteGame;
  }

  @Override
  public void resize(int width, int height) {
    viewport.update(width, height);
  }

  @Override
  public void show() {
    camera = new OrthographicCamera();
    camera.position.set(WORLD_WIDTH / 2, WORLD_HEIGHT / 2, 0);
    camera.update();
    viewport = new FitViewport(WORLD_WIDTH, WORLD_HEIGHT, camera);
    shapeRenderer = new ShapeRenderer();
    batch = new SpriteBatch();
  }

  @Override
  public void render(float delta) {
    update(delta);
    clearScreen();
    draw();
    drawDebug();
  }

  private void update(float delta) {
  }

  private void clearScreen() {
    Gdx.gl.glClearColor(Color.BLACK.r, Color.BLACK.g, Color.BLACK.b, Color.BLACK.a);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
  }

  private void draw() {
    batch.setProjectionMatrix(camera.projection);
    batch.setTransformMatrix(camera.view);
    batch.begin();
    batch.end();
  }

  private void drawDebug() {
    shapeRenderer.setProjectionMatrix(camera.projection);
    shapeRenderer.setTransformMatrix(camera.view);
    shapeRenderer.begin(ShapeRenderer.ShapeType.Line);
    shapeRenderer.end();
  }
}

Hopefully, the preceding code listing shouldn't now come to you as a surprise.

Next, we will need to have a loading screen in place, along with AssetManager that we now know and love.

Let's update our game class with the addition of AssetManager:

public class PeteGame extends Game {
  private final AssetManager assetManager = new AssetManager();

  @Override
  public void create() {
    setScreen(new LoadingScreen(this));
  }

  public AssetManager getAssetManager() {
    return assetManager;
  }
}

Hopefully, your IDE at this stage will be complaining that the LoadingScreen class doesn't exist, which is good, as it doesn't! Let's create it.

Our LoadingScreen class is going to be very similar to what we had before, except we will remove the Flappee Bee references. So, go ahead, create and update your LoadingScreen class.

Done? Excellent. Hopefully, it will look like the following code:

public class LoadingScreen extends ScreenAdapter {
  private static final float WORLD_WIDTH = 640;
  private static final float WORLD_HEIGHT = 480;
  private static final float PROGRESS_BAR_WIDTH = 100;
  private static final float PROGRESS_BAR_HEIGHT = 25;
  private ShapeRenderer shapeRenderer;
  private Viewport viewport;
  private OrthographicCamera
  camera;
  private float progress = 0;
  private final PeteGame peteGame;
  public LoadingScreen(PeteGame peteGame) {
    this.peteGame = peteGame;
  }

  @Override
  public void resize(int width, int height) {
    viewport.update(width, height);
  }

  @Override
  public void show() {
    camera = new OrthographicCamera();
    camera.position.set(WORLD_WIDTH / 2, WORLD_HEIGHT / 2, 0);
    camera.update();
    viewport = new FitViewport(WORLD_WIDTH, WORLD_HEIGHT, camera);
    shapeRenderer = new ShapeRenderer();
  }

  @Override
  public void render(float delta) {
    update();
    clearScreen();
    draw();
  }

  @Override
  public void dispose() {
    shapeRenderer.dispose();
  }

  private void update() {
    if (peteGame.getAssetManager().update()) {
      peteGame.setScreen(new GameScreen(peteGame));
    } else {
      progress = peteGame.getAssetManager().getProgress();
    }
  }

  private void clearScreen() {
    Gdx.gl.glClearColor(Color.BLACK.r, Color.BLACK.g, Color.BLACK.b, Color.BLACK.a);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
  }

  private void draw() {
    shapeRenderer.setProjectionMatrix(camera.projection);
    shapeRenderer.setTransformMatrix(camera.view);
    shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
    shapeRenderer.setColor(Color.WHITE);
    shapeRenderer.rect(
      (WORLD_WIDTH  - PROGRESS_BAR_WIDTH) / 2, WORLD_HEIGHT / 2 - PROGRESS_BAR_HEIGHT / 2,
      progress * PROGRESS_BAR_WIDTH, PROGRESS_BAR_HEIGHT);
    shapeRenderer.end();
  }
}

We have a nice little platform (pun intended!) to start building our game. If you run our project, you should get a nice 640x480px black square on your screen.

If so, we are good to go! If not, just revisit the previous chapters and the games we made, to see if you missed something.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.120.136