doodle/dev-assets/doodads/box/box.js
Noah Petherbridge 962098d4e7 v0.11.0 last minute tweaks
* When playing as the Bird, the dive attack is able to destroy other
  mobile doodads such as Azulians and Thieves.
* The Box has been made invulnerable so it can't be destroyed by Anvils
  or player-controlled Birds.
* Bugfixes with pop-up modals:
  * The quit game confirm modal doesn't appear if another modal is
    already active on screen.
  * The Escape key can dismiss Alert and Confirm modals.
* Add "Level" menu items to Play Mode to restart the level or retry from
  the last checkpoint (in case of softlocks, etc.)
2022-02-21 13:09:51 -08:00

67 lines
1.7 KiB
JavaScript

// Pushable Box.
const speed = 4,
size = 75;
function main() {
Self.SetMobile(true);
Self.SetInvulnerable(true);
Self.SetGravity(true);
Self.SetHitbox(0, 0, size, size);
Events.OnCollide((e) => {
// Ignore events from neighboring Boxes.
if (e.Actor.Actor.Filename === "box.doodad") {
return false;
}
if (e.Actor.IsMobile() && e.InHitbox) {
let overlap = e.Overlap;
if (overlap.Y === 0 && !(overlap.X === 0 && overlap.W < 5) && !(overlap.X === size)) {
// Standing on top, ignore.
return false;
} else if (overlap.Y === size) {
// From the bottom, boop it up.
Self.SetVelocity(Vector(0, -speed * 2))
}
// If touching from the sides, slide away.
if (overlap.X === 0) {
Self.SetVelocity(Vector(speed, 0))
} else if (overlap.X === size) {
Self.SetVelocity(Vector(-speed, 0))
}
return false;
}
});
Events.OnLeave(function (e) {
Self.SetVelocity(Vector(0, 0));
});
// When we receive power, we reset to our original position.
let origPoint = Self.Position();
Message.Subscribe("power", (powered) => {
Self.MoveTo(origPoint);
Self.SetVelocity(Vector(0, 0));
});
// Start animation on a loop.
animate();
}
function animate() {
Self.AddAnimation("animate", 100, [0, 1, 2, 3, 2, 1]);
let running = false;
setInterval(() => {
if (!running) {
running = true;
Self.PlayAnimation("animate", function () {
running = false;
})
}
}, 100);
}