Android Open Source - Apocalypse-Defense Game Facade






From Project

Back to project page Apocalypse-Defense.

License

The source code is released under:

MIT License

If you think the Android project Apocalypse-Defense 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.apocalypsedefense.app.gameplay_visuals;
/*from   w  w w  . j  a  v  a2  s  . c om*/
import java.util.ArrayList;

import android.util.FloatMath;
import android.util.Log;

import com.apocalypsedefense.core.*;

/**
 * Simplified interface to the core game stuff.
 * @author Pat
 *
 */
public class GameFacade {
    public static final String TAG = "GameFacade";
    /**
   * Multiply by this to adjust from pixel location to game tile location.
   */
  public float xScale;
  /**
   * Multiply by this to adjust from pixel location to game tile location.
   */
  public float yScale;
  private Game game;
    private boolean isStarted;
  
  /**
   * @param screenWidth width of view
   * @param screenHeight height of view
   * @param tileWidth how many pixels should be considered as one group/block/tile (across)
   * @param tileHeight how many pixels should be considered as one group/block/tile (down)
   */
  public GameFacade(int screenWidth, int screenHeight, float tileWidth, float tileHeight){
    xScale = 1 / tileWidth;
    yScale = 1 / tileHeight;
    int width = getGameWidth(screenWidth); 
    int height = getGameHeight(screenHeight); 
    game = new Game(height, width);
  }

  public GameFacade(Game game){
    this.game = game;
  }
  
  public void start(){
    game.startNew();
        this.isStarted = true;
  }

  public void update() {
    //TODO Make sure there's only one way to pause/resume
    if(isStarted() && !isGameOver()) {
      game.update();
    }
  }

    private boolean isGameOver() {
        switch (game.getGameState()) {
            case PAUSED:
            case RUNNING:
                return false;
            case LOST:
            case WON:
                return true;
            default:
                return false;
        }
    }

    public ArrayList<GameObject> getGameObjects() {
    ArrayList<GameObject> objects = new ArrayList<GameObject>();
    //TODO: Need to lock these collections while modifying/retrieving
    for (Survivor s : game.survivors){
      objects.add(new GameObject(s, this));
    }
    for (Zombie z : game.zombies){
      objects.add(new GameObject(z, this));
    }
    return objects;
  }

  public boolean isPaused() {
    return game.getGameState() == GameState.PAUSED;
  }

  public boolean isStarted() {
    return this.isStarted;
  }

  public boolean canBuyTowerAt(int screenX, int screenY) {
        //TODO Presumption is that tower was purchasable, so don't check gold here
        if (game.gold < 10) return false; //KLUDGE
        final int gameX = (int) (screenX*xScale);
        final int gameY = (int) (screenY*yScale);
        return game.survivorManager.canBuySurvivorAt(gameX, gameY);
  }

  public void buyTowerAt(int screenX, int screenY) {
    final int gameX = (int) (screenX*xScale);
    final int gameY = (int) (screenY*yScale);
    if (game.survivorManager.canBuySurvivorAt(gameX, gameY)) {
      try {
        game.survivorManager.buySurvivorAt(gameX, gameY);
                game.gold -= 10; //TODO replace with cost obtained from particular tower
      } catch (Exception e) {
        Log.e(TAG, "exception in buySurvivorAt", e);
      }
    }
  }

  private int getGameHeight(int screenHeight) {
    return (int) FloatMath.ceil(screenHeight * yScale);
  }

  private int getGameWidth(int screenWidth) {
    return (int) FloatMath.ceil(screenWidth * xScale);
  }

  public GameData getGameData() {
    Shared.gameData = game.gameData;
        return Shared.gameData;
  }

    public GameState getGameState(){
        return game.getGameState();
    }
}




Java Source Code List

com.apocalypsedefense.app.AchievementsActivity.java
com.apocalypsedefense.app.ApocalypseDefenseActivity.java
com.apocalypsedefense.app.EndOfGameDialog.java
com.apocalypsedefense.app.ExitConfirmationDialog.java
com.apocalypsedefense.app.InGameActivity.java
com.apocalypsedefense.app.InstructionsDialog.java
com.apocalypsedefense.app.NewGameSettingsActivity.java
com.apocalypsedefense.app.OverwriteExistingGameDialog.java
com.apocalypsedefense.app.gameplay_visuals.AndroidLogAdapter.java
com.apocalypsedefense.app.gameplay_visuals.GameFacade.java
com.apocalypsedefense.app.gameplay_visuals.GameObject.java
com.apocalypsedefense.app.gameplay_visuals.GameSurfaceView.java
com.apocalypsedefense.app.gameplay_visuals.GameThread.java
com.apocalypsedefense.app.gameplay_visuals.OnGameEndListener.java
com.apocalypsedefense.app.gameplay_visuals.OnStatsChangedListener.java
com.apocalypsedefense.app.gameplay_visuals.Sprite.java
com.apocalypsedefense.core.ActorType.java
com.apocalypsedefense.core.Armor.java
com.apocalypsedefense.core.GameData.java
com.apocalypsedefense.core.GameState.java
com.apocalypsedefense.core.Game.java
com.apocalypsedefense.core.Gun.java
com.apocalypsedefense.core.LogAdapter.java
com.apocalypsedefense.core.MapElement.java
com.apocalypsedefense.core.Map.java
com.apocalypsedefense.core.Movement.java
com.apocalypsedefense.core.Person.java
com.apocalypsedefense.core.Point.java
com.apocalypsedefense.core.PrintLogAdapter.java
com.apocalypsedefense.core.Shared.java
com.apocalypsedefense.core.SurvivorManager.java
com.apocalypsedefense.core.Survivor.java
com.apocalypsedefense.core.Weapon.java
com.apocalypsedefense.core.ZombieManager.java
com.apocalypsedefense.core.Zombie.java
com.apocalypsedefense.core.testDriver.java