org.newdawn.slick.state
Class StateBasedGame

java.lang.Object
  extended by org.newdawn.slick.state.StateBasedGame
All Implemented Interfaces:
ControlledInputReciever, ControllerListener, Game, InputListener, KeyListener, MouseListener
Direct Known Subclasses:
StateBasedTest, TransitionTest

public abstract class StateBasedGame
extends java.lang.Object
implements Game, InputListener

A state based game isolated different stages of the game (menu, ingame, hiscores, etc) into different states so they can be easily managed and maintained.

Author:
kevin

Constructor Summary
StateBasedGame(java.lang.String name)
          Create a new state based game
 
Method Summary
 void addState(GameState state)
          Add a state to the game.
 boolean closeRequested()
          Notification that a game close has been requested
 void controllerButtonPressed(int controller, int button)
          Notification that a button control has been pressed on the controller.
 void controllerButtonReleased(int controller, int button)
          Notification that a button control has been released on the controller.
 void controllerDownPressed(int controller)
          Notification that the down control has been pressed on the controller.
 void controllerDownReleased(int controller)
          Notification that the down control has been released on the controller.
 void controllerLeftPressed(int controller)
          Notification that the left control has been pressed on the controller.
 void controllerLeftReleased(int controller)
          Notification that the left control has been released on the controller.
 void controllerRightPressed(int controller)
          Notification that the right control has been pressed on the controller.
 void controllerRightReleased(int controller)
          Notification that the right control has been released on the controller.
 void controllerUpPressed(int controller)
          Notification that the up control has been pressed on the controller.
 void controllerUpReleased(int controller)
          Notification that the up control has been released on the controller.
 void enterState(int id)
          Enter a particular game state with no transition
 void enterState(int id, Transition leave, Transition enter)
          Enter a particular game state with the transitions provided
 GameContainer getContainer()
          Get the container holding this game
 GameState getCurrentState()
          Get the state the game is currently in
 int getCurrentStateID()
          Get the ID of the state the game is currently in
 GameState getState(int id)
          Get a state based on it's identifier
 int getStateCount()
          Get the number of states that have been added to this game
 java.lang.String getTitle()
          Get the title of this game
 void init(GameContainer container)
          Initialise the game.
abstract  void initStatesList(GameContainer container)
          Initialise the list of states making up this game
 void inputEnded()
          Notification that all input events have been sent for this frame
 void inputStarted()
          Notification that input is about to be processed
 boolean isAcceptingInput()
          Check if this input listener is accepting input
 void keyPressed(int key, char c)
          Notification that a key was pressed
 void keyReleased(int key, char c)
          Notification that a key was released
 void mouseClicked(int button, int x, int y, int clickCount)
          Notification that a mouse button was clicked.
 void mouseDragged(int oldx, int oldy, int newx, int newy)
          Notification that mouse cursor was dragged
 void mouseMoved(int oldx, int oldy, int newx, int newy)
          Notification that mouse cursor was moved
 void mousePressed(int button, int x, int y)
          Notification that a mouse button was pressed
 void mouseReleased(int button, int x, int y)
          Notification that a mouse button was released
 void mouseWheelMoved(int newValue)
          Notification that the mouse wheel position was updated
protected  void postRenderState(GameContainer container, Graphics g)
          User hook for rendering at the game level after the current state and/or transition have been rendered
protected  void postUpdateState(GameContainer container, int delta)
          User hook for rendering at the game level after the current state and/or transition have been updated
protected  void preRenderState(GameContainer container, Graphics g)
          User hook for rendering at the before the current state and/or transition have been rendered
protected  void preUpdateState(GameContainer container, int delta)
          User hook for updating at the game before the current state and/or transition have been updated
 void render(GameContainer container, Graphics g)
          Render the game's screen here.
 void setInput(Input input)
          Set the input that events are being sent from
 void update(GameContainer container, int delta)
          Update the game logic here.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

StateBasedGame

public StateBasedGame(java.lang.String name)
Create a new state based game

Parameters:
name - The name of the game
Method Detail

