org.jrabbit.standard.game.world
Class World

java.lang.Object
  extended by org.jrabbit.standard.game.world.World
All Implemented Interfaces:
Renderable, Updateable, Layered

public class World
extends java.lang.Object
implements Renderable, Updateable, Layered

A World is a 2D gameworld, populated with Renderable and Updateable objects. The default world is just a Background, a Camera, and dynamic lists to contain objects to update and/or render. It is rather bare bones, and does not contain any particular methods to manage its contents (e.g., checking for collision between two sets of objects, or other such common game mechanics). However, it is simple to extend World and add that functionality to it. That's the whole point - for any reasonably complex game, this particular implementation isn't enough. However, it is a good base class for any particular gameworld, whether it is a top-down shooter, side-scrolling platformer, etc.

Author:
Chris Molini

Field Summary
protected  Background background
          The background of the world.
protected  Camera camera
          The Camera that determines where the player is looking in the world.
protected  int delta
          The current delta by which the World is updating its contents.
protected  LayeredRenderList layers
          The Layered list of all objects being Rendered.
protected  LockingList<Updateable> updated
          The dynamic list of all objects being updated.
 
Constructor Summary
World()
          Creates the default World.
 
Method Summary
 void add(java.lang.Object... objects)
          Attempts to add the Objects to the world.
 void add(java.lang.Object object)
          Attempts to add the indicated object to the World.
 void add(java.lang.String layer, java.lang.Object... objects)
          Attempts to add the Objects to the world, targeting the indicated Layer if they are Renderable.
 void add(java.lang.String layer, java.lang.Object object)
          Attempts to add the indicated object to the World.
 Background background()
          Accesses the object rendering the background.
 Camera camera()
          Accesses this world's Camera.
 void clear()
          Removes all objects from the lists.
 boolean contains(java.lang.Object object)
          Attempts to determine if the indicated object is contained in this world.
 int currentDelta()
          Learns the current timestep the World is using to update its children.
 LayeredRenderList layers()
          Accesses the Layered list of objects being rendered.
 void makeCurrent()
          Makes this world the active one in the current GameLoop.
 void remove(java.lang.Object... objects)
          Attempts to remove all indicated Objects from the World.
 void remove(java.lang.Object object)
          Attempts to remove the indicated Object from the World.
 void render()
          Renders the World.
 void setBackground(Background background)
          Redefines the Background being used by this World.
 void setCamera(Camera camera)
          Redefines the Camera being used to view the World.
 void update(int delta)
          Updates the world and all of its Updateable children.
 LockingList<Updateable> updated()
          Accesses the list of updated objects.
protected  void updateWorld(int delta)
          Updates the World separately from the objects.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

delta

protected int delta
The current delta by which the World is updating its contents.


updated

protected LockingList<Updateable> updated
The dynamic list of all objects being updated.


layers

protected LayeredRenderList layers
The Layered list of all objects being Rendered.


background

protected Background background
The background of the world. This is drawn "behind" all game objects.


camera

protected Camera camera
The Camera that determines where the player is looking in the world.

Constructor Detail

World

public World()
Creates the default World. It has no objects in it, an opaque black Background, its Camera points to [0, 0], and it has one Layer (referenced by the empty String).

Method Detail

add

public void add(java.lang.Object object)
Attempts to add the indicated object to the World. If the object implements Updateable, it is added to the list of updated objects and is updated from the next loop on. Likewise, if the object is Renderable, it is also added to the topmost Layer in the render list, and is rendered from the next frame on.

Parameters:
object - The object to add.

add

public void add(java.lang.Object... objects)
Attempts to add the Objects to the world. This functions exactly the same as calling add() for each individual object.

Parameters:
objects - The list of Objects to add.
See Also:
add(Object)

add

public void add(java.lang.String layer,
                java.lang.Object object)
Attempts to add the indicated object to the World. If the object is Renderable, it is added to the Layer indicated by the supplied reference.

Parameters:
layer - The identifier of the Layer to add to.
object - The object to add.

add

public void add(java.lang.String layer,
                java.lang.Object... objects)
Attempts to add the Objects to the world, targeting the indicated Layer if they are Renderable. This functions exactly the same as calling add() for each individual object, specifying the Layer for each operation.

Parameters:
layer - The identifier of the Layer to add to.
objects - The list of Objects to add.
See Also:
add(String, Object)

remove

public void remove(java.lang.Object object)
Attempts to remove the indicated Object from the World. If the object is Updateable or Renderable, the World attempts to remove it from the appropriate list(s).

Parameters:
object - The object to remove.

remove

public void remove(java.lang.Object... objects)
Attempts to remove all indicated Objects from the World. This functions exactly the same as calling remove() for each individual object.

Parameters:
objects - The objects to remove.
See Also:
remove(Object)

contains

public boolean contains(java.lang.Object object)
Attempts to determine if the indicated object is contained in this world.

Parameters:
object - The object to check for.
Returns:
True if the object is contained, false if not.

clear

public void clear()
Removes all objects from the lists. Also removes all layers.


updated

public LockingList<Updateable> updated()
Accesses the list of updated objects.

Returns:
The dynamic list of Updateable objects being managed by this World.

layers

public LayeredRenderList layers()
Accesses the Layered list of objects being rendered.

Specified by:
layers in interface Layered
Returns:
The LayeredRenderList this World uses to manage the objects it renders.

background

public Background background()
Accesses the object rendering the background.

Returns:
The Background of the World.

setBackground

public void setBackground(Background background)
Redefines the Background being used by this World.

Parameters:
background - The new Background to use.

camera

public Camera camera()
Accesses this world's Camera.

Returns:
The Camera object that controls the viewpoint.

setCamera

public void setCamera(Camera camera)
Redefines the Camera being used to view the World.

Parameters:
camera - The new Camera to use.

currentDelta

public int currentDelta()
Learns the current timestep the World is using to update its children. NOTE: It is very easy to extend this method to allow for "time dilation." Simply have a variable (say, 0.5 or 2) that multiplies the recorded delta when this method is called. Then, the speed of all updates will be affected by that amount.

Returns:
The amount of clock ticks the World should update by.

updateWorld

protected void updateWorld(int delta)
Updates the World separately from the objects. This method is called every update, after all objects have finished being updated. This method is the ideal place to put game controls such as collision detection and resolution. It occurs after objects have finished their normal action, but before rendering occurs.

Parameters:
delta - The amount of clock ticks since the last update.

update

public void update(int delta)
Updates the world and all of its Updateable children.

Specified by:
update in interface Updateable
Parameters:
delta - The amount of clock ticks since the last update.

render

public void render()
Renders the World. First the background is rendered, and then all Renderable entities are drawn as dictated by the Camera.

Specified by:
render in interface Renderable

makeCurrent

public void makeCurrent()
Makes this world the active one in the current GameLoop. This is the same as calling GameManager.setWorld(World) with this world.