From 03f36be63e8637d97915a98bda910ee50fa70907 Mon Sep 17 00:00:00 2001 From: Noah Petherbridge Date: Thu, 18 Apr 2019 18:15:05 -0700 Subject: [PATCH] Script Timers, Multiple Doodad Frames * 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) --- dev-assets/doodads/buttons/button.js | 15 +++++-- dev-assets/doodads/buttons/sticky.js | 5 +-- dev-assets/doodads/doors/electric-door.js | 35 +++++++++++++++ dev-assets/doodads/doors/keys.js | 5 +++ dev-assets/doodads/doors/locked-door.js | 5 +++ dev-assets/doodads/trapdoors/down.js | 55 +++++++++++++++++++++++ 6 files changed, 114 insertions(+), 6 deletions(-) create mode 100644 dev-assets/doodads/doors/electric-door.js create mode 100644 dev-assets/doodads/doors/keys.js create mode 100644 dev-assets/doodads/doors/locked-door.js create mode 100644 dev-assets/doodads/trapdoors/down.js diff --git a/dev-assets/doodads/buttons/button.js b/dev-assets/doodads/buttons/button.js index f640ca0..83318fb 100644 --- a/dev-assets/doodads/buttons/button.js +++ b/dev-assets/doodads/buttons/button.js @@ -1,8 +1,17 @@ function main() { - console.log("Sticky Button initialized!"); + console.log("%s initialized!", Self.Doodad.Title); + + var timer = 0; Events.OnCollide( function() { - console.log("Touched!"); - Self.Canvas.SetBackground(RGBA(255, 153, 0, 153)) + if (timer > 0) { + clearTimeout(timer); + } + + Self.ShowLayer(1); + timer = setTimeout(function() { + Self.ShowLayer(0); + timer = 0; + }, 200); }) } diff --git a/dev-assets/doodads/buttons/sticky.js b/dev-assets/doodads/buttons/sticky.js index f640ca0..cf4fe74 100644 --- a/dev-assets/doodads/buttons/sticky.js +++ b/dev-assets/doodads/buttons/sticky.js @@ -1,8 +1,7 @@ function main() { - console.log("Sticky Button initialized!"); + console.log("%s initialized!", Self.Doodad.Title); Events.OnCollide( function() { - console.log("Touched!"); - Self.Canvas.SetBackground(RGBA(255, 153, 0, 153)) + Self.ShowLayer(1); }) } diff --git a/dev-assets/doodads/doors/electric-door.js b/dev-assets/doodads/doors/electric-door.js new file mode 100644 index 0000000..dedae0d --- /dev/null +++ b/dev-assets/doodads/doors/electric-door.js @@ -0,0 +1,35 @@ +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 + }) +} diff --git a/dev-assets/doodads/doors/keys.js b/dev-assets/doodads/doors/keys.js new file mode 100644 index 0000000..ab81605 --- /dev/null +++ b/dev-assets/doodads/doors/keys.js @@ -0,0 +1,5 @@ +function main() { + Events.OnCollide(function(e) { + Self.Destroy(); + }) +} diff --git a/dev-assets/doodads/doors/locked-door.js b/dev-assets/doodads/doors/locked-door.js new file mode 100644 index 0000000..7d1ee7a --- /dev/null +++ b/dev-assets/doodads/doors/locked-door.js @@ -0,0 +1,5 @@ +function main() { + Events.OnCollide(function(e) { + Self.ShowLayer(1); + }); +} diff --git a/dev-assets/doodads/trapdoors/down.js b/dev-assets/doodads/trapdoors/down.js new file mode 100644 index 0000000..a38b70d --- /dev/null +++ b/dev-assets/doodads/trapdoors/down.js @@ -0,0 +1,55 @@ +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 animationDelay = 8; // delay ticks at the end before reversing, in + // multiples of animationSpeed + var delayCountdown = 0; + var animating = false; // true if animation is actively happening + + console.warn("Trapdoor has %d frames", frames); + + // Animation interval function. + setInterval(function() { + if (!animating) { + return; + } + + // At the end of the animation (door is open), delay before resuming + // the close animation. + if (delayCountdown > 0) { + delayCountdown--; + return; + } + + // Advance the frame forwards or backwards. + frame += animationDirection; + if (frame >= frames) { + // Reached the last frame, start the pause and reverse direction. + delayCountdown = animationDelay; + animationDirection = -1; + + // also bounds check it + frame = frames - 1; + } + + if (frame < 0) { + // reached the start again + frame = 0; + animationDirection = 1; + animating = false; + } + + Self.ShowLayer(frame); + }, animationSpeed); + + Events.OnCollide( function() { + animating = true; // start the animation + }) +}