2019-12-31 02:13:28 +00:00
|
|
|
// State Block Control Button
|
2020-01-03 01:58:22 +00:00
|
|
|
|
|
|
|
// Button is "OFF" by default.
|
2022-01-17 04:09:27 +00:00
|
|
|
let state = false;
|
2020-01-03 01:58:22 +00:00
|
|
|
|
2019-12-31 02:13:28 +00:00
|
|
|
function main() {
|
2020-12-30 04:31:35 +00:00
|
|
|
Self.SetHitbox(0, 0, 42, 42);
|
2019-12-31 02:13:28 +00:00
|
|
|
|
|
|
|
// When the button is activated, don't keep toggling state until we're not
|
|
|
|
// being touched again.
|
2022-01-17 04:09:27 +00:00
|
|
|
let colliding = false;
|
2019-12-31 02:13:28 +00:00
|
|
|
|
2020-01-03 01:58:22 +00:00
|
|
|
// If we receive a state change event from a DIFFERENT on/off button, update
|
|
|
|
// ourself to match the state received.
|
2022-01-17 04:09:27 +00:00
|
|
|
Message.Subscribe("broadcast:state-change", (value) => {
|
2020-01-03 01:58:22 +00:00
|
|
|
state = value;
|
|
|
|
showSprite();
|
|
|
|
});
|
2019-12-31 02:13:28 +00:00
|
|
|
|
2022-01-17 04:09:27 +00:00
|
|
|
Events.OnCollide((e) => {
|
2019-12-31 02:13:28 +00:00
|
|
|
if (colliding) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only trigger for mobile characters.
|
|
|
|
if (e.Actor.IsMobile()) {
|
|
|
|
// Only activate if touched from the bottom or sides.
|
|
|
|
if (e.Overlap.Y === 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
colliding = true;
|
|
|
|
state = !state;
|
|
|
|
Message.Broadcast("broadcast:state-change", state);
|
|
|
|
|
2020-01-03 01:58:22 +00:00
|
|
|
showSprite();
|
2019-12-31 02:13:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Always a solid button.
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
2022-01-17 04:09:27 +00:00
|
|
|
Events.OnLeave((e) => {
|
2019-12-31 02:13:28 +00:00
|
|
|
colliding = false;
|
|
|
|
})
|
|
|
|
}
|
2020-01-03 01:58:22 +00:00
|
|
|
|
|
|
|
// Update the active layer based on the current button state.
|
|
|
|
function showSprite() {
|
|
|
|
if (state) {
|
|
|
|
Self.ShowLayer(1);
|
|
|
|
} else {
|
|
|
|
Self.ShowLayer(0);
|
|
|
|
}
|
|
|
|
}
|