netwar.game
Class Unit

java.lang.Object
  |
  +--netwar.game.GameObject
        |
        +--netwar.game.Unit
Direct Known Subclasses:
UnitDefault, UnitDefault2

public abstract class Unit
extends GameObject

Abstract class which defines a mobile GameObject. This provides default implementation of the following behaviors:
Guard mode in which the Unit scans for nearby enemies.
Move mode in which the Unit tries to reach a particular Hex.
Pursuit mode in which the Unit moves toward a particular GameObject, up to a range less than the weapon range, and fires repeatedly if that GameObject is a damagable object which is not part of the same team.
Receiving damage using the standard health/armor model.
Navigating obstacles using a simple alternate direction system.

Author:
Group N2 - Project Netwar, Daniel Grund

Field Summary
protected  int f
          A code number indicating the direction that the Unit is facing
protected  int health
          A value indicating the amount of damage still needed to destroy this Unit.
protected  boolean reserved
          True if the Unit has a Hex reserved to move into.
protected  int resF
          The facing this Unit should have when moving into the reserved Hex.
protected  int resX
          X part of the Hex coord reserved for this Unit to move into another Hex.
protected  int resY
          Y part of the Hex coord reserved for this Unit to move into another Hex.
 
Fields inherited from class netwar.game.GameObject
action, firstSSMDS, frame, GameObjects, goalX, goalY, mode, myID, myPlayer, reload, target, targetSSMDS, vr, x, y
 
Constructor Summary
Unit()
           
 
Method Summary
protected  boolean aim()
          Make applicable adjustments, then return true if the target is within the firing arc.
protected abstract  void animateMove()
          Do the processing for a frame of animation while moving forward.
protected abstract  void animateRotateLeft()
          Do the processing for a frame of animation while rotating left.
protected abstract  void animateRotateRight()
          Do the processing for a frame of animation while rotating right.
protected abstract  int armor()
          Returns the number of damage points to be ignored from each hit.
protected  int bestDirection(int gx, int gy)
          Returns the facing code for the 'best' direction to head.
protected  void confirmSubPlan(int gx, int gy)
          Checks to make sure that the idealized direction to get to the destination is still good.
 boolean damageable()
          Return True if the Unit can be damaged.
abstract  int followRange()
          Returns the number of Hexes to get within if in Pursuit Mode.
protected abstract  int framesToMove()
          Returns the number of frames needed to move forward one hex.
protected abstract  int framesToRotate()
          Returns the number of frames needed to rotate one hex-side.
 boolean isDead()
          Return True iff the Unit is already destroyed.
protected abstract  int maxHealth()
          Returns The maximum value of health for this Unit, which is the initial value for health.
protected  void param(int p)
          Sets the facing of the Unit when it is created
 void recieveDamage(int dam)
          Applies damage to this Unit, possibly destroying it.
 void setGoal()
          Sets the unit to Guard Mode
 void setGoal(GameObject u)
          Sets the Unit to Pursuit Mode
 void setGoal(int gx, int gy)
          Sets the unit to Move Mode
protected  void think()
          Think about what to do.
protected  void update()
          Called once per time-step to perform necessary processing.
 
Methods inherited from class netwar.game.GameObject
animateDie, animateMake, center, createVectors, draw, fire, framesToDie, framesToMake, getHeight, getID, getMinimapColor, getObjectWithID, getPlayer, getWidth, locate, newGameObject, removeMe, scan, scanRangeSquared, updateAll, weaponDelay, weaponRangeSquared
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

health

protected int health
A value indicating the amount of damage still needed to destroy this Unit.

f

protected int f
A code number indicating the direction that the Unit is facing

resX

protected int resX
X part of the Hex coord reserved for this Unit to move into another Hex.

resY

protected int resY
Y part of the Hex coord reserved for this Unit to move into another Hex.

resF

protected int resF
The facing this Unit should have when moving into the reserved Hex.

reserved

protected boolean reserved
True if the Unit has a Hex reserved to move into.
Constructor Detail

Unit

public Unit()
Method Detail

param

protected void param(int p)
Sets the facing of the Unit when it is created
Overrides:
param in class GameObject

setGoal

public void setGoal()
Sets the unit to Guard Mode
Overrides:
setGoal in class GameObject

