doodle/dev-assets/doodads/switches/switch.js
Noah Petherbridge 4d08bf1d85 Switch JavaScript engine to goja
* Switch from otto to goja for JavaScript engine.
* goja supports many ES6 syntax features like arrow functions,
  const/let, for-of with more coming soon.
* Same great features as otto, more modern environment for doodads!
2022-01-16 20:09:27 -08:00

45 lines
719 B
JavaScript

function main() {
// Switch has two frames:
// 0: Off
// 1: On
let state = false;
let collide = false;
Message.Subscribe("power", (powered) => {
state = powered;
showState(state);
});
Events.OnCollide((e) => {
if (!e.Settled || !e.Actor.IsMobile()) {
return;
}
if (collide === false) {
Sound.Play("button-down.wav")
state = !state;
Message.Publish("switch:toggle", state);
Message.Publish("power", state);
showState(state);
collide = true;
}
});
Events.OnLeave((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);
}
}