doodle/dev-assets/doodads/trapdoors/electric-trapdoor.js
Noah Petherbridge 0518df226c 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.
2021-08-08 21:57:41 -07:00

66 lines
1.6 KiB
JavaScript

// Electric Trapdoor
var animationSpeed = 100,
spriteWidth = 114,
thickness = 7,
isOpen = false,
animating = false,
powerState = false;
function main() {
Self.SetHitbox(0, 2, spriteWidth, thickness);
Self.AddAnimation("open", animationSpeed, [0, 1, 2, 3]);
Self.AddAnimation("close", animationSpeed, [3, 2, 1, 0]);
// Subscribe to Switches and other power sources. Note: if a
// switch toggles us, we ignore the immediately following
// power signal which will be coming from the same switch.
// The electric trapdoor always toggles when hit by a switch.
var ignoreNextPower = false;
Message.Subscribe("switch:toggle", function (powered) {
ignoreNextPower = true
setPoweredState(!powerState);
});
Message.Subscribe("power", function (powered) {
if (ignoreNextPower) {
ignoreNextPower = false;
return;
}
setPoweredState(powered);
});
Events.OnCollide(function (e) {
if (e.InHitbox && !isOpen) {
return false;
}
})
}
function setPoweredState(powered) {
powerState = powered;
if (powered) {
if (animating || isOpen) {
return;
}
animating = true;
Self.PlayAnimation("open", function () {
isOpen = true;
animating = false;
// Had we lost power quickly?
if (!powerState) {
setPoweredState(false);
}
});
} else {
animating = true;
Self.PlayAnimation("close", function () {
isOpen = false;
animating = false;
});
}
}