org.piratis.j2me.core.game
Class Engine

java.lang.Object
  extended byorg.piratis.j2me.core.game.Engine
All Implemented Interfaces:
java.lang.Runnable

public abstract class Engine
extends java.lang.Object
implements java.lang.Runnable

Default Engine class. All game engines based on J2MEGL must override this one because of the tight binding between this class and the Canvas.

Once started, the engine enters and endlessly loop which won't stop until nextRunLoop return false (good candidate to be overwritten, isn't it?).

A game might contain more than one engine, depending on the status of the game and if it has playable sections/levels which differ among them in great measure, etc.. It is reponsibility of the programmer to stop one and start the other.

Version:
$Id: Engine.java,v 1.5 2004/07/22 16:50:39 piratis Exp $
Author:
Jordi Martín

Field Summary
protected  Canvas canvas
          Engine and canvas are tightly bound
static int DEFAULT_FPS
          Default FPS setting
static int IS_DOWN
           
static int IS_FIRE
           
static int IS_GAME_A
           
static int IS_GAME_B
           
static int IS_GAME_C
           
static int IS_GAME_D
           
static int IS_LEFT
           
static int IS_RIGHT
           
static int IS_UP
          Key status constants
protected  LayerManager layers
          Where to store all the layers/sprites/tiles
 
Constructor Summary
Engine(Canvas canvas)
          Creates a new Engine.
Engine(Canvas canvas, int fps)
          Creates a new Engine.
 
Method Summary
 int getKeyStates()
          Gets the states of the game keys.
protected  void initialize(Canvas canvas, int newFps)
          Initialize method..
protected abstract  void input()
          Engine's key management method.
protected  void keyPressed(int keyCode)
          Canvas calls this method upon a key is pressed.
protected  void keyReleased(int keyCode)
          Canvas call this method upon a key is released.
protected  boolean nextRunLoop()
          Checks if the run loop needs to continue running (based on engine status)
protected  void paint()
          Default paint method.
 void run()
           
protected  void setInitialStatus()
          Sets the status variables to an initial status
protected  void setRunningStatus()
          Engine Status to a running status
 void stop()
          Performs a stop
protected  void waitSomething(long loopStartTime, long interval)
          Wait method to synchronize work & repainting.
protected abstract  void work()
          Real engine method that should be implemented in subclasses
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

IS_UP

public static final int IS_UP
Key status constants


IS_DOWN

public static final int IS_DOWN

IS_LEFT

public static final int IS_LEFT

IS_RIGHT

public static final int IS_RIGHT

IS_FIRE

public static final int IS_FIRE

IS_GAME_A

public static final int IS_GAME_A

IS_GAME_B

public static final int IS_GAME_B

IS_GAME_C

public static final int IS_GAME_C

IS_GAME_D

public static final int IS_GAME_D

DEFAULT_FPS

public static int DEFAULT_FPS
Default FPS setting


canvas

protected Canvas canvas
Engine and canvas are tightly bound


layers

protected LayerManager layers
Where to store all the layers/sprites/tiles

Constructor Detail

Engine

public Engine(Canvas canvas)
Creates a new Engine. FPS are set to the default value.

Parameters:
canvas - the canvas of this engine

Engine

public Engine(Canvas canvas,
              int fps)
Creates a new Engine. FPS is not ensured: the engine/library will do its best to accomplish it!

Parameters:
canvas - the canvas of this engine
fps - desired frames per second refresh rate
Method Detail

run

public void run()
Specified by:
run in interface java.lang.Runnable
See Also:
Runnable.run()

stop

public void stop()
Performs a stop


waitSomething

protected void waitSomething(long loopStartTime,
                             long interval)
Wait method to synchronize work & repainting. Subclasses can override this method to adjust waiting times. It is called at the end of the engine loop.

Parameters:
loopStartTime - the start time of the engine loop
interval - adjusted to the engine/canvas FPS

work

protected abstract void work()
Real engine method that should be implemented in subclasses


input

protected abstract void input()
Engine's key management method. This must be implemented in subclasses in order to react to user key strokes


paint

protected void paint()
Default paint method.


initialize

protected void initialize(Canvas canvas,
                          int newFps)
Initialize method..


setInitialStatus

protected void setInitialStatus()
Sets the status variables to an initial status


nextRunLoop

protected boolean nextRunLoop()
Checks if the run loop needs to continue running (based on engine status)

Returns:

setRunningStatus

protected void setRunningStatus()
Engine Status to a running status


keyPressed

protected void keyPressed(int keyCode)
Canvas calls this method upon a key is pressed. The key pressed status will be automaticaly updated.

Parameters:
keyCode - which key has been pressed

keyReleased

protected void keyReleased(int keyCode)
Canvas call this method upon a key is released. The key pressed status will be automaticaly updated.

Parameters:
keyCode - which key has been released

getKeyStates

public int getKeyStates()
Gets the states of the game keys. The key's status must be checked bitwise.

After calling this method, the key status will be cleared.

Returns:

Copyright(c) 2003-2004 Jordi Martin