Android Open Source - feup-lpoo-android-tower-defense Tower Defense Game






From Project

Back to project page feup-lpoo-android-tower-defense.

License

The source code is released under:

MIT License

If you think the Android project feup-lpoo-android-tower-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 pt.up.fe.lpoo.towerdefense;
/* ww  w  . java  2  s .c  o  m*/
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;

import pt.up.fe.lpoo.framework.FileIO;
import pt.up.fe.lpoo.framework.Screen;
import pt.up.fe.lpoo.framework.implementation.AndroidGame;

import android.util.Log;

import pt.up.fe.lpoo.towerdefense.R;

/**
 * This class contains the game's generic information, loading and saving methods as well as methods inherited from
 *  the framework adapted for the development of this game
 * @author Joao Marinheiro
 * @author Luis Cleto
 * @see pt.up.fe.lpoo.framework.implementation.AndroidGame
 */
public class TowerDefenseGame extends AndroidGame {

  /** information regarding the current level being played */
  public static String level;
  private boolean firstTimeCreate = true;
  /** Number of levels currently available */
  public static final int NUM_LEVELS = 12;
  private static final String LEVEL_SCORES_FILENAME = "levelScores.tds";
  private static final String CONFIG_FILENAME = "config.tdg";
  /** path for the storage area for game files */
  public static final String APP_PATH_NAME = "Android"+File.separator+"data"
      +File.separator+"pt.up.fe.lpoo.towerdefense"+File.separator;
  /** Maximum number of lives allowed in a level */
  public static final int MAX_LIVES = 5;

  /**
   * Calls the Assets loading function when the game is created for the first time and returns the initial screen for the app.
   * 
   * @return returns a new SplashLoadingScreen
   */
  @Override
  public Screen getInitScreen() {

    if (firstTimeCreate) {
      Assets.load(this);
      firstTimeCreate = false;
    }


    return new SplashLoadingScreen(this);
  }

  /**
   * Calls the current screen's backButton function
   */
  @Override
  public void onBackPressed() {
    getCurrentScreen().backButton();
  }

  
  private static String convertStreamToString(InputStream is) {

    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();

    String line = null;
    try {
      while ((line = reader.readLine()) != null) {
        sb.append((line + "\n"));
      }
    } catch (IOException e) {
      Log.w("LOG", e.getMessage());
    } finally {
      try {
        is.close();
      } catch (IOException e) {
        Log.w("LOG", e.getMessage());
      }
    }
    return sb.toString();
  }

  /**
   * Resumes the activity using the superclass's onResume and resumes the music
   */
  @Override
  public void onResume() {
    super.onResume();

    if(MusicPlayer.currentTheme != null){
      if(!MusicPlayer.currentTheme.isPlaying())
        MusicPlayer.currentTheme.play();
    }

  }

  /**
   * Pauses the activity with the superclass's onPause method and pauses the current music
   */
  @Override
  public void onPause() {
    super.onPause();
    if(MusicPlayer.currentTheme != null)
      MusicPlayer.currentTheme.pause();
  }

  /**
   * Loads the levels' highscores in the storage file in case it exists, sets them to 0 otherwise
   * @param fio FileIO object to be used for reading the File
   */
  public static void loadLevelScores(FileIO fio) {
    try {
      InputStream levels = fio.readFile(TowerDefenseGame.LEVEL_SCORES_FILENAME);

      BufferedReader reader = new BufferedReader(new InputStreamReader(levels));

      String line = null;
      try {
        int i=0;
        while ((line = reader.readLine()) != null && i < TowerDefenseGame.NUM_LEVELS) {
          Assets.levelScores.add(Integer.parseInt(line));
          i++;
        }
      } catch (IOException e) {
        Log.w("LOG", e.getMessage());
      } finally {
        try {
          levels.close();
        } catch (IOException e) {
          Log.w("LOG", e.getMessage());
        }
      }
    } catch (IOException e) {
      Log.w("LOG", e.getMessage());
    }
    for(int i=Assets.levelScores.size(); i < TowerDefenseGame.NUM_LEVELS; i++){
      Assets.levelScores.add(0);
    }
  }
  /**
   * Loads the current language configuration of the game from file in case file exists
   * @param fio FileIO object to be used for reading the File
   */
  public static void loadConfig(FileIO fio) {
    try {
      InputStream configStr = fio.readFile(TowerDefenseGame.CONFIG_FILENAME);

      BufferedReader reader = new BufferedReader(new InputStreamReader(configStr));

      String line = null;
      try {
        line = reader.readLine();
        if(line != null)
          Messages.setLang(Integer.parseInt(line));
      } catch (IOException e) {
        Log.w("LOG", e.getMessage());
      } finally {
        try {
          configStr.close();
        } catch (IOException e) {
          Log.w("LOG", e.getMessage());
        }
      }
    } catch (IOException e) {
      Log.w("LOG", e.getMessage());
    }
  }
  
