CW Network
http://forums.customwars.com/

[CW 2] Units
http://forums.customwars.com/viewtopic.php?f=109&t=8883
Page 1 of 1

Author:  stef569 [ Wed Feb 18, 2009 10:53 am ]
Post subject:  [CW 2] Units

Terrains are covered and tested, next is a unit class and everything around it.
Units are data stores, they don't contain rules, an example of rules:

When a Unit of the Air ArmyBranch has run out of supplies it crashes
Ground troops(Tanks, Artillery) can no longer move when they run out of supplies
When Naval units run out of fuel they sink to the bottom of the ocean.

Instead we store an army branch field as integer and a UnitRules class can then use that field change to determine if the unit should destroy or stop moving when out of supplies.

But how does UnitRules know that the state of the current supply level has changed? Using the Observer pattern we can inform listeners of the change so they can take an action. I've reused the PropertyChangeSupport and added it to the top object in the game GameObject. Each GameObject can notify observers of a Property aka Field change.

A unit performs some actions:
supplying, healing, capturing, attacking, defending, transporting units, move over tiles
But it does not know about it surroundings. A Unit has a Location and that's it.

supplying:
supply(Unit)
getSupplyPercentage()
getSupplyRate()
canSupply()
hasLowSupplies()

capturing:
unit doesn't know about a tile or a city so it can't capture it directly
getCaptureRate()
canCapture()

attacking/defending
attack(Unit defender, UnitFight fight)
A unit can defend himself, taking in damage + ability to counter attack
defend(Unit attacker, UnitFight fight)
receiveDamage(Unit attacker, UnitFight fight)
Weapons are used only to hold the ammo count, a unit with no weapons cannot attack.

UnitFight hides the dmg calculation, and allows to counter attack.
int calcAttackDamage()
int calcAttackDamagePercentage()
counterAttack(Unit attacker)

Each time a Turn ends the supplies 'lost' each turn are subtracted from supplies aka the daily use of the unit.

Removing a Unit
destroy()
this will garbage collect the unit because all references to it are removed.

Transporting
The unit is acting like a location you can add and remove units to/from it.

Dynamic/static fields
Dynamic fields are those that change like hp, ammo
Static fields are maxHp, maxSupplies they are never changed
Dynamic fields are put last in the declarations.

Copy Constructor and creating units
A unit contains many fields, instead of filling those in each time we need a new unit, we create a copy from a UnitFactory. Using a static method. UnitFactory.get(8) where 8 is the unitID and index in the unit image sheet.
UnitFactory is loaded up with units once from xml. The UnitFactory.get(8) method internally creates a new Unit using a copy constructor.
Units only have a constructor for tests.

move/attack zone
Each unit always has the correct zones.
When a unit moves and wait is clicked we have to recalculate all unit zones.
the reason to always store them is convenience.
You can always ask a unit if a click was within it's move zone. The downside is that units have a tiny delay when they 'wait'

Page 1 of 1 All times are UTC + 9:30 hours
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/