doodle/dev-assets/doodads/switches/switch.js
Noah Petherbridge 27896a9253 Add Initial Sound Effects
Adds support for sound effects in Doodle and configures some for various
doodads to start out with:

* Buttons and Switches: "Clicked down" and "clicked up" sounds.
* Colored Doors: an "unlocked" sound and a "door opened" sound.
* Electric Door: sci-fi sounds when opening and closing.
* Keys: sound effect for collecting keys.

JavaScript API for Doodads adds a global function `Sound.Play(filename)`
to play sounds. All sounds in the `rtp/sfx/` folder are pre-loaded on
startup for efficient use in the app. Otherwise sounds are lazy-loaded
on first playback.
2020-05-22 20:07:48 -07:00

44 lines
711 B
JavaScript

function main() {
console.log("%s initialized!", Self.Title);
// 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) {
return;
}
if (collide === false) {
Sound.Play("button-down.wav")
state = !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);
}
}