sofia.micro
Class ScriptableActor

java.lang.Object
  extended by greenfoot.sofiainternal.Actor
      extended by sofia.graphics.Shape
          extended by sofia.graphics.ImageShape
              extended by sofia.micro.Actor
                  extended by sofia.micro.ScriptableActor
All Implemented Interfaces:
Script
Direct Known Subclasses:
Jeroo

public class ScriptableActor
extends Actor
implements Script

Represents an Actor 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 for an actor: 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 actor).

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

Constructor Summary
ScriptableActor()
          Create a new scriptable actor.
ScriptableActor(boolean scaleToCell)
          Create a new scriptable ctor.
ScriptableActor(java.lang.String nickName)
          Create a new scriptable actor.
ScriptableActor(java.lang.String nickName, boolean scaleToCell)
          Create a new scriptable actor.
 
Method Summary
protected  void _gf_addToWorld(int x, int y, greenfoot.sofiainternal.World world)
           
 void act()
          This implementation of the act method executes one action in this actor's script.
 Script getScript()
          Get the script associated with this actor.
 void move(int distance)
          Move this actor the specified distance in the direction it is currently facing.
 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 setGridLocation(int x, int y)
          Assign a new location for this actor.
 void setGridX(int x)
          Assign a new horizontal (x-axis) location for this actor.
 void setGridY(int y)
          Assign a new vertical (y-axis) location for this actor.
 void setRotation(double rotation)
          Set the rotation of this actor.
<MyActor extends ScriptableActor>
void
setScript(ActorScript<MyActor> script)
          Associate a script with this actor by providing an ActorScript object.
 void setScript(Script script)
          Associate a script with this actor by providing a Script object.
 void stopScript()
          Stop any currently executing script associated with this actor.
 void turn(double amount)
          Turn this actor by the specified amount (in degrees).
 void turnTowards(Actor target)
          Turn this actor to face towards another actor (in the same world).
 void turnTowards(int x, int y)
          Turn this actor to face towards a certain location.
 
Methods inherited from class sofia.micro.Actor
addedToWorld, addOther, getGridX, getGridY, getImage, getIntersectingObjects, getNeighbors, getNickName, getObjectsAtOffset, getObjectsInRange, getOneIntersectingObject, getOneObjectAtOffset, getShape, getWorld, remove, setBounds, setImage, setImage, setNickName, setPosition, setPosition, setPositionAnchor, setPositionAnchor, setX, setY, toString
 
Methods inherited from class sofia.graphics.ImageShape
getSourceBounds, setBitmap, setSourceBounds, setSourceBounds
 
Methods inherited from class sofia.graphics.Shape
conditionallyRelayout, conditionallyRepaint, contains, contains, extendsOutside, getAlpha, getBounds, getColor, getHeight, getPosition, getPositionAnchor, getRotation, getRotationPivot, getShapeParent, getWidth, getX, getY, getZIndex, intersects, isInFrontOf, isVisible, move, notifyParentOfPositionChange, onBoundsResolved, rotateBy, setAlpha, setColor, setRotation, setRotation, setVisible, setZIndex
 
Methods inherited from class greenfoot.sofiainternal.Actor
_gf_getBoundingXs, _gf_getBoundingYs, _gf_getDelegate, _gf_getImage, _gf_getIntersectingObjects, _gf_getNeighbours, _gf_getObjectsAtOffset, _gf_getObjectsInRange, _gf_getOneIntersectingObject, _gf_getOneObjectAtOffset, _gf_getRotation, _gf_getX, _gf_getY, _gf_setImage, _gf_setImage, _gf_setLocation, _gf_setRotation, _gf_turn, addedToWorld, containsPoint, intersects
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ScriptableActor

public ScriptableActor()
Create a new scriptable actor. By default, this actor's image will be scaled to the size of a single grid cell, preserving aspect ratio.


ScriptableActor

public ScriptableActor(java.lang.String nickName)
Create a new scriptable actor. By default, this actor's image will be scaled to the size of a single grid cell, preserving aspect ratio.

