Class UnitFactory
java.lang.Object
edu.ntnu.idatt2001.carljgu.battle.units.UnitFactory
A class to simply and elegantly create units.
Either one by one or many at once. Uses string
instead of enums to directly insert the
unit type from the file reader. In addition,
the exception is handled in the factory.
- Version:
- 1.3 13.04.2022
- Author:
- Carl Gützkow
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptioncreateListOfUnits
(int amount, UnitType unitType, String name, int health) Returns a list of units.createUnit
(UnitType unitType, String name, int health) Creates a unit between InfantryUnit, RangedUnit, CavalryUnit or CommanderUnit.deepCopyBasicUnits
(List<Unit> units) Deep copies units, but ignores overridden attack and armor points.
-
Constructor Details
-
UnitFactory
public UnitFactory()
-
-
Method Details
-
createUnit
Creates a unit between InfantryUnit, RangedUnit, CavalryUnit or CommanderUnit. If the unit type could not be found, it will return null instead.- Parameters:
unitType
- UnitType - the type of unit to create.name
- String - name of the created unit.health
- int - amount of health the unit starts width- Returns:
- unit - Unit - an instance of specialized unit with name and health specified
- Throws:
IllegalArgumentException
- thrown if the unit could not be created or if the type of unit does not exist.
-
createListOfUnits
public List<Unit> createListOfUnits(int amount, UnitType unitType, String name, int health) throws IllegalArgumentException Returns a list of units. Uses the createUnit method to create a list of "amount" units with the same name and health.- Parameters:
amount
- int - amount of units to create.unitType
- UnitType - the type of unit to create.name
- String - name of the created unit.health
- int - amount of health the unit starts width- Returns:
- units - List - a list of units with the equal attributes.
- Throws:
IllegalArgumentException
- thrown if amount is a negative number, if unit could not be created or if unit type does not exist.
-
deepCopyBasicUnits
Deep copies units, but ignores overridden attack and armor points.- Parameters:
units
- List - a list of units.- Returns:
- new_units - List - a deep copied list of all units
- Throws:
IllegalArgumentException
- thrown if any units could not be created or if unit type does not exist.
-