nankin.zheng.gatem.model
Class Role

java.lang.Object
  |
  +--nankin.zheng.gatem.model.Role
Direct Known Subclasses:
Background

public abstract class Role
extends java.lang.Object

This class represents a logical role who lives and acts inside a game container.

Version:
1.0_01, Date 2003-04-14
Author:
ZHENG Zhong

Field Summary
protected  AttributeSet attrSet
          Role's logical attribute set.
protected  GameContainer container
          GameContainer is in charge of the communication with other roles.
private static int createdInstanceCount
           
protected  Env env
          Env is the environment of the game.
protected  RoleView view
          RoleView is in charge of drawing.
 
Constructor Summary
Role(GameContainer gcont, Env environ, RoleView v)
          Constructor.
 
Method Summary
abstract  void act()
          Role's action rules.
 void draw()
          Ask its RoleView object to draw the role itself.
static int getCreatedInstanceCount()
           
 void init(Position pos, int dir, int sts)
          Initialize a role object.
abstract  void receiveGameEvent(GameEvent gevt)
          This method tries a GameEvent, decides whether to accept it or not, and if yes, take actions accordingly.
 void testInitialization()
          Verify whether this object is initialized properly.
 java.lang.String toString()
          Convert to String.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

createdInstanceCount

private static int createdInstanceCount

view

protected RoleView view
RoleView is in charge of drawing. It is obligatory for a Role object.


container

protected GameContainer container
GameContainer is in charge of the communication with other roles. It is obligatory for a Role object.


env

protected Env env
Env is the environment of the game. It is obligatory for a Role object.


attrSet

protected AttributeSet attrSet
Role's logical attribute set.

Constructor Detail

Role

public Role(GameContainer gcont,
            Env environ,
            RoleView v)
     throws GameException
Constructor. A Role object must be connected to a game container, an environment and a RoleView object.

Parameters:
gcont - game container, cannot be null.
environ - game environment, cannot be null.
v - RoleView object, cannot be null.
Throws:
java.lang.NullPointerException - if any of the parameters is null.
GameException - exception may be thrown out by its subclasses.
Method Detail

getCreatedInstanceCount

public static int getCreatedInstanceCount()

init

public void init(Position pos,
                 int dir,
                 int sts)
Initialize a role object. NOTE that a Role object must be initialized before starting a game.

Parameters:
pos - position, cannot be null
dir - direction
sts - status
Throws:
java.lang.NullPointerException - if pos is null. (thrown from AttributeSet.setPosition(pos))

testInitialization

public void testInitialization()
                        throws GameException
Verify whether this object is initialized properly. This method should be overwritten to provide more strict verification.

NOTE that this method will call RoleView.testInitialization(), which means that its RoleView's proper initailization is the necessary condition of its proper initialization.

This method is called before game starts.

Throws:
GameException - if this role is not initialized properly.

act

public abstract void act()
                  throws GameException
Role's action rules.

This method is called by GameContainer during every frame-updating. It describes the behaviour of a role, but it does NOT draw the role.

Throws:
GameException - exception may be thrown out by its subclasses.

draw

public void draw()
          throws GameException
Ask its RoleView object to draw the role itself.

This method is called by GameContainer during every frame-updating. It asks its RoleView object to draw itself onto screen. But it does NOT change the role's attributes. It gives its attribute set to its RoleView object.

NOTE that in a special game container, if AttributeSet is extended to provide more complex role attribute set, this method MUST be overwritten, to pass the new XAttributeSet instance to the Role's View object.

Throws:
GameException - may be thrown out by its view.

receiveGameEvent

public abstract void receiveGameEvent(GameEvent gevt)
                               throws GameException
This method tries a GameEvent, decides whether to accept it or not, and if yes, take actions accordingly.

Role a.receiveGameEvent(...) may be called by its GameContainer, while some other role fires a game event and the GameContainer broadcasts it. a.receiveGameEvent(...) may also be called by Role b, while b receives an event from a, gets a reference of a from the event, and reacts to it.

Parameters:
gevt - game event that it receives.
Throws:
GameException - may be thrown out by its subclasses.

toString

public java.lang.String toString()
Convert to String.

Overrides:
toString in class java.lang.Object