setGoal

public void setGoal(int gx,
                    int gy)
Sets the unit to Move Mode
Overrides:
setGoal in class GameObject
Parameters:
gx - The X part of the Hex coordinate to move to.
gy - The Y part of the Hex coordinate to move to.

setGoal

public void setGoal(GameObject u)
Sets the Unit to Pursuit Mode
Overrides:
setGoal in class GameObject
Parameters:
u - The GameObject to approach/pursue and attack (if not an ally).

update

protected void update()
Called once per time-step to perform necessary processing. Provides default logic to determine what animation is in place, fire a weapon if possible, etc.
Overrides:
update in class GameObject

bestDirection

protected int bestDirection(int gx,
                            int gy)
Returns the facing code for the 'best' direction to head. Assumes the Unit is going from its current location to (gx, gy) with no obstacles in between. Other code will adjust for obstacles.
Parameters:
gx - The x part of the destination hex coordinate.
gy - The y part of the destination hex coordinate.
Returns:
The idealized facing code.

confirmSubPlan

protected void confirmSubPlan(int gx,
                              int gy)
Checks to make sure that the idealized direction to get to the destination is still good. If the previously calculated best direction would no longer cause the Unit to get closer to its destination, a flag is set to calculate a new direction the next time the direction is checked.
Parameters:
gx - The (updated) x part of the goal hex coordinate.
gy - The (updated) y part of the goal hex coordinate.

framesToMove

protected abstract int framesToMove()
Returns the number of frames needed to move forward one hex.
Returns:
How many frames it takes to move one hex.

framesToRotate

protected abstract int framesToRotate()
Returns the number of frames needed to rotate one hex-side.
Returns:
How many frames it takes to rotate sixty degrees.

animateMove

protected abstract void animateMove()
Do the processing for a frame of animation while moving forward.

animateRotateLeft

protected abstract void animateRotateLeft()
Do the processing for a frame of animation while rotating left.

animateRotateRight

protected abstract void animateRotateRight()
Do the processing for a frame of animation while rotating right.

think

protected void think()
Think about what to do. Called by Update() when the Unit is sitting around, or just finished doing something (such as moving one hex or rotating one hex-side). Default version scans for enemies if in Guard Mode (changing to Pursuit Mode if one is detected), then figures out a direction to move if it has a destination.

followRange

public abstract int followRange()
Returns the number of Hexes to get within if in Pursuit Mode. As long as the Unit is this close to its target, it will remain stationary and fire. This allows long range Units to abuse their range advantage by staying back while firing.
Returns:
The range to approach to, in hexes.

maxHealth

protected abstract int maxHealth()
Returns The maximum value of health for this Unit, which is the initial value for health.
Returns:
The amount of damage that must be taken to destroy the Unit.

armor

protected abstract int armor()
Returns the number of damage points to be ignored from each hit. Under standard health/armor rules, a unit cannot take less than 1 damage per hit.
Returns:
The armor value of the unit.

recieveDamage

public void recieveDamage(int dam)
Applies damage to this Unit, possibly destroying it. This version uses the standard health/armor rules:
A unit starts with a fixed amount of health and cannot exceed that amount.
A unit has a (fixed ?) amount of armor.
When a unit is hit by an attack, it loses health equal to the damage minus the armor, but a minimum of one.
The unit is destroyed if it drops below 1 health.
Overrides:
recieveDamage in class GameObject
Following copied from class: netwar.game.GameObject
Parameters:
dam - The number of damage points to be inflicted.

aim

protected boolean aim()
Make applicable adjustments, then return true if the target is within the firing arc. The default version assumes that the Unit has a 120-degree firing arc, centered at the last hex-side it was facing, and the Unit does not have an independant turret. If the unit has an independant turret, it should override aim, and use aim to rotate the turret.
Overrides:
aim in class GameObject
Returns:
True if the target is in the firing arc.

isDead

public boolean isDead()
Return True iff the Unit is already destroyed. Destuction occurs when health drops below 1.
Overrides:
isDead in class GameObject
Returns:
True if healt <= 0.

damageable

public boolean damageable()
Return True if the Unit can be damaged. The Unit can be damaged if it is not yet destroyed.
Overrides:
damageable in class GameObject
Returns:
True if health > 0.