nankin.zheng.gatem.model
Class StageLoader

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

public abstract class StageLoader
extends java.lang.Object

StageLoader loads a new stage and reinitialize the game.

It creates all the RoleView objects while it is constructed. While loading a new stage, it reads the stage matrix (in .gsm format, which stands for Gatem Stage Matrix), creates Role objects according to the matrix, and then adds all these objects into the game container. It may also update the game background image.

NOTE: this class is planned to be rewritten using Apache Jakarta Commons Digester package.

Version:
1.0 (to be rewritten), Date 2003-04-14
Author:
ZHENG Zhong

Field Summary
protected  BackgroundView bgView
          BackgroundView reference.
protected  GameContainer container
          Game container.
protected  Controller controller
          Controller.
protected  DisplayCache displayCache
          Display cache, which is needed while creating RoleView objects.
protected  Env env
          Game environment, which contains some common variables.
protected  java.util.Vector roleViews
          RoleView vector.
protected  java.lang.String stageBgimage
          Current stage background image file name.
protected  java.lang.String stageDesc
          Current stage description.
protected  int stageId
          Current stage Id.
protected  java.lang.String stageName
          Current stage name.
protected  DOMTree stages
          stages contains a DOM tree, each of whose children represents a stage.
 
Constructor Summary
StageLoader(Env environ, DisplayCache dc, GameContainer c, Controller ctrl, java.lang.String fname)
          Constructor, sets data members and creates RoleView objects.
 
Method Summary
private  AnchoredImage createAnchoredImage(DOMTree aimgTree, java.lang.String basedir)
          Create one AnchoredImage object from a DOMTree like ....
private  AnchoredImage[] createAnchoredImageArray(DOMTree imgsTree, java.lang.String basedir)
          Create one AnchoredImage array from a DOMTree like ....
private  RoleView createRoleView(DOMTree rvTree)
          Create one RoleView object from a DOMTree like ....
protected  RoleView getRoleView(java.lang.String rname)
          Get a RoleView object from its vector roleViews by role name.
 boolean hasNextStage()
          Whether there are more stages.
 void loadNextStage()
          Load next stage.
protected abstract  void loadStageData(java.io.InputStream fin)
          Load a stage data file and create Role objects according to it.
protected abstract  RoleView newRoleViewInstance(java.lang.String rname, AnchoredImage[] aimgs)
          Create a RoleView object according to the role name and image file names.
 void readStageScript(java.lang.String fname)
          Read the stage script (in xml format) and generate a DOM tree from it.
 void reloadStage()
          Reload this stage.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

env

protected Env env
Game environment, which contains some common variables.


displayCache

protected DisplayCache displayCache
Display cache, which is needed while creating RoleView objects.


container

protected GameContainer container
Game container.


controller

protected Controller controller
Controller.


roleViews

protected java.util.Vector roleViews
RoleView vector.


bgView

protected BackgroundView bgView
BackgroundView reference.


stages

protected DOMTree stages
stages contains a DOM tree, each of whose children represents a stage.


stageId

protected int stageId
Current stage Id.


stageName

protected java.lang.String stageName
Current stage name.


stageDesc

protected java.lang.String stageDesc
Current stage description.


stageBgimage

protected java.lang.String stageBgimage
Current stage background image file name.

Constructor Detail

StageLoader

public StageLoader(Env environ,
                   DisplayCache dc,
                   GameContainer c,
                   Controller ctrl,
                   java.lang.String fname)
            throws GameException
Constructor, sets data members and creates RoleView objects.

Constructor creates only RoleView objects but not Role objects. Role objects will be created and initialized by loadNextStage() method.

NOTE that unrecognizable tags are ignored, and they do not invoke parsing errors. Parsing errors are invoked only when an expected tag cannot be found.

roleviews

Parameters:
environ - Game environment
dc - DisplayCache
c - GameContainer
ctrl - Controller
fname - file name of the RoleView initialization script
Throws:
GameException - if any error occured.
Method Detail

createRoleView

private RoleView createRoleView(DOMTree rvTree)
                         throws GameException
Create one RoleView object from a DOMTree like ....

roleview

Parameters:
rvTree - a ... DOM tree.
Returns:
a RoleView object, never null.
Throws:
GameException - if cannot create a RoleView object.

createAnchoredImageArray

private AnchoredImage[] createAnchoredImageArray(DOMTree imgsTree,
                                                 java.lang.String basedir)
                                          throws GameException
Create one AnchoredImage array from a DOMTree like ....

images

Parameters:
imgsTree - a ... DOM tree.
basedir - base directory of the file name.
Returns:
an AnchoredImage array (null is allowed).
Throws:
GameException - if any error occured.

createAnchoredImage

private AnchoredImage createAnchoredImage(DOMTree aimgTree,
                                          java.lang.String basedir)
                                   throws GameException
Create one AnchoredImage object from a DOMTree like ....

aimage

Parameters:
aimgTree - a ... DOM tree.
basedir - base directory of the file name.
Returns:
an AnchoredImage object, never null.
Throws:
GameException - if cannot create an AnchoredImage object.

getRoleView

protected RoleView getRoleView(java.lang.String rname)
                        throws GameException
Get a RoleView object from its vector roleViews by role name.

NOTE that this method will never return null.

Parameters:
rname - role name.
Throws:
GameException - if cannot find the expected RoleView object.

readStageScript

public void readStageScript(java.lang.String fname)
                     throws GameException
Read the stage script (in xml format) and generate a DOM tree from it.

NOTE that the DOM tree generated contains the information of all the stages.

Parameters:
fname - stage script file name
Throws:
GameException - if any I/O error or parsing error occured.

hasNextStage

public boolean hasNextStage()
                     throws GameException
Whether there are more stages.

Returns:
true if there are more stages.
Throws:
GameException - if stages are not loaded.

reloadStage

public void reloadStage()
                 throws GameException
Reload this stage. throws GameException if any error occured.

GameException

loadNextStage

public void loadNextStage()
                   throws GameException
Load next stage.

After loading, stage name, description and id will be updated, and all the Role objects will be created and added to GameContainer. The background image may be changed if necessary.

Throws:
GameException - if any error occured.

loadStageData

protected abstract void loadStageData(java.io.InputStream fin)
                               throws GameException
Load a stage data file and create Role objects according to it.

NOTE 1: The format of the stage data file is self-defined.

NOTE 2: The InputStream object will be closed after this method.

Parameters:
fin - an InputStream object connected to the stage data file.
Throws:
GameException - if any error occured.

newRoleViewInstance

protected abstract RoleView newRoleViewInstance(java.lang.String rname,
                                                AnchoredImage[] aimgs)
                                         throws GameException
Create a RoleView object according to the role name and image file names.

NOTE that this method never returns null. If it cannot create a RoleView object, it will throw out a GameException.

Parameters:
rname - role name
Throws:
GameException - if any error occured.