2021-07-19 04:22:53 +00:00
|
|
|
// Pushable Box.
|
2022-01-17 04:09:27 +00:00
|
|
|
|
|
|
|
const speed = 4,
|
|
|
|
size = 75;
|
2021-07-19 04:22:53 +00:00
|
|
|
|
|
|
|
function main() {
|
|
|
|
Self.SetMobile(true);
|
2022-02-21 21:09:51 +00:00
|
|
|
Self.SetInvulnerable(true);
|
2021-07-19 04:22:53 +00:00
|
|
|
Self.SetGravity(true);
|
|
|
|
Self.SetHitbox(0, 0, size, size);
|
|
|
|
|
2022-01-17 04:09:27 +00:00
|
|
|
Events.OnCollide((e) => {
|
2021-07-20 04:20:22 +00:00
|
|
|
// Ignore events from neighboring Boxes.
|
|
|
|
if (e.Actor.Actor.Filename === "box.doodad") {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-07-19 04:22:53 +00:00
|
|
|
if (e.Actor.IsMobile() && e.InHitbox) {
|
2022-01-17 04:09:27 +00:00
|
|
|
let overlap = e.Overlap;
|
2021-07-20 04:20:22 +00:00
|
|
|
|
|
|
|
if (overlap.Y === 0 && !(overlap.X === 0 && overlap.W < 5) && !(overlap.X === size)) {
|
2021-07-19 04:22:53 +00:00
|
|
|
// 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));
|
|
|
|
});
|
|
|
|
|
2021-08-09 03:10:42 +00:00
|
|
|
// When we receive power, we reset to our original position.
|
2022-01-17 04:09:27 +00:00
|
|
|
let origPoint = Self.Position();
|
|
|
|
Message.Subscribe("power", (powered) => {
|
2021-08-09 03:10:42 +00:00
|
|
|
Self.MoveTo(origPoint);
|
|
|
|
Self.SetVelocity(Vector(0, 0));
|
|
|
|
});
|
|
|
|
|
2021-07-19 04:22:53 +00:00
|
|
|
// Start animation on a loop.
|
|
|
|
animate();
|
|
|
|
}
|
|
|
|
|
|
|
|
function animate() {
|
|
|
|
Self.AddAnimation("animate", 100, [0, 1, 2, 3, 2, 1]);
|
|
|
|
|
2022-01-17 04:09:27 +00:00
|
|
|
let running = false;
|
|
|
|
setInterval(() => {
|
2021-07-19 04:22:53 +00:00
|
|
|
if (!running) {
|
|
|
|
running = true;
|
|
|
|
Self.PlayAnimation("animate", function () {
|
|
|
|
running = false;
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}, 100);
|
2022-01-17 04:09:27 +00:00
|
|
|
}
|