/**
*
*/
package javaEffect.spacecraft;
import java.util.zip.DataFormatException;
import javaEffect.ErrAttack;
import javaEffect.ErrNegativeNumber;
import javaEffect.planet.Planet;
/**
* @author jack
*
*/
public class Spaceship extends Spacecraft implements Attacking {
private int attack;
private double shield;
private int shieldEfficiency;
public Spaceship(String name, Planet currentPlanet, int cost)
throws DataFormatException, ErrNegativeNumber {
super(name, currentPlanet, cost);
this.attack = 50;
this.shieldEfficiency = 10;
this.setShield(100.0);
}
public Spaceship(String name, Planet currentPlanet, int cost, int attack,
int armor, int shieldEfficiency, int speed)
throws DataFormatException, ErrNegativeNumber {
super(name, currentPlanet, cost);
this.attack = attack;
this.shieldEfficiency = shieldEfficiency;
this.setShield(100.0);
this.speed = speed;
}
@Override
/*
* (non-Javadoc)
*
* @see javaEffect.spacecraft.Spacecraft#toString()
*/
public String toString() {
return name + " [attack:" + attack + " armor:" + armor + " shield: " + shield + "% Cost: " + cost + "]" ;
}
/*
* (non-Javadoc)
*
* @see
* javaEffect.spacecraft.attacking#attack(javaEffect.spacecraft.Spacecraft)
*/
@Override
public void attack(Spacecraft target) throws ErrAttack {
if (this.currentPlanet.equals(target.currentPlanet) == false)
throw new ErrAttack("Not on the same planet");
target.beAttack(this.attack);
}
/*
* (non-Javadoc)
*
* @see javaEffect.spacecraft.Spacecraft#beAttack(int)
*/
@Override
public void beAttack(int damages) {
int damagesLeft = 0;
if (this.getShield() < damages / this.shieldEfficiency)// if no enough
// shield
damagesLeft = (int) ((damages / this.shieldEfficiency - this
.getShield()) * this.shieldEfficiency);
this.setShield(this.shield - ((double) damages ) / this.shieldEfficiency);
this.armor -= damagesLeft;
if (this.armor < 0) {
this.armor = 0;
}
}
/*
* (non-Javadoc)
*
* @see javaEffect.spacecraft.attacking#getAttack()
*/
@Override
public int getAttack() {
return attack;
}
/*
* (non-Javadoc)
*
* @see javaEffect.spacecraft.attacking#improveAttack(int)
*/
@Override
public void improveAttack(int attack) {
this.attack += attack;
}
/*
* (non-Javadoc)
*
* @see javaEffect.spacecraft.attacking#getShield()
*/
@Override
public double getShield() {
return shield;
}
/*
* (non-Javadoc)
*
* @see javaEffect.spacecraft.attacking#improveShield(int)
*/
@Override
public void improveShield(int shield) {
this.shieldEfficiency += shield;
}
/*
* (non-Javadoc)
*
* @see javaEffect.spacecraft.attacking#setShield(double)
*/
@Override
public void setShield(double shield) {
if (shield >= 0 && shield <= 100)
this.shield = shield;
else
this.shield = 0;
}
}