Package javaEffect.spacecraft

Source Code of javaEffect.spacecraft.Spacecraft

package javaEffect.spacecraft;

import java.util.ArrayList;
import java.util.zip.DataFormatException;

import javaEffect.ErrNegativeNumber;
import javaEffect.planet.Planet;
import javaEffect.weapon.Weapon;

/**
* This is the spacecraft class.
*
* @author Thibaut LOCQUET & Jack OUTRAN
* @version 0.1
*
*/
public class Spacecraft {

  protected String name;
  protected int armor;
  protected int speed;
  protected Planet currentPlanet;
  protected int cost;

  ArrayList<Weapon> weaponList;

  /**
   *
   * @param name
   *            The name of the spacecraft
   * @throws DataFormatException
   * @throws ErrNegativeNumber
   */
  public Spacecraft(String name, Planet currentPlanet, int cost)
      throws DataFormatException, ErrNegativeNumber {
    this.name = name;
    this.armor = 100;
    this.currentPlanet = currentPlanet;

    currentPlanet.addSpacecraft(this);

    this.setCost(cost);

    weaponList = new ArrayList<Weapon>();

    // generate random weapons
    int number = 0;
    int weaponsMax = (int) (Math.random() * 12);
    do {

      double rand = Math.random();
      Weapon weapon = null;

      if (0.0 <= rand && rand < 0.3)// 30%
      {
        weapon = new Weapon("M1", 60, 2000);
      } else if (0.3 <= rand && rand < 0.6)// 30%
      {
        weapon = new Weapon("M2", 80, 3000);
      } else if (0.6 <= rand && rand < 0.8)// 20%
      {
        weapon = new Weapon("M3", 90, 4000);
      } else if (0.8 <= rand && rand < 0.9)// 10%
      {
        weapon = new Weapon("M4", 110, 5000);
      } else if (0.90 <= rand && rand < 0.95)// 5%
      {
        weapon = new Weapon("M5", 150, 6000);
      } else if (0.95 <= rand && rand < 1.0)// 5%
      {
        weapon = new Weapon("M6", 200, 7000);
      }

      weaponList.add(weapon);
      number++;

    } while (number < weaponsMax);

  }

  /**
   *
   * @param name
   * @param currentPlanet
   * @param cost
   * @param armor
   * @param speed
   * @throws DataFormatException
   * @throws ErrNegativeNumber
   */
  public Spacecraft(String name, Planet currentPlanet, int cost, int armor,
      int speed) throws DataFormatException, ErrNegativeNumber {
    this(name, currentPlanet, cost);

    this.armor = armor;
    this.setSpeed(speed);
  }

  /*
   * (non-Javadoc)
   *
   * @see java.lang.Object#toString()
   */
  @Override
  public String toString() {
    return name + " [armor:" + armor + "]" + " Cost: " + cost;
  }
 
  /**
   *
   * @return The name of the ship
   */
  public String getName(){
    return this.name;
  }

  /**
   * @return the armor
   */
  public int getArmor() {
    return armor;
  }

  /**
   * improve the spacecraft armor of the value
   *
   * @param armor
   *            The level of armor to add
   *
   */
  public void improveArmor(int armor) {
    this.armor += armor;
  }

  /**
   * @return the speed
   */
  public int getSpeed() {
    return speed;
  }

  /**
   * Set the speed of the spacecraft
   *
   * @param speed
   *            The speed of the spacecraft
   */
  public void setSpeed(int speed) {
    this.speed = speed;
  }

  /**
   * @return the cost
   */
  public int getCost() {
    return cost;
  }

  /**
   * @param cost
   *            the cost to set
   * @throws ErrNegativeNumber
   */
  public void setCost(int cost) throws ErrNegativeNumber {

    if (cost < 0)
      throw new ErrNegativeNumber("The cost can't be negative!");

    this.cost = cost;
  }

  /**
   * Damages to the spacecraft
   *
   * @param damages
   *            Damages to the spacecraft
   */
  public void beAttack(int damages) {
    this.armor -= damages;

    if (this.armor < 0)
      this.armor = 0;
  }

  /**
   *
   * @return The number of weapon of the spacecraft
   */
  public int showWeaponList() {

    int i = 0;
    for (Weapon w : weaponList) {
      System.out.println("\t" + i + "->" + w.toString());
      i++;
    }

    if (weaponList.size() == 0) {
      System.out.println("\tEmpty");
    }

    return weaponList.size();

  }

  /**
   *
   * @param index
   * @return The weapon at the index index in the list of weapon
   */
  public Weapon getWeapon(int index) {

    return weaponList.get(index);
  }

  /**
   * @param currentPlanet the currentPlanet to set
   */
  public void setCurrentPlanet(Planet currentPlanet) {
    this.currentPlanet = currentPlanet;
  }

}
TOP

Related Classes of javaEffect.spacecraft.Spacecraft

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.