java.lang.Object
edu.ntnu.idatt2001.carljgu.battle.units.Unit
- Direct Known Subclasses:
CavalryUnit
,InfantryUnit
,RangedUnit
The abstract class Unit.
A unit object should not be created because all units
that are used in the simulations are subclasses of Unit.
Since it is abstract a Unit object can not be created.
- Version:
- 1.5 15.05.2022
- Author:
- Carl Gützkow
-
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionint
Method for dealing damage to another unit.int
getArmor()
Gets armor.int
Gets attack.abstract int
getAttackBonus
(Terrain terrain) Gets attack bonus.Gets the name of the class.int
Gets health.getName()
Gets name.abstract int
getResistBonus
(Terrain terrain) Gets resist bonus.void
setHealth
(int health) Sets health.toString()
Overrides toString() method from Object.
-
Field Details
-
name
-
health
private int health -
attack
private final int attack -
armor
private final int armor
-
-
Constructor Details
-
Unit
Checks validity of name and health. Health can not be less than or equal to zero. Attack and armor can not be negative, but can be equal to zero.- Parameters:
name
- String - a short name of the unit that can't be left with whitespace or empty.health
- int - HP - amount of health unit has. If it hits zero or more, the unit is dead.attack
- int - damage done, excluding attack bonus, on an enemy unit.armor
- int - damage resisted, excluding resist bonus, from an enemy unit.- Throws:
IllegalArgumentException
- thrown if name is whitespace or contains comma or if health starts at zero or less.
-
-
Method Details
-
attack
Method for dealing damage to another unit. It uses the enemy unit's attributes like health, resist bonus and armor to calculate its remaining health. If the total damage dealt is less than the armor and resistance bonus for the defender, the unit's health is unaffected.- Parameters:
unit
- Unit - enemy of any unit subclass.terrain
- Terrain - the terrain where the attack is done.- Returns:
- totalDamage - int - the amount of damage the attacker inflicted
-
getName
Gets name.- Returns:
- name - String - a short name of the unit that can't be left with whitespace or empty.
-
getClassName
Gets the name of the class.- Returns:
- class name - String - the name of the class
-
getHealth
public int getHealth()Gets health.- Returns:
- health - int - HP - amount of health unit has. If it hits zero or more, the unit is dead.
-
getAttack
public int getAttack()Gets attack.- Returns:
- attack - int - damage done, excluding attack bonus, on an enemy unit.
-
getArmor
public int getArmor()Gets armor.- Returns:
- armor - int - damage resisted, excluding resist bonus, from an enemy unit.
-
setHealth
public void setHealth(int health) Sets health.- Parameters:
health
- - int - HP - amount of health unit has. If it hits zero or more, the unit is dead.
-
toString
Overrides toString() method from Object. Example: InfantryUnit infantry with 15 health has 10 attack and 15 armor. -
getAttackBonus
Gets attack bonus. Abstract method that is to be defined in subclasses.- Parameters:
terrain
- Terrain - the terrain of the attack- Returns:
- the attack bonus
-
getResistBonus
Gets resist bonus. Abstract method that is to be defined in subclasses.- Parameters:
terrain
- Terrain - the terrain of the attack- Returns:
- the resist bonus
-