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.
|
|
|
|
var state = false;
|
|
|
|
|
2019-12-31 02:13:28 +00:00
|
|
|
function main() {
|
2020-04-22 06:50:45 +00:00
|
|
|
console.log("%s ID '%s' initialized!", Self.Title, Self.ID());
|
2019-12-31 02:13:28 +00:00
|
|
|
Self.SetHitbox(0, 0, 33, 33);
|
|
|
|
|
|
|
|
// When the button is activated, don't keep toggling state until we're not
|
|
|
|
// being touched again.
|
|
|
|
var colliding = false;
|
|
|
|
|
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.
|
|
|
|
Message.Subscribe("broadcast:state-change", function(value) {
|
|
|
|
state = value;
|
|
|
|
showSprite();
|
|
|
|
});
|
2019-12-31 02:13:28 +00:00
|
|
|
|
|
|
|
Events.OnCollide(function(e) {
|
|
|
|
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;
|
|
|
|
});
|
|
|
|
|
|
|
|
Events.OnLeave(function(e) {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|