java.lang.Object
edu.ntnu.idatt2001.carljgu.battle.units.Unit
Direct Known Subclasses:
CavalryUnit, InfantryUnit, RangedUnit

public abstract class Unit extends Object
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 Details

    • name

      private final String name
    • health

      private int health
    • attack

      private final int attack
    • armor

      private final int armor
  • Constructor Details

    • Unit

      public Unit(String name, int health, int attack, int armor) throws IllegalArgumentException
      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

      public int attack(Unit unit, Terrain terrain)
      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

      public String getName()
      Gets name.
      Returns:
      name - String - a short name of the unit that can't be left with whitespace or empty.
    • getClassName

      public String 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

      public String toString()
      Overrides toString() method from Object. Example: InfantryUnit infantry with 15 health has 10 attack and 15 armor.
      Overrides:
      toString in class Object
      Returns:
      String - string representation of object
    • getAttackBonus

      public abstract int getAttackBonus(Terrain terrain)
      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

      public abstract int getResistBonus(Terrain terrain)
      Gets resist bonus. Abstract method that is to be defined in subclasses.
      Parameters:
      terrain - Terrain - the terrain of the attack
      Returns:
      the resist bonus