sofia.micro
Class World

java.lang.Object
  extended by greenfoot.sofiainternal.World
      extended by sofia.micro.World
Direct Known Subclasses:
ScriptableWorld, World

public class World
extends greenfoot.sofiainternal.World

Represents a "microworld" containing Actors and rendered on a screen.

Version:
$Date: 2012/08/06 11:13 $
Author:
Stephen Edwards, Last changed by $Author: edwards $

Constructor Summary
World()
          Construct a new world with a default size of 20 x 12.
World(int width, int height)
          Construct a new world.
World(int width, int height, int scaledCellSize)
          Construct a new world.
 
Method Summary
 void act()
          Act method for world.
 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.
 boolean backgroundIsForCells()
          Return whether the background image is scaled to each cell and repeated, or stretched over the entire grid.
 Image getBackground()
          Return the world's background image.
 int getHeight()
          Return the height of the world (in number of cells).
 java.util.Set<Actor> getObjects()
          Get all the actors in the world.
<MyActor extends Actor>
java.util.Set<MyActor>
getObjects(java.lang.Class<MyActor> cls)
          Get all the actors of the specified type in this world.
<MyActor extends Actor>
java.util.Set<MyActor>
getObjectsAt(float x, float y, java.lang.Class<MyActor> cls)
          Return all objects of the specified type at a given cell.
 int getOrientation()
          Get the preferred orientation of this world.
 int getSpeed()
          Get the speed of this world, which determines the delay between successive steps where act() is called on all Actors.
 int getWidth()
          Return the width of the world (in number of cells).
 int numberOfObjects()
          Get the number of actors currently in the world.
 void remove(Actor actor)
          Remove an Actor from the world.
 void setActOrder(java.lang.Class<? extends Actor>... classes)
          Set the act order of objects in the world.
 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 setCellBackground(java.lang.String 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 setOrientation(int orientation)
          Determine the preferred orientation of this World when displayed on the device.
 void setPaintOrder(java.lang.Class<? extends Actor>... classes)
          Set the paint order of objects in the world.
 void setScaledCellSize(int scaledCellSize, boolean scaleToFit)
          Set the effective cell size of this world (for bitmaps) and whether this world (and its actors) should be automatically scaled up (zoomed) if the Android device resolution permits it.
 void setSpeed(int speed)
          Set the speed of this world, which determines the delay between successive steps where act() is called on all Actors.
 void setWorldBackground(Image background)
          Set a background image to use for the entire world.
 void setWorldBackground(java.lang.String background)
          Set a background image to use for the entire world.
 void start()
          Start (or run, or resume) the world.
 void started()
          This method is called by the system when the execution has started.
 void stop()
          Stop (or pause running of) the world.
 void stopped()
          This method is called by the system when the execution has 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

World

public World()
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.


World

public World(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).

World

public World(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

getWidth

public int getWidth()
Return the width of the world (in number of cells).

Overrides:
getWidth in class greenfoot.sofiainternal.World
Returns:
The width of the world (in number of cells).

getHeight

public int getHeight()
Return the height of the world (in number of cells).

Overrides:
getHeight in class greenfoot.sofiainternal.World
Returns:
The height of the world (in number of cells).

add

public void add(Actor actor)
Add an Actor to the 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.

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)
Remove an Actor from the world.

Parameters:
actor - The Actor to remove.

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).

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.

Parameters:
backgroundColor - The color to use for the background of the world.

setCellBackground

