diff --git a/dev-assets/doodads/on-off/state-block-blue.js b/dev-assets/doodads/on-off/state-block-blue.js index 0b287e3..f09f5cc 100644 --- a/dev-assets/doodads/on-off/state-block-blue.js +++ b/dev-assets/doodads/on-off/state-block-blue.js @@ -7,15 +7,10 @@ function main() { Message.Subscribe("broadcast:state-change", function(newState) { state = !newState; - console.warn("BLUE BLOCK Received state=%+v, set mine to %+v", newState, state); // Layer 0: ON // Layer 1: OFF - if (state) { - Self.ShowLayer(0); - } else { - Self.ShowLayer(1); - } + Self.ShowLayer(state ? 0 : 1); }); Events.OnCollide(function(e) { diff --git a/dev-assets/doodads/on-off/state-block-orange.js b/dev-assets/doodads/on-off/state-block-orange.js index 634d5e8..dfea8b7 100644 --- a/dev-assets/doodads/on-off/state-block-orange.js +++ b/dev-assets/doodads/on-off/state-block-orange.js @@ -7,15 +7,10 @@ function main() { Message.Subscribe("broadcast:state-change", function(newState) { state = newState; - console.warn("ORANGE BLOCK Received state=%+v, set mine to %+v", newState, state); // Layer 0: OFF // Layer 1: ON - if (state) { - Self.ShowLayer(1); - } else { - Self.ShowLayer(0); - } + Self.ShowLayer(state ? 1 : 0); }); Events.OnCollide(function(e) { diff --git a/dev-assets/doodads/on-off/state-button.js b/dev-assets/doodads/on-off/state-button.js index 51a2af3..8770b08 100644 --- a/dev-assets/doodads/on-off/state-button.js +++ b/dev-assets/doodads/on-off/state-button.js @@ -1,14 +1,22 @@ // State Block Control Button + +// Button is "OFF" by default. +var state = false; + function main() { - console.log("%s initialized!", Self.Doodad.Title); + console.log("%s ID '%s' initialized!", Self.Doodad.Title, Self.ID()); Self.SetHitbox(0, 0, 33, 33); // When the button is activated, don't keep toggling state until we're not // being touched again. var colliding = false; - // Button is "OFF" by default. - var state = false; + // If we receive a state change event from a DIFFERENT on/off button, update + // ourself to match the state received. + Message.Subscribe("broadcast:state-change", function(value) { + state = value; + showSprite(); + }); Events.OnCollide(function(e) { if (colliding) { @@ -17,24 +25,16 @@ function main() { // Only trigger for mobile characters. if (e.Actor.IsMobile()) { - console.log("Mobile actor %s touched the on/off button!", e.Actor.Actor.Filename); - // Only activate if touched from the bottom or sides. if (e.Overlap.Y === 0) { - console.log("... but touched the top!"); return false; } colliding = true; - console.log(" -> emit state change"); state = !state; Message.Broadcast("broadcast:state-change", state); - if (state) { - Self.ShowLayer(1); - } else { - Self.ShowLayer(0); - } + showSprite(); } // Always a solid button. @@ -45,3 +45,12 @@ function main() { colliding = false; }) } + +// Update the active layer based on the current button state. +function showSprite() { + if (state) { + Self.ShowLayer(1); + } else { + Self.ShowLayer(0); + } +}