inputStarted

public void inputStarted()
Description copied from interface: ControlledInputReciever
Notification that input is about to be processed

Specified by:
inputStarted in interface ControlledInputReciever
See Also:
ControlledInputReciever.inputStarted()

getStateCount

public int getStateCount()
Get the number of states that have been added to this game

Returns:
The number of states that have been added to this game

getCurrentStateID

public int getCurrentStateID()
Get the ID of the state the game is currently in

Returns:
The ID of the state the game is currently in

getCurrentState

public GameState getCurrentState()
Get the state the game is currently in

Returns:
The state the game is currently in

setInput

public void setInput(Input input)
Description copied from interface: ControlledInputReciever
Set the input that events are being sent from

Specified by:
setInput in interface ControlledInputReciever
Parameters:
input - The input instance sending events
See Also:
ControlledInputReciever.setInput(org.newdawn.slick.Input)

addState

public void addState(GameState state)
Add a state to the game. The state will be updated and maintained by the game

Parameters:
state - The state to be added

getState

public GameState getState(int id)
Get a state based on it's identifier

Parameters:
id - The ID of the state to retrieve
Returns:
The state requested or null if no state with the specified ID exists

enterState

public void enterState(int id)
Enter a particular game state with no transition

Parameters:
id - The ID of the state to enter

enterState

public void enterState(int id,
                       Transition leave,
                       Transition enter)
Enter a particular game state with the transitions provided

Parameters:
id - The ID of the state to enter
leave - The transition to use when leaving the current state
enter - The transition to use when entering the new state

init

public final void init(GameContainer container)
                throws SlickException
Description copied from interface: Game
Initialise the game. This can be used to load static resources. It's called before the game loop starts

Specified by:
init in interface Game
Parameters:
container - The container holding the game
Throws:
SlickException - Throw to indicate an internal error
See Also:
BasicGame.init(org.newdawn.slick.GameContainer)

initStatesList

public abstract void initStatesList(GameContainer container)
                             throws SlickException
Initialise the list of states making up this game

Parameters:
container - The container holding the game
Throws:
SlickException - Indicates a failure to initialise the state based game resources

render

public final void render(GameContainer container,
                         Graphics g)
                  throws SlickException
Description copied from interface: Game
Render the game's screen here.

Specified by:
render in interface Game
Parameters:
container - The container holing this game
g - The graphics context that can be used to render. However, normal rendering routines can also be used.
Throws:
SlickException - Throw to indicate a internal error
See Also:
Game.render(org.newdawn.slick.GameContainer, org.newdawn.slick.Graphics)

preRenderState

protected void preRenderState(GameContainer container,
                              Graphics g)
                       throws SlickException
User hook for rendering at the before the current state and/or transition have been rendered

Parameters:
container - The container in which the game is hosted
g - The graphics context on which to draw
Throws:
SlickException - Indicates a failure within render

postRenderState

protected void postRenderState(GameContainer container,
                               Graphics g)
                        throws SlickException
User hook for rendering at the game level after the current state and/or transition have been rendered

Parameters:
container - The container in which the game is hosted
g - The graphics context on which to draw
Throws:
SlickException - Indicates a failure within render

update

public final void update(GameContainer container,
                         int delta)
                  throws SlickException
Description copied from interface: Game
Update the game logic here. No rendering should take place in this method though it won't do any harm.

Specified by:
update in interface Game
Parameters:
container - The container holing this game
delta - The amount of time thats passed since last update in milliseconds
Throws:
SlickException - Throw to indicate an internal error
See Also:
BasicGame.update(org.newdawn.slick.GameContainer, int)

preUpdateState

protected void preUpdateState(GameContainer container,
                              int delta)
                       throws SlickException
User hook for updating at the game before the current state and/or transition have been updated

Parameters:
container - The container in which the game is hosted
delta - The amount of time in milliseconds since last update
Throws:
SlickException - Indicates a failure within render

postUpdateState

protected void postUpdateState(GameContainer container,
                               int delta)
                        throws SlickException
