gecco.server.core
Class Unit

java.lang.Object
  |
  +--gecco.server.core.Unit
All Implemented Interfaces:
Comparable
Direct Known Subclasses:
GameEventPoint, WarUnit

public abstract class Unit
extends Object
implements Comparable

This class represents a unit in the game, and must be subclassed by every game implementation. Most of the attributes is set up in configuration files, and the things to be done for the game programmer is to initialize all properties in a constructor, using the method setProperty.

Author:
Henrik Bäärnhielm, Karl Hasselström, Henrik Hägerström

Field Summary
protected  Action activeAction
          This is the action that the unit is currently executing.
protected  HashSet blockedActions
          Contains all Actions that have been blocked, that is, have been stopped during execution for some reason.
protected  String commander
          The name of the role that commands this unit, that is, the role that has the right to execute actions with the unit.
protected  int handle
          The handle identifies the unit during communication with clients.
protected  int intX
          The x-coordinate of the unit's position as an integer.
protected  int intY
          The y-coordinate of the unit's position as an integer.
protected  boolean isDestroyed
          Flag to indicate if the unit is destroyed.
protected  String name
          The name of the unit appears in the client, and is set up in a configuration file.
protected  HashSet observers
          The set of observers contains the names of all roles that has observing rights on this unit, that is, that sees everything this unit sees.
protected  TypedProperties properties
          Container for the properties of the unit.
protected  String type
          The type of the unit appears in the client, and is set up in a configuration file.
protected  UnitType unitType
          An instance of the unit type of this unit.
protected  HashSet updatedActions
          ???
protected  HashMap updatedProperties
          Contains all properties that have been updated during the current tick, and the new values.
protected  double x
          The x-coordinate of the unit's position.
protected  double y
          The y-coordinate of the unit's position.
 
Constructor Summary
Unit()
          Constructor for the Unit object
 
Method Summary
 void abortAllActions()
           
 void addBlockedAction(String actionName)
          Block an action.
 void addObserver(String role)
          Adds a new observer of the unit.
 boolean canSeeAutomatonAtPosition(int _x, int _y)
          Determines if the unit can see a specific automaton, that is, if the given position is inside the units visibility range.
 int compareTo(Object o)
          Compares the unit to a given unit.
 boolean equals(Object o)
          Determines if the unit is the same as a given unit.
 Action getActiveAction()
          Gets the currently executing Action.
 Set getBlockedActions()
          Gets the set of actions that have been blocked during the last tick.
 String getCommander()
          Gets the name of the role of the commander of the unit.
 double getDoubleProperty(String name)
          Gets a specific property value that is known to be a double value.
 int getHandle()
          Gets the handle of the unit.
 int getIntProperty(String name)
          Gets a specific property value that is known to be an integer value.
 int getIntX()
          Gets the x coordinate of the unit's position, cast as an integer, using the floor function
 int getIntY()
          Gets the y coordinate of the unit's position, cast as an integer, using the floor function.
 String getName()
          Gets the name of the unit.
 Set getObservers()
          Gets the set of observers of the unit.
 Map getProperties()
          Gets the map of properties of the unit.
 Object getProperty(String name)
          Gets a specific property value, given the property name.
 String getPropertyType(String name)
          Gets the PropertyType attribute of the Unit object
 String getStringProperty(String name)
          Gets a specific property value that is known to be a String.
 UnitType getUnitType()
          Gets the unit type of the unit.
 Set getUpdatedActions()
          Gets the set of all actions that have been updated.
 HashMap getUpdatedProperties()
          Gets the map of all properties that have been updated during the current tick.
 double getX()
          Gets the x coordinate of the unit's position.
 double getY()
          Gets the y coordinate of the unit's position.
 int hashCode()
          Returns a hash code of the unit.
 void init()
          This method is called once before the game starts.
 boolean isDestroyed()
          Determines if the unit has been destroyed.
 boolean isObserver(String role)
          Determines if a given role is an observer of the unit.
 void markAsDestroyed()
          Mark the unit as destroyed.
 void printWriteMe(PrintWriter out)
           
 void removeBlockedAction(String actionName)
          Unblock an action.
 boolean removeObserver(String role)
          Remove a role from the set of observers of the unit, so the role is no longer an observer.
 void removeProperty(String name)
           
 void resetUpdatedActions()
          Clears the set of updates actions.
 void resetUpdatedProperties()
          Clears the set of updated properties.
