This is the manual for one of the add-ons that come with Game Palette. The core library is described in the core manual, and the other add-on in the CheckerboardMap manual.
The LinkedSprite add-on joins sprites together into a hierarchy, so that moving one moves the others. Think of a robot made of separate pictures: wheels, a body, two arms, and a head. The wheels are the root of the hierarchy, the body is linked to the wheels, and the arms and head are linked to the body. Your game moves and turns only the wheels, and the rest of the robot follows.
A linked sprite doesn't follow rigidly, though. Each one keeps its place relative to its parent, turning with it as the parent turns, but it follows with a delay, its drag, and may lag behind its place only within limits that you set. So the parts of the robot sway and trail behind as it moves, and catch up when it stops, as though loosely joined. Each part can have parts of its own, to any depth.
The Linked sprites demo is the robot just described: drive it about with WASD, and turn it with Q and E.
The add-on is in the file linkedsprite.js. Load it
after gamepalette.js (see
Using an add-on):
<script type="text/javascript"
src="gamepalette.js"></script>
<script type="text/javascript"
src="linkedsprite.js"></script>
It adds one class, GAMEPALETTE.LinkedSprite. Your
game can check that the add-on is loaded with
GAMEPALETTE.hasAddOn("LinkedSprite") (see
hasAddOn).
Start by making the sprites as usual, each with its own image, and put each one where it belongs relative to the others: the body just above the wheels, the arms either side of the body, and so on. The handle of each image matters here, as the point the sprite turns about: in the demo, each arm's handle is at its shoulder, so the arms swing from the shoulders. Use z to say which parts are drawn in front of which.
s.wheels = new GAMEPALETTE.Sprite(s.engine, s.wheelsImage, 0, 0, 4);
s.body = new GAMEPALETTE.Sprite(s.engine, s.bodyImage, 0, 12, 0);
s.leftArm = new GAMEPALETTE.Sprite(s.engine, s.leftArmImage,
-20, -17, 3);
s.rightArm = new GAMEPALETTE.Sprite(s.engine, s.rightArmImage,
20, -17, 2);
s.head = new GAMEPALETTE.Sprite(s.engine, s.headImage, 0, 3, 1);
Then make a GAMEPALETTE.LinkedSprite for each
sprite, root first, giving each one after the root its parent's
LinkedSprite (see the constructor):
s.wheelsLink = new GAMEPALETTE.LinkedSprite(s.engine, s.wheels);
s.bodyLink = new GAMEPALETTE.LinkedSprite(s.engine, s.body,
s.wheelsLink);
s.leftArmLink = new GAMEPALETTE.LinkedSprite(s.engine, s.leftArm,
s.bodyLink);
s.rightArmLink = new GAMEPALETTE.LinkedSprite(s.engine, s.rightArm,
s.bodyLink);
s.headLink = new GAMEPALETTE.LinkedSprite(s.engine, s.head,
s.bodyLink);
When a LinkedSprite is made, it notes where its sprite is relative to its parent's sprite: how far away, in the direction the parent is facing, how much nearer or farther in z, and how much it is turned compared with the parent. That is the sprite's place, which it keeps from then on. So put the sprites where they belong before linking them. (They needn't be active yet.)
Next, set how each part follows its parent, with its
positionalDrag,
rotationalDrag,
and limits, such as
minimumX and
maximumX. These are
described in the next chapter. Each LinkedSprite's settings say
how it follows its parent, so the root's are never
used.
s.bodyLink.positionalDrag = 5;
s.bodyLink.rotationalDrag = 4;
s.bodyLink.minimumX = -6;
s.bodyLink.maximumX = 6;
s.bodyLink.minimumY = -3;
s.bodyLink.maximumY = 12;
Finally, in the logic function that runs the game, move the root
sprite and then call the root's
animateChildren,
once each step. That moves every other sprite in the hierarchy, a
parent before its children, so the whole robot keeps up:
engine.addLogicFunction("mainLoop", (s) =>
{
if(s.keyLeft.isPressed) s.wheels.x -= 6;
if(s.keyRight.isPressed) s.wheels.x += 6;
if(s.keyRotateLeft.isPressed) s.wheels.rotation -= 0.1;
if(s.keyRotateRight.isPressed) s.wheels.rotation += 0.1;
s.wheelsLink.animateChildren();
});
Call animateChildren only on the root, and only
once a step. Called on a part below the root, it moves that part
as well as its children, so calling it on both the root and a part
would move that part twice.
A LinkedSprite moves its sprite by setting the sprite's
x,
y,
z, and
rotation.
Nothing else about the sprite is linked: each part keeps its own
image, animation, scale, transparency, and
isActive
setting, so a part can be hidden, or change its picture, on its
own. A parent's scale doesn't stretch the distances to its
children.
The sprites are still ordinary sprites, so the camera can follow
any of them (see
autoCameraSprite), and with the engine's
perspectiveView
on, the parts' different depths show as well.
Each step, a linked sprite works out its place from where its parent's sprite now is and which way it faces. Then it moves toward that place, by an amount set by its drag, and finally keeps itself within its limits of its place.
A linked sprite moves toward its place as though on a spring:
it gathers speed toward its place, and the drag slows it. Each
step, its new speed is its last speed plus how far it is from its
place, all divided by the drag. The position (x, y, and z) uses
positionalDrag,
and the rotation uses
rotationalDrag.
A drag of 4 or more gives a smooth follow: the sprite eases into its place and stops there, without overshooting. The larger the drag, the more slowly it catches up. At 4, a sprite left 20 pixels behind is within 2 pixels of its place after about 6 steps; at 5, after about 8; and at 10, after about 20. The demo uses drags of 4 and 5.
A drag of less than 4 lets the sprite overshoot its place and swing back, like a bouncy spring, settling sooner the closer the drag is to 4. At 1, there is nothing to slow it at all, and the sprite never settles: once it has been left behind, it keeps swinging back and forth through its place until the parent moves again. Below 1, each swing is larger than the last. The default drag is 1, so set a drag for each part that you link.
To have a part follow exactly, with no lag at all, set its limits (below) to 0.
Because the sprite is pulled toward its place each step, moving a linked sprite yourself only nudges it: on the following steps it springs back to its place. This can be put to use, for instance to make a robot's head jolt when it's hit.
The limits say how far a linked sprite may lag behind its place. They are measured as its place minus where it is, in the parent's own directions: x is the way the parent faces when its rotation is 0, turned as the parent turns, and y is at right angles to that. So for a robot that turns, a limit on how far the head may sway sideways stays sideways to the robot whichever way it faces.
A positive lag in x means the sprite is short of its place, on
the lower-x side of it. So
maximumX is how far the
sprite may trail on that side, and
minimumX, usually
negative, is how far it may run ahead on the other side. For
example, with minimumX at -6 and
maximumX at 6, the sprite stays within 6 pixels of its
place across the parent, either way. It is the same for y,
with minimumY and
maximumY, and for z,
with minimumZ and
maximumZ. In the
demo, the head's minimumY is 0 and its
maximumY is 12: it may lag up to 12 pixels above its
place (lower y), but never sink below it.
Rotation has its limits too,
minimumRotation
and maximumRotation,
in radians. A positive lag means the sprite is turned less far
clockwise than its place.
A limit of null, the default, means no limit that
way. A limit of 0 on both sides holds the sprite exactly at its
place in that direction, whatever its drag. When a sprite is held
at a limit, it moves along with its place, so it doesn't build up
speed while it's held.
A LinkedSprite's
parent and
children tell you
how the hierarchy is joined.
To break a part off, call its
unlink. It stays where
it is, no longer follows its parent, and becomes the root of a
hierarchy of its own, with any children it has still linked to it.
From then on your game moves it, and if it has children, calls its
own animateChildren each step too. For example, to
knock a robot's arm off and send it flying:
s.leftArmLink.unlink();
s.flyingArm = s.leftArm; // moved by the main loop from now on
A LinkedSprite can't be given a new parent, nor its place moved. To attach a sprite somewhere else, or at a different place, unlink its LinkedSprite, put the sprite where it should be, and make a new LinkedSprite for it with its new parent (and new ones for its children, if it has any). Don't make a second LinkedSprite for a sprite whose first is still linked, or both will move it.
Linked sprites can be kept in the state object and saved with
the engine's
saveState,
like anything else: each LinkedSprite comes back from
loadState
with its sprite, its parent and children, its settings, and how
fast it was moving, so the hierarchy carries on as it was. The
Linked sprites demo saves the
robot with the 1 key and puts it back with 2, even after the page
has been reloaded.
This is the full description of the add-on's class. For the classes it works with, see the core manual's reference.
A LinkedSprite links a GAMEPALETTE.Sprite to a parent, so that the sprite follows the parent's sprite about, or is the root of a hierarchy of them.
new GAMEPALETTE.LinkedSprite(engine, sprite,
parentLinkedSprite = null)
Creates a LinkedSprite for engine (your
GAMEPALETTE.Engine) that
moves sprite, a
GAMEPALETTE.Sprite. With a
parentLinkedSprite, another LinkedSprite, the new one
is linked to it as one of its
children, and the
sprite's present position, depth, and rotation relative to the
parent's sprite become its place, which it keeps (see
Building a hierarchy). Without one, or
with null, the new LinkedSprite is a root.
An engine that isn't a GAMEPALETTE.Engine, a
sprite that isn't a GAMEPALETTE.Sprite, or a
parentLinkedSprite that is neither a
GAMEPALETTE.LinkedSprite nor null, throws a
TypeError.
The new LinkedSprite has drags of 1 and no limits. The sprite
doesn't move until the root's
animateChildren
is called.
s.wheelsLink = new GAMEPALETTE.LinkedSprite(s.engine, s.wheels);
s.bodyLink = new GAMEPALETTE.LinkedSprite(s.engine, s.body,
s.wheelsLink);
Setting a property to a value of the wrong type throws
a TypeError, and to a value out of range
a RangeError; in either case the property keeps its
old value. Changes take effect the next time
animateChildren
runs. The drags and limits say how this LinkedSprite follows its
parent, and aren't used on a root.
sprite passed to the
constructor
The sprite this LinkedSprite moves.
parentLinkedSprite
passed to the constructor, or null
The LinkedSprite this one follows, or null if this
one is a root. It becomes null when this one is
unlinked.
The LinkedSprites that follow this one directly, in the order
they were linked, which is the order they are moved in. (Their own
children aren't included.) The array is a copy, so changing it
doesn't change the hierarchy: link a child by making it with this
one as its parent, and remove one with its
unlink.
// Hide the robot's head and arms along with its body
for(let part of s.bodyLink.children)
part.sprite.isActive = false;
How slowly the sprite's position (x, y, and z) follows its
place: each step, its speed toward its place becomes its last
speed plus how far it is from its place, divided by this. At 4 or
more, the sprite eases into its place without overshooting, more
slowly the larger it is. Below 4 it overshoots and swings back,
and at 1, the default, it never settles. See
Drag. A value that isn't a finite
number throws a TypeError, and one that isn't greater
than 0 a RangeError.
How slowly the sprite's rotation follows its place, just as
positionalDrag
does for its position, and checked in the same way. Giving it a
different value from positionalDrag lets a part keep
up with its parent's movement but lag as it turns, or the other way
round.
The least and greatest the sprite's lag in x may be: its place's
x minus its own, measured along the parent's x direction, which
turns with the parent. A positive lag means the sprite is short of
its place, on its lower-x side, so maximumX limits how
far it may trail that way and minimumX (usually
negative) how far it may go past its place the other way.
null means no limit. See
Limits. A value that is neither a
finite number nor null throws a
TypeError. The two aren't checked against each other,
so keep the minimum no greater than the maximum.
s.headLink.minimumX = -12; // sway up to 12 pixels either way
s.headLink.maximumX = 12;
The least and greatest the sprite's lag in y may be, measured
along the parent's y direction, which turns with the parent, just
as minimumX and
maximumX are for x, and checked in the same
way.
The least and greatest the sprite's lag in
z may be: its place's z
minus its own. A positive lag means the sprite is nearer than its
place. They are checked as
minimumX is. To keep
the parts of something drawn in the same order in front of each
other, set both to 0, so that the depths follow exactly.
The least and greatest the sprite's lag in
rotation may be:
its place's rotation minus its own, in radians. A positive lag
means the sprite is turned less far clockwise than its place.
They are checked as
minimumX is.
Moves the sprites of this LinkedSprite's whole hierarchy below
it one step, each toward its place (see
How a linked sprite follows): first its
children, in the order of
children, each one
followed by its own children, and so on down. Call it on the root,
once each step of your logic function, after moving the root's
sprite. Called on a LinkedSprite that has a parent, it moves that
one toward its own place first, then its children.
s.wheels.x += 6;
s.wheelsLink.animateChildren();
Takes this LinkedSprite out of its parent's
children, making it
the root of its own hierarchy, along with its children. Its
sprite stays where it is, and stops moving: from then on the game
moves it, and calls this one's
animateChildren
if it has children that should still follow it. Unlinking a root
does nothing. A LinkedSprite can't be linked again; see
Changing the hierarchy.