Parameters:
nickName - The nickname for this actor.

ScriptableActor

public ScriptableActor(boolean scaleToCell)
Create a new scriptable ctor.

Parameters:
scaleToCell - If true, the Actor's image will be scaled to the dimensions of a single World grid cell, while preserving aspect ratio. If false, the image will be sized relative to the underlying bitmap or shape.

ScriptableActor

public ScriptableActor(java.lang.String nickName,
                       boolean scaleToCell)
Create a new scriptable actor.

Parameters:
nickName - The nickname for this actor.
scaleToCell - If true, the Actor's image will be scaled to the dimensions of a single World grid cell, while preserving aspect ratio. If false, the image will be sized relative to the underlying bitmap or shape.
Method Detail

act

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

If, on the other hand, you want to combine the features of a scriptable actor 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 actor will no longer obey its assigned script.

Overrides:
act in class Actor

move

public void move(int distance)
Move this actor the specified distance in the direction it is currently facing.

The direction can be set using the Actor.setRotation(double) method.

Overrides:
move in class Actor
Parameters:
distance - The distance to move (in cell-size units). A negative value will move backwards.
See Also:
Actor.setGridLocation(int, int)

turn

public void turn(double amount)
Turn this actor by the specified amount (in degrees).

Overrides:
turn in class Actor
Parameters:
amount - The number of degrees to turn. Positive values turn clockwise.
See Also:
Actor.setRotation(double)

turnTowards

public void turnTowards(int x,
                        int y)
Turn this actor to face towards a certain location.

Overrides:
turnTowards in class Actor
Parameters:
x - The x-coordinate of the cell to turn towards.
y - The y-coordinate of the cell to turn towards.

turnTowards

public void turnTowards(Actor target)
Turn this actor to face towards another actor (in the same world).

Overrides:
turnTowards in class Actor
Parameters:
target - The actor to turn towards.

setRotation

public void setRotation(double rotation)
Set the rotation of this actor. Rotation is expressed as a degree value, range (0..359). Zero degrees is to the east (right-hand side of the world), and the angle increases clockwise.

Overrides:
setRotation in class Actor
Parameters:
rotation - The new rotation amount, expressed as an absolute angle in degrees.

setGridX

public void setGridX(int x)
Assign a new horizontal (x-axis) location for this actor. This moves the actor to the specified x-axis location, without changing its y-axis location. The location is specified as the x-coordinate of a cell column in the world.

Overrides:
setGridX in class Actor
Parameters:
x - The location to move to on the x-axis.
See Also:
Actor.setGridLocation(int, int)

setGridY

public void setGridY(int y)
Assign a new vertical (y-axis) location for this actor. This moves the actor to the specified y-axis location, without changing its x-axis location. The location is specified as the y-coordinate of a cell row in the world.

Overrides:
setGridY in class Actor
Parameters:
y - The location to move to on the y-axis.
See Also:
Actor.setGridLocation(int, int)

setGridLocation

public void setGridLocation(int x,
                            int y)
Assign a new location for this actor. This moves the actor to the specified location. The location is specified as the coordinates of a cell in the world.

If this method is overridden it is important to call this method as super.setLocation(x,y) from the overriding method, to avoid infinite recursion.

Overrides:
setGridLocation in class Actor
Parameters:
x - The location to move to on the x-axis.
y - The location to move to on the y-axis.
See Also:
Actor.move(int)

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 <MyActor extends ScriptableActor> void setScript(ActorScript<MyActor> script)
Associate a script with this actor by providing an ActorScript 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 actor.

Type Parameters:
MyActor - The type of actor this script is written for, which should be the same as this actor's type (or one of its supertypes).
Parameters:
script - The script to activate.

setScript

public void setScript(Script script)
Associate a script with this actor 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 actor.

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.


_gf_addToWorld

protected void _gf_addToWorld(int x,
                              int y,
                              greenfoot.sofiainternal.World world)
Overrides:
_gf_addToWorld in class greenfoot.sofiainternal.Actor


Greenfoot homepage