User hook for rendering at the game level after the current state and/or transition have been updated

Parameters:
container - The container in which the game is hosted
delta - The amount of time in milliseconds since last update
Throws:
SlickException - Indicates a failure within render

closeRequested

public boolean closeRequested()
Description copied from interface: Game
Notification that a game close has been requested

Specified by:
closeRequested in interface Game
Returns:
True if the game should close
See Also:
Game.closeRequested()

getTitle

public java.lang.String getTitle()
Description copied from interface: Game
Get the title of this game

Specified by:
getTitle in interface Game
Returns:
The title of the game
See Also:
Game.getTitle()

getContainer

public GameContainer getContainer()
Get the container holding this game

Returns:
The game container holding this game

controllerButtonPressed

public void controllerButtonPressed(int controller,
                                    int button)
Description copied from interface: ControllerListener
Notification that a button control has been pressed on the controller.

Specified by:
controllerButtonPressed in interface ControllerListener
Parameters:
controller - The index of the controller on which the control was pressed.
button - The index of the button pressed (starting at 1)
See Also:
ControllerListener.controllerButtonPressed(int, int)

controllerButtonReleased

public void controllerButtonReleased(int controller,
                                     int button)
Description copied from interface: ControllerListener
Notification that a button control has been released on the controller.

Specified by:
controllerButtonReleased in interface ControllerListener
Parameters:
controller - The index of the controller on which the control was released.
button - The index of the button released (starting at 1)
See Also:
ControllerListener.controllerButtonReleased(int, int)

controllerDownPressed

public void controllerDownPressed(int controller)
Description copied from interface: ControllerListener
Notification that the down control has been pressed on the controller.

Specified by:
controllerDownPressed in interface ControllerListener
Parameters:
controller - The index of the controller on which the control was pressed.
See Also:
ControllerListener.controllerDownPressed(int)

controllerDownReleased

public void controllerDownReleased(int controller)
Description copied from interface: ControllerListener
Notification that the down control has been released on the controller.

Specified by:
controllerDownReleased in interface ControllerListener
Parameters:
controller - The index of the controller on which the control was released.
See Also:
ControllerListener.controllerDownReleased(int)

controllerLeftPressed

public void controllerLeftPressed(int controller)
Description copied from interface: ControllerListener
Notification that the left control has been pressed on the controller.

Specified by:
controllerLeftPressed in interface ControllerListener
Parameters:
controller - The index of the controller on which the control was pressed.
See Also:
ControllerListener.controllerLeftPressed(int)

controllerLeftReleased

public void controllerLeftReleased(int controller)
Description copied from interface: ControllerListener
Notification that the left control has been released on the controller.

Specified by:
controllerLeftReleased in interface ControllerListener
Parameters:
controller - The index of the controller on which the control was released.
See Also:
ControllerListener.controllerLeftReleased(int)

controllerRightPressed

public void controllerRightPressed(int controller)
Description copied from interface: ControllerListener
Notification that the right control has been pressed on the controller.

Specified by:
controllerRightPressed in interface ControllerListener
Parameters:
controller - The index of the controller on which the control was pressed.
See Also:
ControllerListener.controllerRightPressed(int)

controllerRightReleased

public void controllerRightReleased(int controller)
Description copied from interface: ControllerListener
Notification that the right control has been released on the controller.

Specified by:
controllerRightReleased in interface ControllerListener
Parameters:
controller - The index of the controller on which the control was released.
See Also:
ControllerListener.controllerRightReleased(int)

controllerUpPressed

public void controllerUpPressed(int controller)
Description copied from interface: ControllerListener
Notification that the up control has been pressed on the controller.

Specified by:
controllerUpPressed in interface ControllerListener
Parameters:
controller - The index of the controller on which the control was pressed.
See Also:
ControllerListener.controllerUpPressed(int)

controllerUpReleased

public void controllerUpReleased(int controller)
Description copied from interface: ControllerListener
Notification that the up control has been released on the controller.

Specified by:
controllerUpReleased in interface ControllerListener
Parameters:
controller - The index of the controller on which the control was released.
See Also:
ControllerListener.controllerUpReleased(int)

