2022-01-17 04:09:27 +00:00
|
|
|
// Colored Locked Doors.
|
|
|
|
|
|
|
|
const color = Self.GetTag("color"),
|
|
|
|
keyname = color === "small" ? "small-key.doodad" : "key-" + color + ".doodad";
|
2020-04-03 04:43:41 +00:00
|
|
|
|
2022-01-17 04:09:27 +00:00
|
|
|
function main() {
|
2020-04-03 04:43:41 +00:00
|
|
|
// Layers in the doodad image.
|
2022-01-17 04:09:27 +00:00
|
|
|
let layer = {
|
2020-04-03 04:43:41 +00:00
|
|
|
closed: 0,
|
2020-12-30 01:24:42 +00:00
|
|
|
unlocked: 1,
|
|
|
|
right: 2,
|
|
|
|
left: 3,
|
2020-04-03 04:43:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Variables that change in event handler.
|
2022-01-17 04:09:27 +00:00
|
|
|
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.
|
2020-04-03 04:43:41 +00:00
|
|
|
|
2020-12-30 01:24:42 +00:00
|
|
|
Self.SetHitbox(34, 0, 13, 76);
|
2020-04-03 04:43:41 +00:00
|
|
|
|
2022-01-17 04:09:27 +00:00
|
|
|
Events.OnCollide((e) => {
|
2020-04-03 04:43:41 +00:00
|
|
|
// 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;
|
2020-05-23 03:07:48 +00:00
|
|
|
Sound.Play("door-open.wav")
|
2020-04-03 04:43:41 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-03 06:09:46 +00:00
|
|
|
// Do they have our key?
|
2022-01-17 04:09:27 +00:00
|
|
|
let hasKey = e.Actor.HasItem(keyname) >= 0;
|
2020-04-03 06:09:46 +00:00
|
|
|
if (!hasKey) {
|
2020-04-03 04:43:41 +00:00
|
|
|
// Door is locked.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (e.Settled) {
|
|
|
|
unlocked = true;
|
|
|
|
Self.ShowLayer(enterSide < 0 ? layer.right : layer.left);
|
2021-01-04 01:06:33 +00:00
|
|
|
Sound.Play("unlock.wav");
|
|
|
|
|
|
|
|
// If a Small Key door, consume a small key.
|
|
|
|
if (color === "small") {
|
|
|
|
e.Actor.RemoveItem(keyname, 1)
|
|
|
|
}
|
2020-04-03 04:43:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2022-01-17 04:09:27 +00:00
|
|
|
Events.OnLeave((e) => {
|
2020-12-30 01:24:42 +00:00
|
|
|
Self.ShowLayer(unlocked ? layer.unlocked : layer.closed);
|
2020-05-23 03:07:48 +00:00
|
|
|
// Sound.Play("door-close.wav")
|
2020-04-03 04:43:41 +00:00
|
|
|
|
|
|
|
// Reset collision state.
|
|
|
|
opened = false;
|
|
|
|
enterSide = 0;
|
|
|
|
});
|
|
|
|
}
|