New Doodad: Anvil

* The Anvil doodad is affected by gravity and becomes dangerous when
  falling. If it lands on the player character, you die! If it lands on
  any other mobile doodad, it destroys it! It can land on solid doodads
  such as the Electric Trapdoor and the Crumbly Floor. It will activate
  a Crumbly Floor if it lands on one, and can activate buttons and
  switches that it passes.
* JavaScript API: FailLevel(message) can be called from a doodad to kill
  the player character. The Anvil does this if it collides with the
  player while it's been falling.
This commit is contained in:
Noah 2021-08-08 21:54:37 -07:00
parent 4cb441dc46
commit f8ddf512a9
7 changed files with 71 additions and 8 deletions

View File

@ -41,9 +41,6 @@ function main() {
// When we receive power, we reset to our original position.
var origPoint = Self.Position();
Message.Subscribe("power", function (powered) {
console.error("Box received power! %+v", powered);
console.error("MoveTo: %+v", origPoint);
console.error("Keys: %+v", Object.keys(Self));
Self.MoveTo(origPoint);
Self.SetVelocity(Vector(0, 0));
});

View File

@ -58,6 +58,10 @@ objects() {
cd box/
make
cd ..
cd crumbly-floor/
make
cd ..
}
onoff() {

View File

@ -2,10 +2,16 @@ ALL: build
.PHONY: build
build:
# Start Flag
doodad convert -t "Start Flag" start-flag.png start-flag.doodad
# Exit Flag
doodad convert -t "Exit Flag" exit-flag.png exit-flag.doodad
doodad install-script exit-flag.js exit-flag.doodad
doodad convert -t "Start Flag" start-flag.png start-flag.doodad
# Anvil
doodad convert -t "Anvil" anvil.png anvil.doodad
doodad install-script anvil.js anvil.doodad
for i in *.doodad; do\
doodad edit-doodad --tag "category=objects" $${i};\

View File

@ -0,0 +1,51 @@
// 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.
var lastPoint = Self.Position();
setInterval(function () {
var nowAt = Self.Position();
if (nowAt.Y > lastPoint.Y) {
falling = true;
} else {
falling = false;
}
lastPoint = nowAt;
}, 100);
Events.OnCollide(function (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.
var origPoint = Self.Position();
Message.Subscribe("power", function (powered) {
Self.MoveTo(origPoint);
Self.SetVelocity(Vector(0, 0));
});
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 796 B

View File

@ -7,13 +7,13 @@ function main() {
var state = false;
var collide = false;
Message.Subscribe("power", function(powered) {
Message.Subscribe("power", function (powered) {
state = powered;
showState(state);
});
Events.OnCollide(function(e) {
if (!e.Settled) {
Events.OnCollide(function (e) {
if (!e.Settled || !e.Actor.IsMobile()) {
return;
}
@ -30,7 +30,7 @@ function main() {
}
});
Events.OnLeave(function(e) {
Events.OnLeave(function (e) {
collide = false;
});
}

View File

@ -49,6 +49,11 @@ function setPoweredState(powered) {
Self.PlayAnimation("open", function () {
isOpen = true;
animating = false;
// Had we lost power quickly?
if (!powerState) {
setPoweredState(false);
}
});
} else {
animating = true;