Script Classes for Adventure Games/Motion Classes
Author: Jeff Stephenson
- 1 The Motion Classes
- 1.1 Motion
- 1.2 MoveTo
- 1.3 Wander
- 1.4 Follow
- 1.5 Chase
- 1.6 Jump
- 1.7 JumpTo
- 1.8 Orbit
- 1.9 Path
- 1.10 RelPath
- 1.11 Example
The Motion Classes
The sub-classes of class Motion implement the various kinds of motion which Actors can execute.
The Motion class is the basis for all the specialized motions. If a caller is specified in the setMotion: for a motion, it will be notified of the motion's completion by being sent the cue: message.
yThe coordinates towards which the Actor is moving.
clientThe Actor being moved by the instance of motion.
callerThe object to cue: when the motion is complete.
b-incrThese properties are used internally by the modified Bresenham line algorithm which moves the Actors.
moveDoneExecuted when the motion completes or is blocked. Cue:s the caller, if there was one, and dispose:s of the motion class.
The MoveTo class just moves an object to a particular position. An Actor is moved to a position with (actor setMotion: MoveTo x y [caller]).
The Wander motion class implements a random wander for Actors. It is invoked for an Actor by (actor setMotion: Wander [distance]) where the optional argument, distance, is the maximum distance of a leg of the wander (default is 20 pixels). This motion never completes — the Actor will keep wandering until a new motion type is set.
distanceThe maximum distance to be wandered on a given leg of the wander.
This class is for making one Actor follow another. It is invoked for an Actor with (actor setMotion: Follow anotherActor [distance]), where anotherActor is the Actor to be followed and the optional distance specifies how closely to follow. If actor is further from anotherActor than distance, it will start moving towards anotherActor. Otherwise it will stop moving. The default distance is 15 pixels. As for Wander, this motion never completes.
whoThe ID of the Actor being followed.
distanceThe distance to try and maintain from the Actor specified by who.
This class implements the concept of trying to catch another Actor. It is invoked with (actor setMotion: Chase anotherActor distance [caller]). As in Follow, anotherActor is the Actor to chase. Distance is the distance from anotherActor at which it is considered to be caught. When anotherActor has been caught, caller is cue:ed.
whoThe ID of the Actor being chased.
distanceThe distance from who at which who is considered caught.
This class simulates motion under a gravitational field. It is useful to make an Actor simultate a falling motion. The fall is straight down by default. This simulates, for example, falling off of a cliff. A horizontal gravitation can also be simulated by setting the gx property.
xx coord of finish. This property is set by the init method to reflect to clients velocity.
yy coord of finish. This property is also set internally.
gxgravitational acceleration in pixels/(animation cycle)**2. The default is no horizontal gravity, or 0.
gygravitational acceleration in pixels/(animation cycle)**2. This is set according to how intense a gravitation field you wish to have simulated. The default is 3 pixels per (animation cycle)**2, so after 1 cycle the Actor would be moving 3 pixels/animation cycle 6 after 2 cycles etc.
xStephorizontal step size. Used by Jump to calculate next position.
yStepvertical step size. Used by Jump to calculate next position.
signalsave area for client's signal bits. Don't set this property.
illegalBitssave area for illegal bits of client. Don't set this property.
The following two properties, when true, indicate that we are to check for motion completion only after the apogee of the motion. This is necessary if we are to be able to jump onto things. Set these to FALSE if you do NOT want this behavior.
setTestprivate — set up the jump completion test.
This class sets up a Jump which will reach a certain x,y position. The kernel call (SetJump) does this in order to use long integer arithmetic. The Motion can the cue: a caller to signal then completion of the jump.
initThis method is called by the Actors setMotion method. A JumpTo is initiated by, (actorNam. setMotion: JumpTo x y whoCares) This will make an Actor jump to the coordinates x y, and then cue whoCares.
This class sets up an elliptical path for an Actor to follow. This allows the simulation of an object orbiting around another object. This mover takes into account the global variable perspective.
centerObjThe object (Feature View PicView Prop or Actor) to orbit around.
radiusThe radius of the orbit along its major axis (the widest part).
xTiltThe horizontal tilt of ellipse. 0 would be circular and 90 would be edge on.
yTiltThe horizontal tilt of ellipse. 0 would be circular and 90 would be edge on.
angleStepAngle degree increment per animation cycle.
windingThe direction of the orbit. clockwise=1 counterclockwise=-1
curAngleWhere the Actor will be along the Orbit. 0=north, 90=east etc.
initThis method is called by the Actors setMotion method. a JumpTo is initiated by,Code:(anActor setMotion: Orbit ;class Orbit or an instance of it theCtrObj ;some object with x and y properties theRadius ;of the orbit theXTilt ;counterclockwise from x-axis in degrees theYTilt ;same from y-axis theStep ;in degrees, default is 10 theWinding ;clockwise=1 counterclockwise=-1 theAngle ;0=north, etc. )
This class gives the Actor a Path to follow and allows cue'ing of an object at the intermediate points as well as at the end of the path. It requires less memory than a Script and is also easier to use. The points of the Path are specified in a local array.
intermediateObject to cue at intermediate endpoints.
valueindex into path array
atReturns the nth element of control array. This method must be redefined to point to the users path array.
nextMove to next point in path. This method is used internally.
atEndReturns TRUE if at the end of the path. This method is used internally.
To create a square Path (assuming that ego is positioned at 100,100) we do the following:Code:(local sPath = [ 100 60 160 60 160 100 100 100 PATHEND ] ) (instance squarePath of Path (method (at n) (return [sPath n]) ) )
The following will make ego walk the path once and cue obj1 at completion of entire path and will cue obj2 at all intermediate pts.
(ego posn:100 100, setMotion: squarePath obj1 obj2)
This class gives the Actor a Path to follow and allows cue'ing of an object at the intermediate points as well as at the end of the path. It requires less memory than a Script and is also easier to use. The relative offsets of the Path are specified in a local array.
The following uses a RelPath to make ego do loop-the-loops across the screen:
Code:(local lPath = [ -10 -10 0 -10 1 -10 1 0 1 10 0 10 -10 10 PATHEND ] ) (instance loopPath of Path (method (at n) (return [lPath n]) ) )
Code:(instance rm1 of Room (properties picture 1 ) (method (init) (ego posn: 60 100, init:, setMotion: aPath self ) (super init:) ) (method (cue) (ego setMotion: aPath self) ) )