sofia.micro
Class ScriptableWorld

java.lang.Object
  extended by greenfoot.sofiainternal.World
      extended by sofia.micro.World
          extended by sofia.micro.ScriptableWorld
All Implemented Interfaces:
Script
Direct Known Subclasses:
Island

public class ScriptableWorld
extends World
implements Script

Represents a World that is controlled by a script--that is, a predefined sequence of behavior played out over time. There are two ways to provide a script: either override the script() method in a subclass (most common for beginner programmers), or create a script as a separate object and pass it into setScript(Script) (more advanced, and more useful if there may be multiple scripts that might be associated with a given world).

Version:
$Date: 2012/08/04 16:40 $
Author:
Stephen Edwards, Last changed by $Author: edwards $

Constructor Summary
ScriptableWorld()
          Construct a new world with a default size of 20 x 12.
ScriptableWorld(int width, int height)
          Construct a new world.
ScriptableWorld(int width, int height, int scaledCellSize)
          Construct a new world.
 
Method Summary
 void act()
          This implementation of the act method executes one action in this world's script.
 void add(Actor actor)
          Add an Actor to the world.
 void add(Actor actor, int x, int y)
          Add an Actor to the world at a specified location.
 Script getScript()
          Get the script associated with this actor.
 void remove(Actor actor)
          Remove an Actor from the world.
 void script()
          Subclasses can override this method to provide the "script" for the actor to follow.
protected  void scriptStep()
          Triggers one action in this actor's script.
 void setBackgroundColor(sofia.graphics.Color backgroundColor)
          Set the color of the world's background, if you prefer a solid color background instead of an Image.
 void setCellBackground(Image background)
          Set a background image to use for each Cell.
 void setGridColor(sofia.graphics.Color gridColor)
          Ask the world to draw visible grid lines of a specified color.
 void setScript(Script script)
          Associate a script with this world by providing a Script object.
 void setWorldBackground(Image background)
          Set a background image to use for the entire world.
 void stopScript()
          Stop any currently executing script associated with this actor.
 
Methods inherited from class sofia.micro.World
backgroundIsForCells, getBackground, getHeight, getObjects, getObjects, getObjectsAt, getOrientation, getSpeed, getWidth, numberOfObjects, setActOrder, setCellBackground, setOrientation, setPaintOrder, setScaledCellSize, setSpeed, setWorldBackground, start, started, stop, stopped
 
Methods inherited from class greenfoot.sofiainternal.World
_gf_addObject, _gf_getBackground, _gf_getCellSize, _gf_getColorAt, _gf_getIntersectingObjects, _gf_getNeighbours, _gf_getObjects, _gf_getObjectsAt, _gf_getObjectsInDirection, _gf_getObjectsInRange, _gf_getOneIntersectingObject, _gf_getOneObjectAt, _gf_removeObject, _gf_removeObjects, _gf_repaint, _gf_setActOrder, _gf_setBackground, _gf_setBackground, _gf_setPaintOrder
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ScriptableWorld

public ScriptableWorld()
Construct a new world with a default size of 20 x 12. This default size is based on a 320x480 (Android's HVGA resolution, which is a mid-level phone resolution) in landscape orientation, leaving some room for a notification bar and other decorations. This would result in 24x24 pixel cells, or 16x16 cells on a 240x320 phone. This world (and its actors) will be automatically scaled up (zoomed) if the Android device resolution permits it.

If an image based on the world's class name exists, it will be used as the background for each cell.


ScriptableWorld

public ScriptableWorld(int width,
                       int height)
Construct a new world. The size of the world (in number of cells) must be specified. This world (and its actors) will be automatically scaled up (zoomed) if the Android device resolution permits it.

If an image based on the world's class name exists, it will be used as the background for each cell.

Parameters:
width - The width of the world (in cells).
height - The height of the world (in cells).

ScriptableWorld

public ScriptableWorld(int width,
                       int height,
                       int scaledCellSize)
Construct a new world. The size of the world (in number of cells) must be specified. This constructor also sets the effective cell size of this world (for bitmaps). This world (and its actors) will be automatically scaled up (zoomed) if the Android device resolution permits it.

If an image based on the world's class name exists, it will be used as the background for each cell.

Parameters:
width - The width of the world (in cells).
height - The height of the world (in cells).
scaledCellSize - For rendering bitmaps, treat each cell as if it were a square of this many pixels on each side.
Method Detail

add

public void add(Actor actor)
Add an Actor to the world.

Overrides:
add in class World
Parameters:
actor - The Actor to add.

add

public void add(Actor actor,
                int x,
                int y)
Add an Actor to the world at a specified location. This is a convenience method that is equivalent to calling add() on an actor, and then calling setGridLocation() on the actor to specify its position.

Overrides:
add in class World
Parameters:
actor - The Actor to add.
x - The x coordinate of the location where the actor is added.
y - The y coordinate of the location where the actor is added.

remove

public void remove(Actor actor)
Description copied from class: World
Remove an Actor from the world.

Overrides:
remove in class World
Parameters:
actor - The Actor to remove.

act

public void act()
This implementation of the act method executes one action in this world's script. Normally, scriptable worlds do not directly call or override this method--if you want to provide act(), more often than not you want to use World as your base class.

If, on the other hand, you want to combine the features of a scriptable world with a custom act() method, you can do that by inheriting from this class and overriding this method. If you override this method, be sure to call super.act() or your world will no longer obey its assigned script.

Overrides:
act in class World

setGridColor

public void setGridColor(sofia.graphics.Color gridColor)
Ask the world to draw visible grid lines of a specified color. Use a parameter of null to request that no grid lines be shown (the default).

Overrides:
setGridColor in class World
Parameters:
gridColor - The color to use for grid lines around each cell, or null to suppress grid lines (the default).

setBackgroundColor

public void setBackgroundColor(sofia.graphics.Color backgroundColor)
Set the color of the world's background, if you prefer a solid color background instead of an Image.

Overrides:
setBackgroundColor in class World
Parameters:
backgroundColor - The color to use for the background of the world.

setCellBackground

public void setCellBackground(Image background)
Set a background image to use for each Cell. The given image will be scaled to fill exactly one cell, and tiled across the entire world's grid. This will replace any world background image.

Overrides:
setCellBackground in class World
Parameters:
background - The background image to use for each cell.

setWorldBackground

public void setWorldBackground(Image background)
Set a background image to use for the entire world. The given image will be scaled to fill the world's entire grid. This will replace any tiled cell background image.

Overrides:
setWorldBackground in class World
Parameters:
background - The background image to use for the entire world.

script

public void script()
Subclasses can override this method to provide the "script" for the actor to follow. The default implementation does nothing (i.e., no script by default).

Specified by:
script in interface Script

setScript

public void setScript(Script script)
Associate a script with this world by providing a Script object. Actions in the script will execute one move at a time as act() is called. A script value of null will remove any assigned script for this world.

Parameters:
script - The script to activate.

getScript

public Script getScript()
Get the script associated with this actor.

Returns:
This actor's script.

stopScript

public void stopScript()
Stop any currently executing script associated with this actor.


scriptStep

protected void scriptStep()
Triggers one action in this actor's script.



Greenfoot homepage