doodle/dev-assets/doodads/on-off/state-button.js
Noah Petherbridge 08e65c32b5 Overhaul the Platformer Physics System
* Player character now experiences acceleration and friction when
  walking around the map!
* Actor position and movement had to be converted from int's
  (render.Point) to float64's to support fine-grained acceleration
  steps.
* Added "physics" package and physics.Vector to be a float64 counterpart
  for render.Point. Vector is used for uix.Actor.Position() for the sake
  of movement math. Vector is flattened back to a render.Point for
  collision purposes, since the levels and hitboxes are pixel-bound.
* Refactor the uix.Actor to no longer extend the doodads.Drawing (so it
  can have a Position that's a Vector instead of a Point). This broke
  some code that expected `.Doodad` to directly reference the
  Drawing.Doodad: now you had to refer to it as `a.Drawing.Doodad` which
  was ugly. Added convenience method .Doodad() for a shortcut.
* Moved functions like GetBoundingRect() from doodads package to
  collision, where it uses its own slimmer Actor interface for just the
  relevant methods it needs.
2020-04-04 21:00:32 -07:00

57 lines
1.2 KiB
JavaScript

// State Block Control Button
// Button is "OFF" by default.
var state = false;
function main() {
console.log("%s ID '%s' initialized!", Self.Doodad().Title, Self.ID());
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;
// 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();
});
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);
showSprite();
}
// Always a solid button.
return false;
});
Events.OnLeave(function(e) {
colliding = false;
})
}
// Update the active layer based on the current button state.
function showSprite() {
if (state) {
Self.ShowLayer(1);
} else {
Self.ShowLayer(0);
}
}