Android Open Source - spacegunner Game Presenter Impl






From Project

Back to project page spacegunner.

License

The source code is released under:

MIT License

If you think the Android project spacegunner listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.example.spacegunner.game;
//  w  w  w  .j a va2 s .c o  m
import static com.example.spacegunner.constants.Constants.TAG;

import java.util.Random;

import android.os.Handler;
import android.util.Log;

public class GamePresenterImpl implements GamePresenter, Runnable {

  private GameModel model;
  private GameView view;
  private Handler handler;
  private Random random;
  private static final int THOUSAND_MS = 1000;
  private static final int TIME_BETWEEN_FRAMES = 50;
  private static final float DIFFICULTY_MODIFIER = 1.5f;

  public GamePresenterImpl(GameView view, int level, int points) {
    super();
    this.view = view;
    this.model = new GameModelImpl(level, points);
    this.handler = new Handler();
    this.random = new Random();
    startNextLevel();
  }

  @Override
  public void startNextLevel() {
    this.model.startNextLevel();
    this.view.setBackgroundImageAndCredits(this.model.getLevel());
    Log.d(TAG, "Started level: " + model);
    this.view.showLevelStartInfo(this.model.getLevel());
    refreshScreen();
    this.handler.postDelayed(this, TIME_BETWEEN_FRAMES);
  }

  @Override
  public void run() {
    this.view.moveShipsToNewLocation(this.model.getSpeedModifier());
    this.view.increaseFrame();
    if (this.view.getFrame() >= THOUSAND_MS / TIME_BETWEEN_FRAMES) {
      countDown();
      this.view.resetFrame();
    }
    if (this.model.isGameOver()) {
      gameOver();
    } else if (this.model.isLevelFinished()) {
      levelFinished();
    } else {
      handler.postDelayed(this, TIME_BETWEEN_FRAMES);
    }
  }

  @Override
  public void countDown() {
    this.model.countdownTime();
    final float randomValue = this.random.nextFloat();
    // display 50 per cent more ships than necessary
    final double probability = this.model.getShipsToDestroy()
        * DIFFICULTY_MODIFIER / this.model.getSecondsPerLevel();
    // if the probability is above 1, two ships might have to be displayed
    if (probability > 1) {
      this.view.displayShip();
      if (randomValue < probability - 1) {
        this.view.displayShip();
      }
    } else if (randomValue < probability) {
      this.view.displayShip();
    }
    this.view.removeShips(this.model.getMaximumTimeShown());
    refreshScreen();
  }

  @Override
  public void refreshScreen() {
    this.view.setHits(this.model.getShipsDestroyed());
    this.view.setPoints(this.model.getPoints());
    this.view.setLevel(this.model.getLevel());
    this.view.setTime(this.model.getTime());
    this.view.setHitBarWidth(this.model.getShipsDestroyed(),
        this.model.getShipsToDestroy());
    this.view.setTimeBarWidth(this.model.getTime(),
        this.model.getSecondsPerLevel());
  }

  @Override
  public void shipDestroyed() {
    this.model.shipDestroyed();
    this.view.playExplosionSound();
    this.view.showShipDestroyedAnimation();
    refreshScreen();
  }

  @Override
  public void levelFinished() {
    handler.removeCallbacks(this);
    this.view.startLevelView(this.model.getLevel(), this.model.getPoints());
  }

  @Override
  public void backButtonPressed() {
    returnToPreviousScreen();
  }

  @Override
  public void gamePaused() {
    handler.removeCallbacks(this);
  }

  @Override
  public void gameOver() {
    Log.d(TAG, "Game over: " + this.model);
    this.view.startGameResultView(this.model.getPoints());
  }

  private void returnToPreviousScreen() {
    handler.removeCallbacks(this);
    final int currentLevel = this.model.getLevel();
    if (currentLevel == this.model.getFirstLevel()) {
      this.view.startMainView();
    } else {
      this.view.returnToPreviousLevelView(currentLevel - 1,
          this.model.getPointsAtLevelStart());
    }
  }
}




Java Source Code List

com.example.spacegunner.constants.Constants.java
com.example.spacegunner.game.GameModelImpl.java
com.example.spacegunner.game.GameModel.java
com.example.spacegunner.game.GamePresenterImpl.java
com.example.spacegunner.game.GamePresenter.java
com.example.spacegunner.game.GameViewImpl.java
com.example.spacegunner.game.GameView.java
com.example.spacegunner.gameresult.GameResultModelImpl.java
com.example.spacegunner.gameresult.GameResultModel.java
com.example.spacegunner.gameresult.GameResultPresenterImpl.java
com.example.spacegunner.gameresult.GameResultPresenter.java
com.example.spacegunner.gameresult.GameResultViewImpl.java
com.example.spacegunner.gameresult.GameResultView.java
com.example.spacegunner.ioservice.IOService.java
com.example.spacegunner.ioservice.PlayerHighscore.java
com.example.spacegunner.level.LevelModelImpl.java
com.example.spacegunner.level.LevelModel.java
com.example.spacegunner.level.LevelPresenterImpl.java
com.example.spacegunner.level.LevelPresenter.java
com.example.spacegunner.level.LevelViewImpl.java
com.example.spacegunner.level.LevelView.java
com.example.spacegunner.main.MainPresenterImpl.java
com.example.spacegunner.main.MainPresenter.java
com.example.spacegunner.main.MainViewImpl.java
com.example.spacegunner.main.MainView.java