doodads/doors/colored-door.js

89 lines
2.0 KiB
JavaScript
Raw Normal View History

Doodad/Actor Runtime Options * Add "Options" support for Doodads: these allow for individual Actor instances on your level to customize properties about the doodad. They're like "Tags" except the player can customize them on a per-actor basis. * Doodad Editor: you can specify the Options in the Doodad Properties window. * Level Editor: when the Actor Tool is selected, on mouse-over of an actor, clicking on the gear icon will open a new "Actor Properties" window which shows metadata (title, author, ID, position) and an Options tab to configure the actor's options. Updates to the scripting API: * Self.Options() returns a list of option names defined on the Doodad. * Self.GetOption(name) returns the value for the named option, or nil if neither the actor nor its doodad have the option defined. The return type will be correctly a string, boolean or integer type. Updates to the doodad command-line tool: * `doodad show` will print the Options on a .doodad file and, when showing a .level file with --actors, prints any customized Options with the actors. * `doodad edit-doodad` adds a --option parameter to define options. Options added to the game's built-in doodads: * Warp Doors: "locked (exit only)" will make it so the door can not be opened by the player, giving the "locked" message (as if it had no linked door), but the player may still exit from the door if sent by another warp door. * Electric Door & Electric Trapdoor: "opened" can make the door be opened by default when the level begins instead of closed. A switch or a button that removes power will close the door as normal. * Colored Doors & Small Key Door: "unlocked" will make the door unlocked at level start, not requiring a key to open it. * Colored Keys & Small Key: "has gravity" will make the key subject to gravity and set its Mobile flag so that if it falls onto a button, it will activate. * Gemstones: they had gravity by default; you can now uncheck "has gravity" to remove their Gravity and IsMobile status. * Gemstone Totems: "has gemstone" will set the totem to its unlocked status by default with the gemstone inserted. No power signal will be emitted; it is cosmetic only. * Fire Region: "name" can let you set a name for the fire region similarly to names for fire pixels: "Watch out for ${name}!" * Invisible Warp Door: "locked (exit only)" added as well.
2022-10-10 00:44:27 +00:00
/*
Colored Locked Doors
This script handles the blue, green, red, yellow and small-key doors.
Each door has a corresponding key that will unlock it. Small Key doors consume a
small key when unlocked for the first time.
Options: "unlocked" can make the door unlocked by default when the level begins.
*/
const color = Self.GetTag("color"),
Doodad/Actor Runtime Options * Add "Options" support for Doodads: these allow for individual Actor instances on your level to customize properties about the doodad. They're like "Tags" except the player can customize them on a per-actor basis. * Doodad Editor: you can specify the Options in the Doodad Properties window. * Level Editor: when the Actor Tool is selected, on mouse-over of an actor, clicking on the gear icon will open a new "Actor Properties" window which shows metadata (title, author, ID, position) and an Options tab to configure the actor's options. Updates to the scripting API: * Self.Options() returns a list of option names defined on the Doodad. * Self.GetOption(name) returns the value for the named option, or nil if neither the actor nor its doodad have the option defined. The return type will be correctly a string, boolean or integer type. Updates to the doodad command-line tool: * `doodad show` will print the Options on a .doodad file and, when showing a .level file with --actors, prints any customized Options with the actors. * `doodad edit-doodad` adds a --option parameter to define options. Options added to the game's built-in doodads: * Warp Doors: "locked (exit only)" will make it so the door can not be opened by the player, giving the "locked" message (as if it had no linked door), but the player may still exit from the door if sent by another warp door. * Electric Door & Electric Trapdoor: "opened" can make the door be opened by default when the level begins instead of closed. A switch or a button that removes power will close the door as normal. * Colored Doors & Small Key Door: "unlocked" will make the door unlocked at level start, not requiring a key to open it. * Colored Keys & Small Key: "has gravity" will make the key subject to gravity and set its Mobile flag so that if it falls onto a button, it will activate. * Gemstones: they had gravity by default; you can now uncheck "has gravity" to remove their Gravity and IsMobile status. * Gemstone Totems: "has gemstone" will set the totem to its unlocked status by default with the gemstone inserted. No power signal will be emitted; it is cosmetic only. * Fire Region: "name" can let you set a name for the fire region similarly to names for fire pixels: "Watch out for ${name}!" * Invisible Warp Door: "locked (exit only)" added as well.
2022-10-10 00:44:27 +00:00
keyname = color === "small" ? "small-key.doodad" : "key-" + color + ".doodad",
isUnlocked = Self.GetOption("unlocked");
function main() {
// Layers in the doodad image.
let layer = {
closed: 0,
unlocked: 1,
right: 2,
left: 3,
};
// Variables that change in event handler.
let unlocked = false; // Key has been used to unlock the door (one time).
let opened = false; // If door is currently showing its opened state.
let enterSide = 0; // Side of player entering the door, -1 or 1, left or right.
Self.SetHitbox(34, 0, 13, 76);
Doodad/Actor Runtime Options * Add "Options" support for Doodads: these allow for individual Actor instances on your level to customize properties about the doodad. They're like "Tags" except the player can customize them on a per-actor basis. * Doodad Editor: you can specify the Options in the Doodad Properties window. * Level Editor: when the Actor Tool is selected, on mouse-over of an actor, clicking on the gear icon will open a new "Actor Properties" window which shows metadata (title, author, ID, position) and an Options tab to configure the actor's options. Updates to the scripting API: * Self.Options() returns a list of option names defined on the Doodad. * Self.GetOption(name) returns the value for the named option, or nil if neither the actor nor its doodad have the option defined. The return type will be correctly a string, boolean or integer type. Updates to the doodad command-line tool: * `doodad show` will print the Options on a .doodad file and, when showing a .level file with --actors, prints any customized Options with the actors. * `doodad edit-doodad` adds a --option parameter to define options. Options added to the game's built-in doodads: * Warp Doors: "locked (exit only)" will make it so the door can not be opened by the player, giving the "locked" message (as if it had no linked door), but the player may still exit from the door if sent by another warp door. * Electric Door & Electric Trapdoor: "opened" can make the door be opened by default when the level begins instead of closed. A switch or a button that removes power will close the door as normal. * Colored Doors & Small Key Door: "unlocked" will make the door unlocked at level start, not requiring a key to open it. * Colored Keys & Small Key: "has gravity" will make the key subject to gravity and set its Mobile flag so that if it falls onto a button, it will activate. * Gemstones: they had gravity by default; you can now uncheck "has gravity" to remove their Gravity and IsMobile status. * Gemstone Totems: "has gemstone" will set the totem to its unlocked status by default with the gemstone inserted. No power signal will be emitted; it is cosmetic only. * Fire Region: "name" can let you set a name for the fire region similarly to names for fire pixels: "Watch out for ${name}!" * Invisible Warp Door: "locked (exit only)" added as well.
2022-10-10 00:44:27 +00:00
// Options: door is unlocked at level start?
if (isUnlocked) {
unlocked = true;
Self.ShowLayer(layer.unlocked);
}
Events.OnCollide((e) => {
// Record the side that this actor has touched us, in case the door
// needs to open.
if (enterSide === 0) {
enterSide = e.Overlap.X > 0 ? 1 : -1;
}
if (opened) {
return;
}
if (e.InHitbox) {
if (unlocked) {
Self.ShowLayer(enterSide < 0 ? layer.right : layer.left);
opened = true;
2023-03-10 06:57:30 +00:00
if (Self.IsOnScreen()) {
Sound.Play("door-open.mp3");
}
return;
}
// Do they have our key?
let hasKey = e.Actor.HasItem(keyname) >= 0;
if (!hasKey) {
// Door is locked.
return false;
}
if (e.Settled) {
unlocked = true;
Self.ShowLayer(enterSide < 0 ? layer.right : layer.left);
2023-03-10 06:57:30 +00:00
if (Self.IsOnScreen()) {
Sound.Play("unlock.mp3");
}
// If a Small Key door, consume a small key.
if (color === "small") {
e.Actor.RemoveItem(keyname, 1)
}
}
}
});
Events.OnLeave((e) => {
Self.ShowLayer(unlocked ? layer.unlocked : layer.closed);
2023-03-10 06:57:30 +00:00
// Sound.Play("door-close.mp3")
// Reset collision state.
opened = false;
enterSide = 0;
});
}