From f0393fb4c7b11fc5673de0b1e477e3169023151d Mon Sep 17 00:00:00 2001 From: Noah Petherbridge Date: Sun, 23 Jun 2019 17:30:12 -0700 Subject: [PATCH] Pub/Sub Messages Between Linked Actors (JavaScript) * Implement the pub/sub message passing system that lets the JavaScript VM of one actor (say, a Button) send messages to other linked actors in the level (say, an Electric Door) * Buttons now emit a "power(true)" message while pressed and "power(false)" when released. Sticky Buttons do not release and so do not send the power(false) message. * Electric Doors listen for the "power" event and open or close themselves based on the boolean value received. * If a Sticky Button receives power and is currently pressed down, it will pop back up (reset to "off" position) and notify its linked actors that they have lost power too. So if a Sticky Button held an Electric Door open, and another Button powers the Sticky Button, it would pop back up and also close the Electric Door. --- dev-assets/doodads/azulian/azulian.js | 3 -- dev-assets/doodads/buttons/button.js | 12 +++---- dev-assets/doodads/buttons/sticky.js | 10 ++++++ dev-assets/doodads/doors/electric-door.js | 38 +++++++++++++++++------ dev-assets/doodads/doors/locked-door.js | 12 +++---- 5 files changed, 50 insertions(+), 25 deletions(-) diff --git a/dev-assets/doodads/azulian/azulian.js b/dev-assets/doodads/azulian/azulian.js index 40291ca..5178cc7 100644 --- a/dev-assets/doodads/azulian/azulian.js +++ b/dev-assets/doodads/azulian/azulian.js @@ -1,9 +1,6 @@ function main() { log.Info("Azulian '%s' initialized!", Self.Doodad.Title); - - Self.Canvas.SetBackground(RGBA(0, 153, 255, 100)); - var playerSpeed = 12; var gravity = 4; var Vx = Vy = 0; diff --git a/dev-assets/doodads/buttons/button.js b/dev-assets/doodads/buttons/button.js index 4cb1d9e..5ae0a17 100644 --- a/dev-assets/doodads/buttons/button.js +++ b/dev-assets/doodads/buttons/button.js @@ -6,11 +6,10 @@ function main() { 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)); + Message.Publish("power", true); if (timer > 0) { clearTimeout(timer); @@ -19,12 +18,13 @@ function main() { Self.ShowLayer(1); timer = setTimeout(function() { Self.ShowLayer(0); + Message.Publish("power", false); 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)); - }) + // 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 a3e1373..bd29aee 100644 --- a/dev-assets/doodads/buttons/sticky.js +++ b/dev-assets/doodads/buttons/sticky.js @@ -3,6 +3,15 @@ function main() { var pressed = false; + // When a sticky button receives power, it pops back up. + Message.Subscribe("power", function(powered) { + if (powered && pressed) { + Self.ShowLayer(0); + pressed = false; + Message.Publish("power", false); + } + }) + Events.OnCollide(function(e) { if (pressed) { return; @@ -15,5 +24,6 @@ function main() { Self.ShowLayer(1); pressed = true; + Message.Publish("power", true); }); } diff --git a/dev-assets/doodads/doors/electric-door.js b/dev-assets/doodads/doors/electric-door.js index b9ae2b7..cd028b7 100644 --- a/dev-assets/doodads/doors/electric-door.js +++ b/dev-assets/doodads/doors/electric-door.js @@ -6,24 +6,42 @@ function main() { var animating = false; var opened = false; - Events.OnCollide(function(e) { - if (animating || opened) { - return; - } + Self.SetHitbox(16, 0, 32, 64); + + Message.Subscribe("power", function(powered) { + console.log("%s got power=%+v", Self.Doodad.Title, powered); + + if (powered) { + if (animating || opened) { + return; + } - if (e.Overlap.X + e.Overlap.W >= 16 && e.Overlap.X < 48) { animating = true; Self.PlayAnimation("open", function() { opened = true; animating = false; }); + } else { + animating = true; + Self.PlayAnimation("close", function() { + opened = false; + animating = false; + }) + } + }); + + Events.OnCollide(function(e) { + if (e.InHitbox) { + if (!opened) { + return false; + } } }); Events.OnLeave(function() { - if (opened) { - Self.PlayAnimation("close", function() { - opened = false; - }); - } + // 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 fee9610..28f6d70 100644 --- a/dev-assets/doodads/doors/locked-door.js +++ b/dev-assets/doodads/doors/locked-door.js @@ -2,7 +2,7 @@ function main() { Self.AddAnimation("open", 0, [1]); var unlocked = false; - Self.Canvas.SetBackground(RGBA(0, 255, 255, 100)); + // Self.Canvas.SetBackground(RGBA(0, 255, 255, 100)); // Map our door names to key names. var KeyMap = { @@ -12,8 +12,8 @@ function main() { "Yellow Door": "Yellow Key" } - log.Warn("%s loaded!", Self.Doodad.Title); - console.log("%s Setting hitbox", Self.Doodad.Title); + // log.Warn("%s loaded!", Self.Doodad.Title); + // console.log("%s Setting hitbox", Self.Doodad.Title); Self.SetHitbox(16, 0, 32, 64); Events.OnCollide(function(e) { @@ -32,7 +32,7 @@ function main() { Self.PlayAnimation("open", null); } }); - Events.OnLeave(function(e) { - console.log("%s has stopped touching %s", e, Self.Doodad.Title) - }) + // Events.OnLeave(function(e) { + // console.log("%s has stopped touching %s", e, Self.Doodad.Title) + // }) }