public void setCellBackground(java.lang.String 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.

Parameters:
background - The background image to use for each cell.

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.

Parameters:
background - The background image to use for each cell.

setWorldBackground

public void setWorldBackground(java.lang.String 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.

Parameters:
background - The background image to use for the entire world.

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.

Parameters:
background - The background image to use for the entire world.

getBackground

public Image getBackground()
Return the world's background image.

Returns:
The background image.

backgroundIsForCells

public boolean backgroundIsForCells()
Return whether the background image is scaled to each cell and repeated, or stretched over the entire grid.

Returns:
True if the background is applied separately to each cell, or false if the background is fit to the entire world grid.

setPaintOrder

public void setPaintOrder(java.lang.Class<? extends Actor>... classes)
Set the paint order of objects in the world. Paint order is specified by class: objects of one class will always be painted on top of objects of some other class. The order of objects of the same class cannot be specified.

Objects of classes listed first in the parameter list will appear on top of all objects of classes listed later.

Objects of a class not explicitly specified inherit the paint order from their superclass.

Objects of classes not listed will appear below the objects whose classes have been specified.

Parameters:
classes - The classes in desired paint order.

setActOrder

public void setActOrder(java.lang.Class<? extends Actor>... classes)
Set the act order of objects in the world. Act order is specified by class: objects of one class will always act before objects of some other class. The order of objects of the same class cannot be specified.

Objects of classes listed first in the parameter list will act before any objects of classes listed later.

Objects of a class not explicitly specified inherit the act order from their superclass.

Objects of classes not listed will act after all objects whose classes have been specified.

Parameters:
classes - The classes in desired act order.

getObjects

public java.util.Set<Actor> getObjects()
Get all the actors in the world.

Returns:
A set of all the actors in this world.

getObjects

public <MyActor extends Actor> java.util.Set<MyActor> getObjects(java.lang.Class<MyActor> cls)
Get all the actors of the specified type in this world.

Type Parameters:
MyActor - The type of actor to look for, as specified in the cls parameter.
Parameters:
cls - Class of objects to look for (passing 'null' will find all objects).
Returns:
Set of all the actors of the specified type (or any of its subtypes) in the world.

numberOfObjects

public int numberOfObjects()
Get the number of actors currently in the world.

Overrides:
numberOfObjects in class greenfoot.sofiainternal.World
Returns:
The number of actors in the world.

act

public void act()
Act method for world. The act method is called by the system at each action step in the environment. The world's act method is called before the act method of any objects in the world.

This method does nothing. It should be overridden in subclasses to implement an world's action.

Overrides:
act in class greenfoot.sofiainternal.World

started

public void started()
This method is called by the system when the execution has started. This method can be overridden to implement custom behavior when the execution is started.

This default implementation is empty.

Overrides:
started in class greenfoot.sofiainternal.World

stopped

public void stopped()
This method is called by the system when the execution has stopped. This method can be overridden to implement custom behavior when the execution is stopped.

This default implementation is empty.

Overrides:
stopped in class greenfoot.sofiainternal.World

getObjectsAt

public <MyActor extends Actor> java.util.Set<MyActor> getObjectsAt(float x,
                                                                   float y,
                                                                   java.lang.Class<MyActor> cls)
Return all objects of the specified type at a given cell.

An object is defined to be at that cell if its graphical representation overlaps the center of the cell.

Type Parameters:
MyActor - The type of actor to look for, as specified in the cls parameter.
Parameters:
x - X-coordinate of the cell to be checked.
y - Y-coordinate of the cell to be checked.
cls - Class of objects to find ('null' will return all objects).
Returns:
A set of objects at the specified location.

setOrientation

public void setOrientation(int orientation)
Determine the preferred orientation of this World when displayed on the device.

Parameters:
orientation - The orientation to use, specified as one of the screen orientation constants defined in android.content.pm.ActivityInfo; typically, either android.content.pm.ActivityInfo#SCREEN_ORIENTATION_LANDSCAPE (locks the display in landscape orientation), android.content.pm.ActivityInfo#SCREEN_ORIENTATION_PORTRAIT (locks the display in portrait orientation), or android.content.pm.ActivityInfo#SCREEN_ORIENTATION_SENSOR (the orientation is controlled by the device's built-in orientation sensor).

getOrientation

public int getOrientation()
Get the preferred orientation of this world.

Returns:
An integer constant representing the orientation.
See Also:
setOrientation(int)

setScaledCellSize

public void setScaledCellSize(int scaledCellSize,
                              boolean scaleToFit)
Set the effective cell size of this world (for bitmaps) and whether this world (and its actors) should be automatically scaled up (zoomed) if the Android device resolution permits it.

Parameters:
scaledCellSize - For rendering bitmaps, treat each cell as if it were a square of this many pixels on each side.
scaleToFit - If true, cells will be scaled larger or smaller so that the grid is as large as possible on the physical device, with bitmaps scaled proportionately. If false, the grid will be rendered so that each cell is exactly scaledCellSize pixels in size, no more and no less, even if this means some of the grid will be clipped by the screen boundaries.

setSpeed

public void setSpeed(int speed)
Set the speed of this world, which determines the delay between successive steps where act() is called on all Actors. The speed is a value between 0-100, where 100 is maximum speed (least delay) and 0 is the slowest speed.

Parameters:
speed - The new speed (0-100)

getSpeed

public int getSpeed()
Get the speed of this world, which determines the delay between successive steps where act() is called on all Actors. The speed is a value between 0-100, where 100 is maximum speed (least delay) and 0 is the slowest speed.

Returns:
The current speed (0-100)

start

public void start()
Start (or run, or resume) the world. This requires that the world has already been attached to a view.


stop

public void stop()
Stop (or pause running of) the world. This requires that the world has already been attached to a view.



Greenfoot homepage