quantum-space-buddies/QSB/ElevatorSync/WorldObjects/QSBElevator.cs

90 lines
2.5 KiB
C#
Raw Normal View History

2022-01-29 04:49:07 +00:00
using Cysharp.Threading.Tasks;
using QSB.ElevatorSync.Messages;
2022-01-23 01:30:08 +00:00
using QSB.Messaging;
2022-03-22 17:56:41 +00:00
using QSB.Patches;
using QSB.Utility;
2022-01-23 01:30:08 +00:00
using QSB.WorldSync;
2022-01-29 04:49:07 +00:00
using System.Threading;
2020-08-12 19:58:29 +00:00
using UnityEngine;
2022-03-03 03:46:33 +00:00
namespace QSB.ElevatorSync.WorldObjects;
public class QSBElevator : WorldObject<Elevator>
2020-08-12 19:58:29 +00:00
{
2022-03-03 03:46:33 +00:00
private OWTriggerVolume _elevatorTrigger;
// Used to reset attach position. This is in local space.
public Vector3 originalAttachPosition;
2022-03-03 03:46:33 +00:00
public override async UniTask Init(CancellationToken ct)
{
var boxShape = AttachedObject.gameObject.GetAddComponent<BoxShape>();
if (Name == "LogLift")
{
boxShape.size = new Vector3(4.6f, 3.5f, 12);
boxShape.center = new Vector3(0.1f, 1.75f, 1.3f);
}
else
{
boxShape.size = new Vector3(3, 3.5f, 3);
boxShape.center = new Vector3(0, 1.75f, 0.25f);
}
2022-01-23 01:30:08 +00:00
2022-03-03 03:46:33 +00:00
_elevatorTrigger = AttachedObject.gameObject.GetAddComponent<OWTriggerVolume>();
originalAttachPosition = AttachedObject._attachPoint.transform.localPosition;
2022-03-03 03:46:33 +00:00
}
2021-12-07 15:56:08 +00:00
2022-03-03 03:46:33 +00:00
public override void SendInitialState(uint to) =>
this.SendMessage(new ElevatorMessage(AttachedObject._goingToTheEnd) { To = to });
2021-11-25 18:55:22 +00:00
2022-03-03 03:46:33 +00:00
public void RemoteCall(bool isGoingUp)
{
if (AttachedObject._goingToTheEnd == isGoingUp)
{
2022-03-03 03:46:33 +00:00
return;
}
2022-03-22 17:56:41 +00:00
SetDirection(isGoingUp);
2022-03-03 03:46:33 +00:00
if (_elevatorTrigger.IsTrackingObject(Locator.GetPlayerDetector()))
{
var attachPoint = AttachedObject._attachPoint;
attachPoint.transform.position = Locator.GetPlayerTransform().position;
attachPoint.AttachPlayer();
2022-03-03 03:46:33 +00:00
if (Locator.GetPlayerSuit().IsWearingSuit() && Locator.GetPlayerSuit().IsTrainingSuit())
{
2022-03-03 03:46:33 +00:00
Locator.GetPlayerSuit().RemoveSuit();
}
}
2022-03-22 17:56:41 +00:00
2022-08-29 05:21:20 +00:00
AttachedObject.StartLift();
// Runs when the lift/elevator is done moving.
// Reset the position of the attach point.
Delay.RunWhen(() => !AttachedObject.enabled, () =>
{
AttachedObject._attachPoint.transform.localPosition = originalAttachPosition;
});
2022-03-03 03:46:33 +00:00
}
2022-03-03 03:46:33 +00:00
private void SetDirection(bool isGoingUp)
{
AttachedObject._interactVolume.transform.Rotate(0f, 180f, 0f);
AttachedObject._goingToTheEnd = isGoingUp;
AttachedObject._targetLocalPos = isGoingUp ? AttachedObject._endLocalPos : AttachedObject._startLocalPos;
}
public override void DisplayLines()
{
var boxShape = (BoxShape)_elevatorTrigger._shape;
Popcron.Gizmos.Cube(
ShapeUtil.Box.CalcWorldSpaceCenter(boxShape),
boxShape.transform.rotation,
ShapeUtil.Box.CalcWorldSpaceSize(boxShape),
_elevatorTrigger.IsTrackingObject(Locator.GetPlayerDetector()) ? Color.green : Color.white
);
2020-12-02 21:23:01 +00:00
}
2022-03-22 17:56:41 +00:00
}