  /**
   * Saves the current levels' highschores to the level highschores file
   * @param fio FileIO object to be used for writing to the File
   */
  public static void saveLevelScores(FileIO fio){
    try {
      OutputStream writeToFile = fio.writeFile(LEVEL_SCORES_FILENAME);
      for(int i=0; i < Assets.levelScores.size(); i++){
        writeToFile.write((Integer.toString(Assets.levelScores.get(i))+'\n').getBytes());
      }
    } catch (IOException e) {
      Log.w("LOG", e.getMessage());
    }
  }
  
  /**
   * Saves the current language configuration to the configuration file
   * @param fio FileIO object to be used for writing to the File
   */
  public static void saveConfig(FileIO fio){
    try {
      OutputStream writeToFile = fio.writeFile(CONFIG_FILENAME);
      writeToFile.write((Integer.toString(Messages.getCurrentLang())).getBytes());
    } catch (IOException e) {
      Log.w("LOG", e.getMessage());
    }
  }
  
  /**
   * Loads a given level from the raw resources, storing the information in the level static string
   * 
   * @param chosenLevel number of the level to load
   */
  public void loadLevel(int chosenLevel) {
    InputStream is = null;
    if(chosenLevel < NUM_LEVELS && chosenLevel >= 0)
      is = getResources().openRawResource(R.raw.level000+chosenLevel);
    else
      is = getResources().openRawResource(R.raw.level011);
    level = convertStreamToString(is);
  }
}




Java Source Code List

pt.up.fe.lpoo.framework.Audio.java
pt.up.fe.lpoo.framework.FileIO.java
pt.up.fe.lpoo.framework.Game.java
pt.up.fe.lpoo.framework.Graphics.java
pt.up.fe.lpoo.framework.Image.java
pt.up.fe.lpoo.framework.Input.java
pt.up.fe.lpoo.framework.Music.java
pt.up.fe.lpoo.framework.Pool.java
pt.up.fe.lpoo.framework.Screen.java
pt.up.fe.lpoo.framework.Sound.java
pt.up.fe.lpoo.framework.implementation.AndroidAudio.java
pt.up.fe.lpoo.framework.implementation.AndroidFastRenderView.java
pt.up.fe.lpoo.framework.implementation.AndroidFileIO.java
pt.up.fe.lpoo.framework.implementation.AndroidGame.java
pt.up.fe.lpoo.framework.implementation.AndroidGraphics.java
pt.up.fe.lpoo.framework.implementation.AndroidImage.java
pt.up.fe.lpoo.framework.implementation.AndroidInput.java
pt.up.fe.lpoo.framework.implementation.AndroidMusic.java
pt.up.fe.lpoo.framework.implementation.AndroidSound.java
pt.up.fe.lpoo.framework.implementation.MultiTouchHandler.java
pt.up.fe.lpoo.framework.implementation.SingleTouchHandler.java
pt.up.fe.lpoo.framework.implementation.TouchHandler.java
pt.up.fe.lpoo.towerdefense.Animation.java
pt.up.fe.lpoo.towerdefense.Assets.java
pt.up.fe.lpoo.towerdefense.BarricadeDefense.java
pt.up.fe.lpoo.towerdefense.BoulderProjectile.java
pt.up.fe.lpoo.towerdefense.BoulderTower.java
pt.up.fe.lpoo.towerdefense.BuildingOption.java
pt.up.fe.lpoo.towerdefense.DefenseObject.java
pt.up.fe.lpoo.towerdefense.DescriptionObject.java
pt.up.fe.lpoo.towerdefense.Enemy.java
pt.up.fe.lpoo.towerdefense.FreezeTower.java
pt.up.fe.lpoo.towerdefense.GameElements.java
pt.up.fe.lpoo.towerdefense.GameObj.java
pt.up.fe.lpoo.towerdefense.Golem.java
pt.up.fe.lpoo.towerdefense.InstructionsScreen.java
pt.up.fe.lpoo.towerdefense.LevelScreen.java
pt.up.fe.lpoo.towerdefense.LevelSelectionScreen.java
pt.up.fe.lpoo.towerdefense.LoadingScreen.java
pt.up.fe.lpoo.towerdefense.MainMenuScreen.java
pt.up.fe.lpoo.towerdefense.Messages.java
pt.up.fe.lpoo.towerdefense.MusicPlayer.java
pt.up.fe.lpoo.towerdefense.PathCluster.java
pt.up.fe.lpoo.towerdefense.PoisonTower.java
pt.up.fe.lpoo.towerdefense.Projectile.java
pt.up.fe.lpoo.towerdefense.Skull.java
pt.up.fe.lpoo.towerdefense.Slime.java
pt.up.fe.lpoo.towerdefense.SplashLoadingScreen.java
pt.up.fe.lpoo.towerdefense.StatusEffect.java
pt.up.fe.lpoo.towerdefense.TarPoolDefense.java
pt.up.fe.lpoo.towerdefense.TextParser.java
pt.up.fe.lpoo.towerdefense.Tile.java
pt.up.fe.lpoo.towerdefense.TowerDefenseGame.java
pt.up.fe.lpoo.towerdefense.TurretProjectile.java
pt.up.fe.lpoo.towerdefense.TurretTower.java
pt.up.fe.lpoo.towerdefense.Wave.java