doodle/dev-assets/doodads/objects/anvil.js
Noah Petherbridge 4d08bf1d85 Switch JavaScript engine to goja
* Switch from otto to goja for JavaScript engine.
* goja supports many ES6 syntax features like arrow functions,
  const/let, for-of with more coming soon.
* Same great features as otto, more modern environment for doodads!
2022-01-16 20:09:27 -08:00

52 lines
1.4 KiB
JavaScript

// Anvil
var falling = false;
function main() {
// Note: doodad is not "solid" but hurts if it falls on you.
Self.SetHitbox(0, 0, 48, 25);
Self.SetMobile(true);
Self.SetGravity(true);
// Monitor our Y position to tell if we've been falling.
let lastPoint = Self.Position();
setInterval(() => {
let nowAt = Self.Position();
if (nowAt.Y > lastPoint.Y) {
falling = true;
} else {
falling = false;
}
lastPoint = nowAt;
}, 100);
Events.OnCollide((e) => {
if (!e.Settled) {
return;
}
// Were we falling?
if (falling) {
if (e.InHitbox) {
if (e.Actor.IsPlayer()) {
// Fatal to the player.
Sound.Play("crumbly-break.wav");
FailLevel("Watch out for anvils!");
return;
}
else if (e.Actor.IsMobile()) {
// Destroy mobile doodads.
Sound.Play("crumbly-break.wav");
e.Actor.Destroy();
}
}
}
});
// 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));
});
}