Android Open Source - project2 Player






From Project

Back to project page project2.

License

The source code is released under:

MIT License

If you think the Android project project2 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 team2.scdm;
//from   w  w  w  .  j a  v a  2  s  . com
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;
import java.util.Scanner;

import android.content.Context;
import android.util.Log;


public class Player {

  private String name;
  private Armor armor;
  private Weapon weapon;
  private int gender; 
  private int exp; 
  private int USD; 
  private int health; 
  private int lives; 
  private int lat;
  private int lon;
  
  private Context context;
  private File file;
  
  private final static int STARTINGHEALTH = 4000; 
  private final static int STARTINGLIVES = 3; 
  
  
  /**
   * 
   * @param name
   * @param gender
   * @throws FileNotFoundException 
   */
  public Player(String name, int gender, Context context)  {
    // If file exists:
    this.context = context;
    if (file != null) {
      Scanner scan;
      try {
        scan = new Scanner(file);
        name = scan.next();
        armor = new Armor(scan.nextInt());
        weapon = new Weapon(scan.nextInt());
        gender = scan.nextInt();
        exp = scan.nextInt();
        USD = scan.nextInt();
        health = scan.nextInt();
        lives = scan.nextInt();
        Log.e("file successfully read", "file successfully read");
      } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block

        Log.e("its not easy", "this time");
      }
      
    }
    // If file does not exist: 
    else {
      this.name = name; 
      this.gender = gender; // 0 for M, 1 for F, 2 for O
      exp = 0; 
      USD = 0; 
      health = STARTINGHEALTH; 
      lives = STARTINGLIVES;
      weapon = new Weapon(Weapon.WOODEN_SWORD); // should start with wooden sword
      armor = new Armor(Armor.CLOTHES); // should start wearing clothes
      Log.e("file created", "file created");
      dataToFile();
    }
  }
  
  /**
   * 
   * @return
   */
  public String getName() {
    return name; 
  }
  
  /**
   * 
   * @return
   */
  public Weapon getWeapon() {
    return weapon; 
  }
  
  /**
   * 
   * @return
   */
  public Armor getArmor() {
    return armor; 
  }
  
  /**
   * 
   * @return
   */
  public int getGender() {
    return gender; 
  }
  
  /**
   * 
   * @return
   */
  public int getExp() {
    return exp; 
  }
  
  /**
   * 
   * @return
   */
  public int getUSD() {
    return USD; 
  }
  
  public void setUSD(int amount) {
    USD = amount;
    dataToFile();
  }
  
  /**
   * 
   * @return
   */
  public int getHealth() {
    return health; 
  }
  
  /**
   * 
   * @return
   */
  public int getLives() {
    return lives; 
  }
  
  /**
   * The player is being hurt by something. 
   * @param painAmt
   * The amount the player is getting hurt. 
   * @return
   * Returns true if they die and false if they don't die
   */
  public boolean getHurt(int painAmt) {
    if(armor != null && armor.endure()) {
      health -= painAmt; 
      if (!isAlive()) {
        loseBattle(); 
        dataToFile();
        return true; 
      }
    }
    dataToFile();
    return false; 
  }
  
  /**
   * The player wins the battle. 
   * @param addExp
   * The amount of experience the player gains. 
   * @param addUSD
   * The amount of USD the player gains. 
   */
  public void winBattle(int addExp, int addUSD) {
    exp += addExp; 
    USD += addUSD;
    dataToFile();
  }
  
  /**
   * The player loses the battle. 
   */
  public void loseBattle() {
    lives--; 
    if (lives < 0) { // There are no extra lives left
      lives = STARTINGLIVES; 
      USD = 0;
      weapon = new Weapon(Weapon.WOODEN_SWORD);
      armor = new Armor(Armor.CLOTHES);
    }
    dataToFile();
  }
  
  /**
   * Makes the string for the stored data
   * @return
   *   the string of data for the file
   * 
   */
  private void dataToFile() {
    String data = name + " " + armor.getValue() + " " + weapon.getValue() + " " + gender + " " + exp + " " + USD + " " + health + " " + lives;
      try {
        if (file != null) {
          file.delete();
        }
          String fileName = "SovietDanceMix.txt";

          file = File.createTempFile(fileName, null, context.getCacheDir());
          PrintWriter out = new PrintWriter(file);
          out.println(data);
          out.close();
          
          // file = new File(this.getCacheDir() + fileName);
          
      }
      catch (IOException e) {
          // Error while creating file
      }

      }
  
  public boolean isAlive() {
    if (health <= 0) {
      return false;
    }
    return true;
  }
  
  // TODO: Implement Folk.tradeItem
  public void trade(Folk folk) {
    Random rand = new Random();
    int smalls = rand.nextInt(2);
    
    if (smalls % 2 == 0) {
      // armor = folk.tradeItem();
    }
    else {
      // weapon = folk.tradeItem();
    }    
  }
}




Java Source Code List

team2.scdm.AboutActivity.java
team2.scdm.Armor.java
team2.scdm.Assets.java
team2.scdm.Audio.java
team2.scdm.BattleActivity.java
team2.scdm.Enemy.java
team2.scdm.Folk.java
team2.scdm.GameAudio.java
team2.scdm.GameMusic.java
team2.scdm.GameOverActivity.java
team2.scdm.GameSound.java
team2.scdm.GestureListener.java
team2.scdm.Intro1.java
team2.scdm.Intro2.java
team2.scdm.Intro3.java
team2.scdm.Intro4.java
team2.scdm.Intro5.java
team2.scdm.Intro6.java
team2.scdm.InventoryActivity.java
team2.scdm.Item.java
team2.scdm.LocationMapActivity.java
team2.scdm.Media.java
team2.scdm.MenuActivity.java
team2.scdm.NPC.java
team2.scdm.NameActivity.java
team2.scdm.Player.java
team2.scdm.SettingsActivity.java
team2.scdm.Sound.java
team2.scdm.TitleActivity.java
team2.scdm.Weapon.java