nankin.zheng.gatem.model
Class GameContainer

java.lang.Object
  |
  +--nankin.zheng.gatem.model.GameContainer

public abstract class GameContainer
extends java.lang.Object

GameContainer is in charge of the game (frame) updating and the communications among all the roles inside it. All the work is done by a thread while game is not over. NOTE that by default, GameContainer does not care about user input.

Version:
1.0, Date 2003-04-11
Author:
ZHENG Zhong

Field Summary
protected  Background background
          A background reference is held seperately to update viewport.
protected  Env env
          The game environment.
protected  StageLoader loader
          Stage loader, which loads stage information (creates RoleView and Role objects) for this container.
protected  java.util.Vector roleList
          Role list, containing all the active roles whose act() will be called during every frame-updating.
protected  GameScheduler scheduler
          Game scheduler, which schedules all the game containers.
protected  int status
          Status of the container.
 
Constructor Summary
GameContainer(Env environ)
          Constructor.
 
Method Summary
 void addRole(Role r)
          Add a new role into the role list.
 void broadcastGameEvent(GameEvent gevt)
          Broadcast a game event to all the roles in the roleList.
protected  void drawGameFrame()
          This method draws all the roles onto the screen without changing their status.
 int getContainerStatus()
          Get container status.
 StageLoader getStageLoader()
          Get stage loader for this container.
protected abstract  void receiveGameEvent(GameEvent gevt)
          This method works as Role.receiveGameEvent(...) since game event may also have some impact on GameContainer.
 void setBackground(Background b)
          Sets background.
 void setContainerStatus(int sts)
          Set container status.
 void setGameScheduler(GameScheduler s)
          Set game scheduler.
 void setStageLoader(StageLoader l)
          Set stage loader for this container.
 void testInitialization()
          Verify whether this object is initialized properly.
 void unloadStage()
          Unload this stage.
 void updateGameFrame()
          This method updates all the roles' logical status.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

env

protected Env env
The game environment.


scheduler

protected GameScheduler scheduler
Game scheduler, which schedules all the game containers.


status

protected int status
Status of the container. Constants of container status are defined in class GameScheduler.


background

protected Background background
A background reference is held seperately to update viewport.


roleList

protected java.util.Vector roleList
Role list, containing all the active roles whose act() will be called during every frame-updating.


loader

protected StageLoader loader
Stage loader, which loads stage information (creates RoleView and Role objects) for this container.

Constructor Detail

GameContainer

public GameContainer(Env environ)
              throws GameException
Constructor.

Parameters:
environ - game environment
Throws:
java.lang.NullPointerException - if environ is null.
GameException - if any error occured.
Method Detail

setBackground

public void setBackground(Background b)
                   throws GameException
Sets background.

The specified Background b should be a Role object, and should have already been added in the roleList.

Parameters:
b - background
Throws:
GameException - if b is not in the roleList yet.

getContainerStatus

public int getContainerStatus()
Get container status.

Returns:
status of GameContainer.

setContainerStatus

public void setContainerStatus(int sts)
Set container status.

Parameters:
sts - status of GameContainer.

setGameScheduler

public void setGameScheduler(GameScheduler s)
Set game scheduler.

Parameters:
s - game scheduler.

addRole

public void addRole(Role r)
             throws java.lang.NullPointerException
Add a new role into the role list.

Parameters:
r - role
Throws:
java.lang.NullPointerException - if r is null.

testInitialization

public void testInitialization()
                        throws GameException
Verify whether this object is initialized properly.

This method should be overridden to provide more strict verification.

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

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

updateGameFrame

public void updateGameFrame()
                     throws GameException
This method updates all the roles' logical status.

Throws:
GameException - may be thrown out by Role.act().

drawGameFrame

protected void drawGameFrame()
                      throws GameException
This method draws all the roles onto the screen without changing their status.

Throws:
GameException - may be thrown out by Role.draw().

broadcastGameEvent

public void broadcastGameEvent(GameEvent gevt)
                        throws GameException
Broadcast a game event to all the roles in the roleList.

This method is called by a role who fires a GameEvent. It will do the following two things:

Parameters:
gevt - game event to be broadcast.
Throws:
GameException - may be thrown out by receiveGameEvent().

receiveGameEvent

protected abstract void receiveGameEvent(GameEvent gevt)
                                  throws GameException
This method works as Role.receiveGameEvent(...) since game event may also have some impact on GameContainer.

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

setStageLoader

public void setStageLoader(StageLoader l)
Set stage loader for this container.

Parameters:
l - stage loader.

getStageLoader

public StageLoader getStageLoader()
Get stage loader for this container.

Returns:
stage loader.

unloadStage

public void unloadStage()
Unload this stage.

This method will clean its role list and unset its background, and then run System.gc(). This work will be done before a new stage is loaded.