Android Open Source - DeathRally Player






From Project

Back to project page DeathRally.

License

The source code is released under:

GNU General Public License

If you think the Android project DeathRally 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

/*
 * Copyright (c) 2012 Johannes Vestlund <Johannes@westlundarna.se>
 * //  ww w.j  a va2s.c o  m
 * This file is part of DeathRally
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by 
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even implied warranty of
 * MERCHANTABILITY or FITTNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

package project.gamedev.deathrally.game.model;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.io.StreamCorruptedException;

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

public class Player implements Serializable{
  /**
   * Generated serialVersion
   */
  private static final int initialMoney = 10000;
  private static final long serialVersionUID = 6336819303006304934L;
  private static final String defaultName = "Anonymous";
  private String name;
  private Vehicle vehicle;
  /**
   * Standard amount of money, may be overridden by constructor
   */
  private int money = initialMoney;
  
  /**
   * Standard constructor for initializing completely new player. If
   * name is invalid it defaults to "Anonymous"
   * @param name Name of player. 
   */
  public Player(String name) {
    setName(name);
  }
  
  /**
   * Constructor for initializing saved player (or directly feed
   * in player with more flexibility for test or cheat purpose).
   * Invalid name defaults to "Anonymous".
   * @param name Name of player.
   * @param money Amount of money player have
   * @param vehicle The vehicle player owns.
   */
  public Player(String name, int money, Vehicle vehicle){
    setName(name);
    this.money = money;
    this.vehicle = vehicle;
  }
  
  /**
   * 
   * @return The amount of money player have at the moment
   */
  public int getMoney(){
    return money;
  }
  
  /**
   * 
   * @param sum
   */
  public void addMoney(int sum){
    money += sum;
  }
  
  /**
   * Attempt execute a payment. Will return false if player 
   * cannot pay
   * @param sum to be payed
   * @return True if player money > sum.
   */
  public boolean pay(int sum){
    if(money > sum){
      money = money - sum;
    }else{
      return false;
    }
    return true;
  }
  
  public String getName(){
    return name;
  }
  
  public void setVehicle(Vehicle vehicle) {
    this.vehicle = vehicle;
  }
  
  public Vehicle getVehicle() {
    return vehicle;
  }
  
  public boolean hasVehicle(){
    return vehicle != null;
  }
  
  public String toString(){
    return Player.class.getSimpleName() + ": " + getName();
  }
  
  public static String getDefaultName(){
    return defaultName;
  }
  
  private void setName(String name){
    if(isName(name)){
      this.name = name;
    }else{
      this.name = defaultName;
    }
  }
  
  private boolean isName(String name){
    if(name == null){
      return false;
    }
    if(name.isEmpty()){
      return false;
    }
    return true;
  }
  
  public boolean save(Context context){
    String SAVENAME = name + ".txt";
    try {
      ObjectOutputStream OOS = new ObjectOutputStream(
          context.openFileOutput(SAVENAME, Context.MODE_PRIVATE));
      OOS.writeObject(this);
      OOS.close();
      return true;
    } catch (FileNotFoundException e) {
      Log.d("Player", "File not found.");
      e.printStackTrace();
    } catch (IOException e) {
      Log.d("Player", "IOException");
      e.printStackTrace();
    }
    return false;
  }
  
  public static Player load(Context context, String SAVENAME){
    if(!SAVENAME.contains(".txt")){
      SAVENAME = SAVENAME+".txt";
    }
    try {
      ObjectInputStream OIS = new ObjectInputStream(context.openFileInput(SAVENAME));
      try {
        Player p = (Player) OIS.readObject();
        return p;
      } catch (ClassNotFoundException e) {
        Log.d("OIS", "ClassNotFound");
        e.printStackTrace();
      }
    } catch (StreamCorruptedException e) {
      Log.d("OIS", "StreamCorrupted");
      e.printStackTrace();
    } catch (FileNotFoundException e) {
      Log.d("OIS", "FileNotFound: "+SAVENAME);
      e.printStackTrace();
    } catch (IOException e) {
      Log.d("OIS", "IOException");
      e.printStackTrace();
    }
    return null;
  }
  
  public static boolean delete(String name, Context c){
    c.deleteFile(name+".txt");
    return false;
  }
}




Java Source Code List

project.gamedev.deathrally.MainActivity.java
project.gamedev.deathrally.game.DeathRallyGame.java
project.gamedev.deathrally.game.GameLoop.java
project.gamedev.deathrally.game.GameModel.java
project.gamedev.deathrally.game.MainGamePanel.java
project.gamedev.deathrally.game.constants.LevelName.java
project.gamedev.deathrally.game.controls.Joystick.java
project.gamedev.deathrally.game.graphics.Circle.java
project.gamedev.deathrally.game.graphics.Mesh.java
project.gamedev.deathrally.game.graphics.Rectangle.java
project.gamedev.deathrally.game.graphics.Triangle.java
project.gamedev.deathrally.game.model.CollisionEvent.java
project.gamedev.deathrally.game.model.Direction.java
project.gamedev.deathrally.game.model.Entity.java
project.gamedev.deathrally.game.model.GameAction.java
project.gamedev.deathrally.game.model.Hitbox.java
project.gamedev.deathrally.game.model.Level.java
project.gamedev.deathrally.game.model.MovableEntity.java
project.gamedev.deathrally.game.model.Player.java
project.gamedev.deathrally.game.model.Players.java
project.gamedev.deathrally.game.model.Position.java
project.gamedev.deathrally.game.model.Vector2D.java
project.gamedev.deathrally.game.model.VehicleName.java
project.gamedev.deathrally.game.model.VehicleType.java
project.gamedev.deathrally.game.model.Vehicle.java
project.gamedev.deathrally.game.view.GameRenderer.java
project.gamedev.deathrally.game.view.JoystickView.java
project.gamedev.deathrally.game.view.VisualEntity.java
project.gamedev.deathrally.game.view.VisualVehicle.java