protected  void sendUnitMessage(String message)
          Sends a message to all observers of the unit (as well as to all God's Eye roles).
 void setActiveAction(Action action)
          Sets the ActiveAction attribute of the Unit object
 void setCommander(String _commander)
          Sets the Commander attribute of the Unit object
 void setHandle(int _handle)
          Sets the Handle attribute of the Unit object
 void setName(String _name)
          Sets the Name attribute of the Unit object
 void setPosition(double _x, double _y)
          Sets the Position attribute of the Unit object
 void setProperty(String name, double value)
          Sets a property to a Double value.
 void setProperty(String name, int value)
          Sets a property to an Integer value.
 void setProperty(String name, String value)
          Sets a property to a String value.
 void setUnitType(String _type)
          Sets the UnitType attribute of the Unit object
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

handle

protected int handle
The handle identifies the unit during communication with clients.


activeAction

protected Action activeAction
This is the action that the unit is currently executing.


name

protected String name
The name of the unit appears in the client, and is set up in a configuration file.


type

protected String type
The type of the unit appears in the client, and is set up in a configuration file. This attribute also identifies to the server which UnitType this unit is an instance of.


unitType

protected UnitType unitType
An instance of the unit type of this unit.


isDestroyed

protected boolean isDestroyed
Flag to indicate if the unit is destroyed.


commander

protected String commander
The name of the role that commands this unit, that is, the role that has the right to execute actions with the unit.


observers

protected HashSet observers
The set of observers contains the names of all roles that has observing rights on this unit, that is, that sees everything this unit sees.


x

protected double x
The x-coordinate of the unit's position.


y

protected double y
The y-coordinate of the unit's position.


intX

protected int intX
The x-coordinate of the unit's position as an integer. This value should be Math.floor(x).


intY

protected int intY
The y-coordinate of the unit's position as an integer. This value should be Math.floor(y).


properties

protected TypedProperties properties
Container for the properties of the unit. A property is represented as a String. The keys of the Map are property names, and the values are property values.


updatedProperties

protected HashMap updatedProperties
Contains all properties that have been updated during the current tick, and the new values.


blockedActions

protected HashSet blockedActions
Contains all Actions that have been blocked, that is, have been stopped during execution for some reason.


updatedActions

protected HashSet updatedActions
???

Constructor Detail

Unit

public Unit()
Constructor for the Unit object

Method Detail

setName

public void setName(String _name)
Sets the Name attribute of the Unit object

Parameters:
_name - The new Name value

setUnitType

public void setUnitType(String _type)
Sets the UnitType attribute of the Unit object

Parameters:
_type - The new unit type

setHandle

public void setHandle(int _handle)
Sets the Handle attribute of the Unit object

Parameters:
_handle - The new handle

setPosition

public void setPosition(double _x,
                        double _y)
Sets the Position attribute of the Unit object

Parameters:
_x - The x coordinate of the new position
_y - The y coordinate of the new position

setCommander

public void setCommander(String _commander)
Sets the Commander attribute of the Unit object

Parameters:
_commander - The new Commander value

setActiveAction

public void setActiveAction(Action action)
Sets the ActiveAction attribute of the Unit object

Parameters:
action - The new ActiveAction value

setProperty

public void setProperty(String name,
                        double value)
Sets a property to a Double value.

Parameters:
name - The name of the property.
value - The value of the property.

setProperty

public void setProperty(String name,
                        int value)
Sets a property to an Integer value.

Parameters:
name - The name of the property.
value - The value of the property.

setProperty

public void setProperty(String name,
                        String value)
Sets a property to a String value.

Parameters:
name - The name of the property.
value - The value of the property.

getName

public String getName()
Gets the name of the unit.

Returns:
The name

getUnitType

public UnitType getUnitType()
Gets the unit type of the unit.

Returns:
The unit type

getHandle

public int getHandle()
Gets the handle of the unit.

Returns:
The handle

getX

public double getX()
Gets the x coordinate of the unit's position.

Returns:
The x coordinate.

getY

public double getY()
Gets the y coordinate of the unit's position.

Returns:
The y coordinate.

getIntX

public int getIntX()
Gets the x coordinate of the unit's position, cast as an integer, using the floor function

Returns:
The integer x coordinate.

getIntY

public int getIntY()
Gets the y coordinate of the unit's position, cast as an integer, using the floor function.

Returns:
The integer y coordinate.

getCommander

public String getCommander()
Gets the name of the role of the commander of the unit.

Returns:
The name of the commander.

getActiveAction

public Action getActiveAction()
Gets the currently executing Action.

Returns:
The active action.

isObserver

public boolean isObserver(String role)
Determines if a given role is an observer of the unit.

Parameters:
role - The name of the role.
Returns:
true if the role is an observer, false otherwise

getObservers

public Set getObservers()
Gets the set of observers of the unit.

Returns:
The set of observers.

getProperties

public Map getProperties()
Gets the map of properties of the unit. The keys of the Map are property names, and the values are property values.

Returns:
The map of properties.

getProperty

public Object getProperty(String name)
Gets a specific property value, given the property name.

Parameters:
name - The property name.
Returns:
The property value.

getPropertyType

public String getPropertyType(String name)
Gets the PropertyType attribute of the Unit object

Parameters:
name - Description of Parameter
Returns:
The PropertyType value

getDoubleProperty

public double getDoubleProperty(String name)
Gets a specific property value that is known to be a double value.

Parameters:
name - The property name.
Returns:
The property value.

getIntProperty

public int getIntProperty(String name)
Gets a specific property value that is known to be an integer value.

Parameters:
name - The property name.
Returns:
The property value

getStringProperty

public String getStringProperty(String name)
Gets a specific property value that is known to be a String.

Parameters:
name - The property name.
Returns:
The property value.

getUpdatedProperties

public HashMap getUpdatedProperties()
Gets the map of all properties that have been updated during the current tick.

Returns:
The updated properties.

getUpdatedActions

public Set getUpdatedActions()
Gets the set of all actions that have been updated.

Returns:
The UpdatedActions value

getBlockedActions

public Set getBlockedActions()
Gets the set of actions that have been blocked during the last tick. That is, the execution of the actions have stopped because they could not continue, for some reason.

Returns:
The set of blocked actions.

isDestroyed

public boolean isDestroyed()
Determines if the unit has been destroyed.

Returns:
True if the unit has been destroyed, false otherwise.

addObserver

public void addObserver(String role)
Adds a new observer of the unit.

Parameters:
role - The name of the role that is to be an observer of the unit.

removeObserver

public boolean removeObserver(String role)
Remove a role from the set of observers of the unit, so the role is no longer an observer.

Parameters:
role - The name of the role.
Returns:
true if the role was an observer of the unit, false otherwise.

resetUpdatedProperties

public void resetUpdatedProperties()
Clears the set of updated properties.


resetUpdatedActions

public void resetUpdatedActions()
Clears the set of updates actions.


addBlockedAction

public void addBlockedAction(String actionName)
Block an action.

Parameters:
actionName - The name of the action.

removeBlockedAction

public void removeBlockedAction(String actionName)
Unblock an action.

Parameters:
actionName - The name of the action.

canSeeAutomatonAtPosition

public boolean canSeeAutomatonAtPosition(int _x,
                                         int _y)
Determines if the unit can see a specific automaton, that is, if the given position is inside the units visibility range.

Parameters:
_x - The x coordinate of the position.
_y - The x coordinate of the position.
Returns:
true if the unit can see the position, false otherwise.

markAsDestroyed

public void markAsDestroyed()
Mark the unit as destroyed.


sendUnitMessage

protected void sendUnitMessage(String message)
Sends a message to all observers of the unit (as well as to all God's Eye roles).

Parameters:
message - The message to be sent.

abortAllActions

public void abortAllActions()

init

public void init()
This method is called once before the game starts. All the properties are set and that makes this method usful for saving intial states and set up variables for a Unit HS


equals

public boolean equals(Object o)
Determines if the unit is the same as a given unit. The effect is that the handles of the units are compared, and the units are considered equal if the handles are equal. This method obeys the rules of the Comparable interface.

Overrides:
equals in class Object
Parameters:
o - The given object, possibly a unit.
Returns:
true if the given object is a unit, and the same unit as the current unit.

compareTo

public int compareTo(Object o)
Compares the unit to a given unit. The effect is that the handles are compared. This method obeys the rules of the Comparable interface.

Specified by:
compareTo in interface Comparable
Parameters:
o - The given object, possibly a unit.
Returns:
negative if this unit is less than the given unit, zero if the units are equal, and positive if this unit is greater than the given unit.

hashCode

public int hashCode()
Returns a hash code of the unit.

Overrides:
hashCode in class Object
Returns:
The hash code.

removeProperty

public void removeProperty(String name)

toString

public String toString()
Overrides:
toString in class Object

printWriteMe

public void printWriteMe(PrintWriter out)