keyPressed

public void keyPressed(int key,
                       char c)
Description copied from interface: KeyListener
Notification that a key was pressed

Specified by:
keyPressed in interface KeyListener
Parameters:
key - The key code that was pressed (@see org.newdawn.slick.Input)
c - The character of the key that was pressed
See Also:
KeyListener.keyPressed(int, char)

keyReleased

public void keyReleased(int key,
                        char c)
Description copied from interface: KeyListener
Notification that a key was released

Specified by:
keyReleased in interface KeyListener
Parameters:
key - The key code that was released (@see org.newdawn.slick.Input)
c - The character of the key that was released
See Also:
KeyListener.keyReleased(int, char)

mouseMoved

public void mouseMoved(int oldx,
                       int oldy,
                       int newx,
                       int newy)
Description copied from interface: MouseListener
Notification that mouse cursor was moved

Specified by:
mouseMoved in interface MouseListener
Parameters:
oldx - The old x position of the mouse
oldy - The old y position of the mouse
newx - The new x position of the mouse
newy - The new y position of the mouse
See Also:
MouseListener.mouseMoved(int, int, int, int)

mouseDragged

public void mouseDragged(int oldx,
                         int oldy,
                         int newx,
                         int newy)
Description copied from interface: MouseListener
Notification that mouse cursor was dragged

Specified by:
mouseDragged in interface MouseListener
Parameters:
oldx - The old x position of the mouse
oldy - The old y position of the mouse
newx - The new x position of the mouse
newy - The new y position of the mouse
See Also:
MouseListener.mouseDragged(int, int, int, int)

mouseClicked

public void mouseClicked(int button,
                         int x,
                         int y,
                         int clickCount)
Description copied from interface: MouseListener
Notification that a mouse button was clicked. Due to double click handling the single click may be delayed slightly. For absolute notification of single clicks use mousePressed(). To be absolute this method should only be used when considering double clicks

Specified by:
mouseClicked in interface MouseListener
Parameters:
button - The index of the button (starting at 0)
x - The x position of the mouse when the button was pressed
y - The y position of the mouse when the button was pressed
clickCount - The number of times the button was clicked
See Also:
MouseListener.mouseClicked(int, int, int, int)

mousePressed

public void mousePressed(int button,
                         int x,
                         int y)
Description copied from interface: MouseListener
Notification that a mouse button was pressed

Specified by:
mousePressed in interface MouseListener
Parameters:
button - The index of the button (starting at 0)
x - The x position of the mouse when the button was pressed
y - The y position of the mouse when the button was pressed
See Also:
MouseListener.mousePressed(int, int, int)

mouseReleased

public void mouseReleased(int button,
                          int x,
                          int y)
Description copied from interface: MouseListener
Notification that a mouse button was released

Specified by:
mouseReleased in interface MouseListener
Parameters:
button - The index of the button (starting at 0)
x - The x position of the mouse when the button was released
y - The y position of the mouse when the button was released
See Also:
MouseListener.mouseReleased(int, int, int)

isAcceptingInput

public boolean isAcceptingInput()
Description copied from interface: ControlledInputReciever
Check if this input listener is accepting input

Specified by:
isAcceptingInput in interface ControlledInputReciever
Returns:
True if the input listener should recieve events
See Also:
ControlledInputReciever.isAcceptingInput()

inputEnded

public void inputEnded()
Description copied from interface: ControlledInputReciever
Notification that all input events have been sent for this frame

Specified by:
inputEnded in interface ControlledInputReciever
See Also:
ControlledInputReciever.inputEnded()

mouseWheelMoved

public void mouseWheelMoved(int newValue)
Description copied from interface: MouseListener
Notification that the mouse wheel position was updated

Specified by:
mouseWheelMoved in interface MouseListener
Parameters:
newValue - The amount of the wheel has moved
See Also:
MouseListener.mouseWheelMoved(int)


Copyright © 2006 New Dawn Software. All Rights Reserved.