Noah Petherbridge
258b2eb285
* CLI: fix the `doodad convert` command to share the same Palette when converting each frame (layer) of a doodad so subsequent layers find the correct color swatches for serialization. * Scripting: add timers and intervals to Doodad scripts to allow them to animate themselves or add delayed callbacks. The timers have the same API as a web browser: setTimeout(), setInterval(), clearTimeout(), clearInterval(). * Add support for uix.Actor to change its currently rendered layer in the level. For example a Button Doodad can set its image to Layer 1 (pressed) when touched by the player, and Trapdoors can cycle through their layers to animate opening and closing. * Usage from a Doodad script: Self.ShowLayer(1) * Default Doodads: added scripts for all Buttons, Doors, Keys and the Trapdoor to run their various animations when touched (in the case of Keys, destroy themselves when touched, because there is no player inventory yet)
36 lines
854 B
JavaScript
36 lines
854 B
JavaScript
function main() {
|
|
console.log("%s initialized!", Self.Doodad.Title);
|
|
|
|
var timer = 0;
|
|
|
|
// Animation frames.
|
|
var frame = 0;
|
|
var frames = Self.LayerCount();
|
|
var animationDirection = 1; // forward or backward
|
|
var animationSpeed = 100; // interval between frames when animating
|
|
var animating = false; // true if animation is actively happening
|
|
|
|
console.warn("Electric Door has %d frames", frames);
|
|
|
|
// Animation interval function.
|
|
setInterval(function() {
|
|
if (!animating) {
|
|
return;
|
|
}
|
|
|
|
// Advance the frame forwards or backwards.
|
|
frame += animationDirection;
|
|
if (frame >= frames) {
|
|
// Reached the last frame, start the pause and reverse direction.
|
|
animating = false;
|
|
frame = frames - 1;
|
|
}
|
|
|
|
Self.ShowLayer(frame);
|
|
}, animationSpeed);
|
|
|
|
Events.OnCollide( function() {
|
|
animating = true; // start the animation
|
|
})
|
|
}
|