doodle/dev-assets/doodads/box/box.js
Noah Petherbridge 9b75f1b039 Spit and polish
* New built-in wallpaper: "Dotted paper (dark)" is a dark-themed wallpaper.
* New built-in palette: "Neon Bright" with bright colors for dark levels.
* New cheat: "warp whistle" to automatically win the level.
* In case the user has a VERY LARGE screen resolution bigger than the full
  bounds of a Bounded level, the Play Scene will cap the size and center
  the level canvas onto the window. This is preferable to being able to see
  beyond the level's boundaries and hitting an invisible wall in-game.
* Make the titlescreen Lazy Scroll work on unbounded levels. It can't bounce
  off scroll boundaries but it will reverse course if it reaches the level's
  furthest limits.
* Bugfix: characters' white eyes were transparent in-game. Multiple culprits
  from the `doodad convert` tool defaulting the chroma key to white, to the
  SDL2 textures considering white to be transparent. For the latter, the game
  offsets the color by -1 blue.
2022-05-03 21:15:39 -07:00

74 lines
2.1 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)) {
// Be sure to position them snug on top.
// TODO: this might be a nice general solution in the
// collision detector...
e.Actor.MoveTo(Point(
e.Actor.Position().X,
Self.Position().Y - e.Actor.Hitbox().Y - e.Actor.Hitbox().H - 2,
))
e.Actor.SetGrounded(true);
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);
}