doodle/dev-assets/doodads/buttons/sticky.js
Noah Petherbridge 99eab19c5b 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.
2019-06-23 17:34:11 -07:00

30 lines
556 B
JavaScript

function main() {
console.log("%s initialized!", Self.Doodad.Title);
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;
}
// Verify they've touched the button.
if (e.Overlap.Y + e.Overlap.H < 24) {
return;
}
Self.ShowLayer(1);
pressed = true;
Message.Publish("power", true);
});
}