From 13d40423cccd15da605ee356d1b9437d371cbfdb Mon Sep 17 00:00:00 2001 From: Noah Petherbridge Date: Mon, 6 May 2019 22:57:32 -0700 Subject: [PATCH] Improve OnCollide Doodad Script Handling * Events.OnCollide now receives a CollideEvent object, which makes available the .Actor who collided and the .Overlap rect which is zero-relative to the target actor. Doodad scripts can use the .Overlap to see WHERE in their own box the other actor has intruded. * Update the LockedDoor and ElectricDoor doodads to detect when the player has entered their inner rect (since their doors are narrower than their doodad size) * Update the Button doodads to only press in when the player actually touches them (because their sizes are shorter than their doodad height) * Update the Trapdoor to only trigger its animation when the board along its top has been touched, not when the empty space below was touched from the bottom. * Events.OnLeave now implemented and fires when an actor who was previously intersecting your doodad has left. * The engine detects when an event JS callback returns false. Eventually, the OnCollide can return false to signify the collision is not accepted and the actor should be bumped away as if they hit solid geometry. --- dev-assets/doodads/buttons/button.js | 15 +++++++++++++- dev-assets/doodads/buttons/sticky.js | 16 +++++++++++++-- dev-assets/doodads/doors/electric-door.js | 25 +++++++++++++++++------ dev-assets/doodads/doors/locked-door.js | 16 +++++++++++++++ dev-assets/doodads/trapdoors/down.js | 25 ++++++++++++++--------- 5 files changed, 78 insertions(+), 19 deletions(-) diff --git a/dev-assets/doodads/buttons/button.js b/dev-assets/doodads/buttons/button.js index 83318fb..4cb1d9e 100644 --- a/dev-assets/doodads/buttons/button.js +++ b/dev-assets/doodads/buttons/button.js @@ -3,7 +3,15 @@ function main() { var timer = 0; - Events.OnCollide( function() { + Events.OnCollide(function(e) { + // Verify they've touched the button. + if (e.Overlap.Y + e.Overlap.H < 24) { + Self.Canvas.SetBackground(RGBA(0, 255, 0, 153)); + return; + } + + Self.Canvas.SetBackground(RGBA(255, 255, 0, 153)); + if (timer > 0) { clearTimeout(timer); } @@ -13,5 +21,10 @@ function main() { Self.ShowLayer(0); timer = 0; }, 200); + }); + + Events.OnLeave(function(e) { + console.log("%s has stopped touching %s", e, Self.Doodad.Title) + Self.Canvas.SetBackground(RGBA(0, 0, 1, 0)); }) } diff --git a/dev-assets/doodads/buttons/sticky.js b/dev-assets/doodads/buttons/sticky.js index cf4fe74..a3e1373 100644 --- a/dev-assets/doodads/buttons/sticky.js +++ b/dev-assets/doodads/buttons/sticky.js @@ -1,7 +1,19 @@ function main() { console.log("%s initialized!", Self.Doodad.Title); - Events.OnCollide( function() { + var pressed = false; + + Events.OnCollide(function(e) { + if (pressed) { + return; + } + + // Verify they've touched the button. + if (e.Overlap.Y + e.Overlap.H < 24) { + return; + } + Self.ShowLayer(1); - }) + pressed = true; + }); } diff --git a/dev-assets/doodads/doors/electric-door.js b/dev-assets/doodads/doors/electric-door.js index b665d73..b9ae2b7 100644 --- a/dev-assets/doodads/doors/electric-door.js +++ b/dev-assets/doodads/doors/electric-door.js @@ -1,16 +1,29 @@ function main() { console.log("%s initialized!", Self.Doodad.Title); - var err = Self.AddAnimation("open", 100, [0, 1, 2, 3]); - console.error("door error: %s", err) + Self.AddAnimation("open", 100, [0, 1, 2, 3]); + Self.AddAnimation("close", 100, [3, 2, 1, 0]); var animating = false; + var opened = false; - Events.OnCollide(function() { - if (animating) { + Events.OnCollide(function(e) { + if (animating || opened) { return; } - animating = true; - Self.PlayAnimation("open", null); + if (e.Overlap.X + e.Overlap.W >= 16 && e.Overlap.X < 48) { + animating = true; + Self.PlayAnimation("open", function() { + opened = true; + animating = false; + }); + } }); + Events.OnLeave(function() { + if (opened) { + Self.PlayAnimation("close", function() { + opened = false; + }); + } + }) } diff --git a/dev-assets/doodads/doors/locked-door.js b/dev-assets/doodads/doors/locked-door.js index 75545f9..0c18d2f 100644 --- a/dev-assets/doodads/doors/locked-door.js +++ b/dev-assets/doodads/doors/locked-door.js @@ -3,6 +3,18 @@ function main() { var unlocked = false; Events.OnCollide(function(e) { + console.log("%s was touched by %s!", Self.Doodad.Title, e.Actor.ID()); + console.log("my box: %+v and theirs: %+v", Self.GetBoundingRect(), e.Actor.GetBoundingRect()); + console.warn("But the overlap is: %+v", e.Overlap); + console.log(Object.keys(e)); + + if (e.Overlap.X + e.Overlap.W >= 16 && e.Overlap.X < 48) { + Self.Canvas.SetBackground(RGBA(255, 0, 0, 153)); + } else { + Self.Canvas.SetBackground(RGBA(0, 255, 0, 153)); + return; + } + if (unlocked) { return; } @@ -10,4 +22,8 @@ function main() { unlocked = true; Self.PlayAnimation("open", null); }); + Events.OnLeave(function(e) { + console.log("%s has stopped touching %s", e, Self.Doodad.Title) + Self.Canvas.SetBackground(RGBA(0, 0, 1, 0)); + }) } diff --git a/dev-assets/doodads/trapdoors/down.js b/dev-assets/doodads/trapdoors/down.js index 209eedc..0764ccb 100644 --- a/dev-assets/doodads/trapdoors/down.js +++ b/dev-assets/doodads/trapdoors/down.js @@ -4,22 +4,27 @@ function main() { var timer = 0; var animationSpeed = 100; - var animating = false; + var opened = false; Self.AddAnimation("open", animationSpeed, ["down1", "down2", "down3", "down4"]); Self.AddAnimation("close", animationSpeed, ["down4", "down3", "down2", "down1"]); - Events.OnCollide( function() { - if (animating) { + Events.OnCollide( function(e) { + if (opened) { return; } - animating = true; + // Not touching the top of the door means door doesn't open. + if (e.Overlap.Y > 9) { + return; + } + + opened = true; Self.PlayAnimation("open", function() { - setTimeout(function() { - Self.PlayAnimation("close", function() { - animating = false; - }); - }, 3000); - }) + }); }); + Events.OnLeave(function() { + Self.PlayAnimation("close", function() { + opened = false; + }); + }) }