You are here

Chapter 33: Showing a status display between levels

I realized that I completely forgot about requirement #30 (showing a brief summary between levels) in the previous chapter, so let's do it now.

(I decided to skip the "Next Level" button, and instead add a countdown timer before next level starts.)

We only have to edit two files to get the summary and count down timer in place;

GameStatus.java:

package com.ajomannen.justroids;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;

public class GameStatus {
  private Paint textPaint;
  private int level;
  private int thrusts;
  private int thrustsLastLevel;
  private int firedMissiles;
  private int firedMissilesLastLevel;
  private long levelStartTime;
  private long passedLevelTime;
  private long passedLevelTimeLastLevel;

  private static final long GRACE_PERIOD = 5000000000L;

  public GameStatus() {
   textPaint = new Paint();
   textPaint.setColor(Color.LTGRAY);
   textPaint.setTextSize(25);
  }

  public int getLevel() {
   return level;
  }

  public void setLevel(int level) {
   this.level = level;

   thrustsLastLevel = thrusts;
   firedMissilesLastLevel = firedMissiles;
   passedLevelTimeLastLevel = passedLevelTime;

   thrusts = 0;
   firedMissiles = 0;
   levelStartTime = System.nanoTime() + GRACE_PERIOD;
  }

  public long getPassedLevelTime() {
   return passedLevelTime;
  }

  public void increaseThrusts() {
   thrusts++;
  }

  public void increaseFiredMissiles() {
   firedMissiles++;
  }

  public void update() {
   passedLevelTime = (System.nanoTime() - levelStartTime) / 1000000000L;
  }

  public void draw(Canvas canvas, float screenHeight, float screenWidth) {
   // long passedLevelTime = (System.nanoTime() - levelStartTime) / 1000000000L;

   if (passedLevelTime > 0) {
    textPaint.setTextAlign(Paint.Align.LEFT);
    canvas.drawText("Level: " + level, 1, 19, textPaint);
    canvas.drawText("Thrusts: " + thrusts, 1, screenHeight - 2,
      textPaint);

    textPaint.setTextAlign(Paint.Align.RIGHT);
    canvas.drawText("Time: " + passedLevelTime + "s", screenWidth - 1,
      19, textPaint);
    canvas.drawText("Missiles: " + firedMissiles, screenWidth - 1,
      screenHeight - 2, textPaint);
   } else {
    textPaint.setTextAlign(Paint.Align.CENTER);
    if (level > 1) {
     canvas.drawText("Level " + (level - 1) + " completed! ",
       screenWidth / 2, 29, textPaint);
     canvas.drawText(" Time: " + passedLevelTimeLastLevel + "s",
       screenWidth / 2, 60, textPaint);
     canvas.drawText("Thrusts: " + thrustsLastLevel,
       screenWidth / 2, 90, textPaint);
     canvas.drawText("Missiles: " + firedMissilesLastLevel,
       screenWidth / 2, 120, textPaint);
    }
    textPaint.setColor(Color.LTGRAY);
    textPaint.setTextSize(30);
    canvas.drawText("Level " + level + " starts in "
      + (-passedLevelTime) + "s", screenWidth / 2,
      screenHeight - 40, textPaint);
    textPaint.setTextSize(25);
   }
  }

}

and GameEngine.java:

package com.ajomannen.justroids;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;

public class GameEngine {

  Context context;
  Resources resources;

  public float screenWidth;
  public float screenHeight;
  private Paint blackPaint;

  private AsteroidHandler asteroidHandler;
  private MissileHandler missileHandler;
  private Ship ship;
  private GameStatus gameStatus;

  public static final int EVENT_NONE = 0;
  public static final int EVENT_LEFT = 1;
  public static final int EVENT_RIGHT = 2;
  public static final int EVENT_THRUST = 3;
  public static final int EVENT_FIRE = 4;
  public int pendingEvent = EVENT_NONE;

  public void Init(Context context) {
   this.context = context;
   resources = context.getResources();

   blackPaint = new Paint();
   blackPaint.setColor(Color.BLACK);
   blackPaint.setStyle(Style.FILL);

   gameStatus = new GameStatus();
   setLevel(1);

   setSurfaceDimensions(240, 320);
  }

  private void setLevel(int level) {
   gameStatus.setLevel(level);
   asteroidHandler = new AsteroidHandler(resources, level);
   missileHandler = new MissileHandler(resources);
   ship = new Ship(resources, 120, 280, 0.1, 0, 0);
  }

  public void onDestroy() {
   try {
   } catch (Exception e) {
   }
  }

  public void setSurfaceDimensions(int width, int height) {
   screenWidth = width;
   screenHeight = height;
  }

  public void update() {
   gameStatus.update();
   if (gameStatus.getPassedLevelTime() > 0) {
    asteroidHandler.update(screenWidth, screenHeight);
    ship.update(screenWidth, screenHeight, asteroidHandler.asteroids,
      pendingEvent, gameStatus);
    missileHandler.update(screenWidth, screenHeight, pendingEvent,
      ship.x, ship.y, ship.angle, asteroidHandler.asteroids,
      gameStatus);
    pendingEvent = EVENT_NONE;

    if (asteroidHandler.asteroids.isEmpty())
     setLevel(gameStatus.getLevel() + 1);

   }
  }

  public void draw(Canvas canvas) {
   canvas.drawRect(0, 0, canvas.getWidth(), canvas.getHeight(), blackPaint);
   asteroidHandler.draw(canvas);
   missileHandler.draw(canvas);
   ship.draw(canvas);
   gameStatus.draw(canvas, screenHeight, screenWidth);
  }

}

That's it! Now we have a nice little summary between levels:

 

Requirements implemented in this chapter

✔ 30. When a level is completed, a "Next Level" link/button shall be shown along with a summary displaying how much time it took to complete the level, how many missiles that were fired and how much fuel that was used. (All these resources are unlimited in this game, but it can be fun to compete with yourself).

Theme by Danetsoft and Danang Probo Sayekti inspired by Maksimer