doodle/dev-assets/doodads/switches/switch.js
Noah Petherbridge 0518df226c New Doodad: Anvil
* The Anvil doodad is affected by gravity and becomes dangerous when
  falling. If it lands on the player character, you die! If it lands on
  any other mobile doodad, it destroys it! It can land on solid doodads
  such as the Electric Trapdoor and the Crumbly Floor. It will activate
  a Crumbly Floor if it lands on one, and can activate buttons and
  switches that it passes.
* JavaScript API: FailLevel(message) can be called from a doodad to kill
  the player character. The Anvil does this if it collides with the
  player while it's been falling.
2021-08-08 21:57:41 -07:00

46 lines
780 B
JavaScript

function main() {
// Switch has two frames:
// 0: Off
// 1: On
var state = false;
var collide = false;
Message.Subscribe("power", function (powered) {
state = powered;
showState(state);
});
Events.OnCollide(function (e) {
if (!e.Settled || !e.Actor.IsMobile()) {
return;
}
if (collide === false) {
Sound.Play("button-down.wav")
state = !state;
var nonce = Math.random() * 2147483647;
Message.Publish("switch:toggle", state);
Message.Publish("power", state);
showState(state);
collide = true;
}
});
Events.OnLeave(function (e) {
collide = false;
});
}
// showState shows the on/off frame based on the boolean powered state.
function showState(state) {
if (state) {
Self.ShowLayer(1);
} else {
Self.ShowLayer(0);
}
}