2020-12-30 04:31:35 +00:00
|
|
|
// Warp Doors
|
|
|
|
|
2022-01-17 04:09:27 +00:00
|
|
|
const color = Self.GetTag("color"),
|
|
|
|
isStateDoor = color === 'blue' || color === 'orange';
|
|
|
|
|
|
|
|
// State in case we're a blue warp door.
|
|
|
|
let state = color === 'blue',
|
|
|
|
animating = false,
|
|
|
|
collide = false;
|
2020-12-30 04:31:35 +00:00
|
|
|
|
2022-01-17 04:09:27 +00:00
|
|
|
function main() {
|
2020-12-30 04:31:35 +00:00
|
|
|
// Declare animations and sprite names.
|
2022-01-17 04:09:27 +00:00
|
|
|
let animSpeed = 100;
|
|
|
|
let spriteDefault, spriteDisabled; // the latter for state doors.
|
2020-12-30 04:31:35 +00:00
|
|
|
if (color === 'blue') {
|
|
|
|
Self.AddAnimation("open", animSpeed, ["blue-2", "blue-3", "blue-4"]);
|
|
|
|
Self.AddAnimation("close", animSpeed, ["blue-4", "blue-3", "blue-2", "blue-1"]);
|
|
|
|
spriteDefault = "blue-1";
|
|
|
|
spriteDisabled = "blue-off";
|
|
|
|
} else if (color === 'orange') {
|
|
|
|
Self.AddAnimation("open", animSpeed, ["orange-2", "orange-3", "orange-4"]);
|
|
|
|
Self.AddAnimation("close", animSpeed, ["orange-4", "orange-3", "orange-2", "orange-1"]);
|
|
|
|
spriteDefault = "orange-1";
|
|
|
|
spriteDisabled = "orange-off";
|
2022-01-03 06:36:32 +00:00
|
|
|
} else if (color === 'invisible') {
|
|
|
|
// Invisible Warp Door region.
|
|
|
|
Self.Hide();
|
|
|
|
Self.AddAnimation("open", animSpeed, [0, 0]);
|
|
|
|
Self.AddAnimation("close", animSpeed, [0, 0]);
|
2020-12-30 04:31:35 +00:00
|
|
|
} else {
|
|
|
|
Self.AddAnimation("open", animSpeed, ["door-2", "door-3", "door-4"]);
|
|
|
|
Self.AddAnimation("close", animSpeed, ["door-4", "door-3", "door-2", "door-1"]);
|
|
|
|
spriteDefault = "door-1";
|
|
|
|
}
|
|
|
|
|
2021-01-03 23:19:21 +00:00
|
|
|
// Find our linked Warp Door.
|
2022-01-17 04:09:27 +00:00
|
|
|
let linkedDoor = null;
|
|
|
|
for (let link of Self.GetLinks()) {
|
|
|
|
if (link.Title.indexOf("Warp Door") > -1) {
|
|
|
|
linkedDoor = link;
|
|
|
|
break;
|
2021-01-03 23:19:21 +00:00
|
|
|
}
|
|
|
|
}
|
2020-12-30 04:31:35 +00:00
|
|
|
|
|
|
|
// Subscribe to the global state-change if we are a state door.
|
|
|
|
if (isStateDoor) {
|
2022-01-17 04:09:27 +00:00
|
|
|
Message.Subscribe("broadcast:state-change", (newState) => {
|
2020-12-30 04:31:35 +00:00
|
|
|
state = color === 'blue' ? !newState : newState;
|
|
|
|
|
|
|
|
// Activate or deactivate the door.
|
|
|
|
Self.ShowLayerNamed(state ? spriteDefault : spriteDisabled);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-01-03 06:36:32 +00:00
|
|
|
// For player groundedness work-around
|
2022-01-17 04:09:27 +00:00
|
|
|
let playerLastY = []; // last sampling of Y values
|
2022-01-03 06:36:32 +00:00
|
|
|
|
2021-01-03 23:19:21 +00:00
|
|
|
// The player Uses the door.
|
2022-01-17 04:09:27 +00:00
|
|
|
let flashedCooldown = false; // "Locked Door" flashed message.
|
|
|
|
Events.OnUse((e) => {
|
2021-01-03 23:19:21 +00:00
|
|
|
if (animating) {
|
2020-12-30 04:31:35 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-01-03 23:19:21 +00:00
|
|
|
// Doors without linked exits are not usable.
|
|
|
|
if (linkedDoor === null) {
|
|
|
|
if (!flashedCooldown) {
|
|
|
|
Flash("This door is locked.");
|
|
|
|
flashedCooldown = true;
|
|
|
|
setTimeout(function() {
|
|
|
|
flashedCooldown = false;
|
|
|
|
}, 1000);
|
|
|
|
}
|
2020-12-30 04:31:35 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only players can use doors for now.
|
2021-01-03 23:19:21 +00:00
|
|
|
if (e.Actor.IsPlayer()) {
|
2020-12-30 04:31:35 +00:00
|
|
|
if (isStateDoor && !state) {
|
|
|
|
// The state door is inactive (dotted outline).
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-01-03 06:36:32 +00:00
|
|
|
// The player must be grounded or have no gravity to open the door.
|
|
|
|
if (!e.Actor.Grounded() && e.Actor.HasGravity()) {
|
|
|
|
// Work-around: if two Boxes are stacked atop each other the player can
|
|
|
|
// get stuck if he jumps on top. He may not be Grounded but isn't changing
|
|
|
|
// effective Y position and a warp door may work as a good way out.
|
2022-01-17 04:09:27 +00:00
|
|
|
let yValue = e.Actor.Position().Y;
|
2022-01-03 06:36:32 +00:00
|
|
|
|
|
|
|
// Collect a sampling of last few Y values. If the player Y position
|
|
|
|
// is constant the last handful of frames, treat them as if they're
|
|
|
|
// grounded (or else they can't activate the warp door).
|
|
|
|
playerLastY.unshift(yValue);
|
|
|
|
if (playerLastY.length < 6) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We have enough history.
|
|
|
|
playerLastY.pop();
|
|
|
|
|
|
|
|
// Hasn't moved?
|
2022-01-17 04:09:27 +00:00
|
|
|
let isGrounded = true;
|
|
|
|
for (let i = 0; i < playerLastY.length; i++) {
|
2022-01-03 06:36:32 +00:00
|
|
|
if (yValue !== playerLastY[i]) {
|
|
|
|
isGrounded = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isGrounded) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Player was effectively grounded! No change in Y position.
|
|
|
|
}
|
|
|
|
|
2021-01-03 23:19:21 +00:00
|
|
|
// Freeze the player.
|
|
|
|
e.Actor.Freeze()
|
|
|
|
|
2020-12-30 04:31:35 +00:00
|
|
|
// Play the open and close animation.
|
|
|
|
animating = true;
|
2022-01-17 04:09:27 +00:00
|
|
|
Self.PlayAnimation("open", () => {
|
2020-12-30 04:31:35 +00:00
|
|
|
e.Actor.Hide()
|
2022-01-17 04:09:27 +00:00
|
|
|
Self.PlayAnimation("close", () => {
|
2020-12-30 04:31:35 +00:00
|
|
|
Self.ShowLayerNamed(isStateDoor && !state ? spriteDisabled : spriteDefault);
|
|
|
|
animating = false;
|
2021-01-03 23:19:21 +00:00
|
|
|
|
|
|
|
// Teleport the player to the linked door. Inform the target
|
|
|
|
// door of the arrival of the player so it doesn't trigger
|
|
|
|
// to send the player back here again on a loop.
|
|
|
|
if (linkedDoor !== null) {
|
|
|
|
Message.Publish("warp-door:incoming", e.Actor);
|
|
|
|
e.Actor.MoveTo(linkedDoor.Position());
|
|
|
|
}
|
2020-12-30 04:31:35 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-01-03 23:19:21 +00:00
|
|
|
// Respond to incoming warp events.
|
2022-01-17 04:09:27 +00:00
|
|
|
Message.Subscribe("warp-door:incoming", (player) => {
|
2021-01-03 23:19:21 +00:00
|
|
|
animating = true;
|
|
|
|
player.Unfreeze();
|
2022-01-17 04:09:27 +00:00
|
|
|
Self.PlayAnimation("open", () => {
|
2021-01-03 23:19:21 +00:00
|
|
|
player.Show();
|
2022-01-17 04:09:27 +00:00
|
|
|
Self.PlayAnimation("close", () => {
|
2021-01-03 23:19:21 +00:00
|
|
|
animating = false;
|
|
|
|
|
|
|
|
// If the receiving door was a State Door, fix its state.
|
|
|
|
if (isStateDoor) {
|
|
|
|
Self.ShowLayerNamed(state ? spriteDefault : spriteDisabled);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2020-12-30 04:31:35 +00:00
|
|
|
});
|
|
|
|
}
|