Made the elevator button-functional. Needs some fine-tuning and audio work.

This commit is contained in:
2025-11-30 22:05:25 +02:00
parent bcf7e3121d
commit dab5bfc677
12 changed files with 483 additions and 74 deletions

View File

@@ -7,9 +7,9 @@ using UnityEditor;
using UnityEngine;
public enum ElevatorState
{
Stationary,
OpenDoorsStationary,
CloseDoorsStationary,
OpeningDoors,
AwaitingPassengers,
ClosingDoors,
Moving
}
@@ -59,10 +59,10 @@ public class ElevatorBox : NetworkBehaviour
public AudioClip[] closeDoorsClips;
public AudioClip[] moveElevatorClips;
private int currentFloor = 2;
private int targetFloor = 2;
public int currentFloor = 2;
public int targetFloor = 2;
private ElevatorState state = ElevatorState.Stationary;
public ElevatorState state = ElevatorState.CloseDoorsStationary;
void Awake()
{
@@ -88,7 +88,7 @@ public class ElevatorBox : NetworkBehaviour
{
//Debug.Log("Calling Elevator");
Debug.Log(state);
if (state == ElevatorState.Stationary || state == ElevatorState.ClosingDoors)
if (state == ElevatorState.OpenDoorsStationary || state == ElevatorState.CloseDoorsStationary || state == ElevatorState.ClosingDoors)
{
Debug.Log(floor);
@@ -103,13 +103,13 @@ public class ElevatorBox : NetworkBehaviour
StartCoroutine(OpenDoors());
SetState(ElevatorState.AwaitingPassengers);
SetState(ElevatorState.OpenDoorsStationary);
}
}
public void interestExpired()
{
if (state == ElevatorState.AwaitingPassengers)
if (state == ElevatorState.OpenDoorsStationary)
{
if (IsElevatorEmpty())
{
@@ -130,12 +130,13 @@ public class ElevatorBox : NetworkBehaviour
}
private void OnTriggerEnter(Collider other)
{
if (other.GetComponentInParent<SmartHandPresence>() != null) return; // So it ignores player hands.
XROrigin enteredPlayerVR = other.GetComponent<XROrigin>();
KbmController enteredPlayerKbm = other.GetComponent<KbmController>();
XRPlayerMirror networkPlayerDisplay = other.GetComponentInParent<XRPlayerMirror>();
Debug.Log("Player Entered box");
//Debug.Log("Player Entered box");
if (enteredPlayerVR != null)
{
addChild(enteredPlayerVR);
@@ -150,11 +151,11 @@ public class ElevatorBox : NetworkBehaviour
}
else {
Debug.Log(other + " Cannot ride in lifts.");
//Debug.Log(other + " Cannot ride in lifts.");
return;
}
Debug.Log("Calling Lift transfer sequence");
/*Debug.Log("Calling Lift transfer sequence");
switch (currentFloor)
{
@@ -170,7 +171,7 @@ public class ElevatorBox : NetworkBehaviour
break;
}
StartCoroutine(LiftTransferSequence());
StartCoroutine(LiftTransferSequence()); */
}
@@ -199,7 +200,7 @@ public class ElevatorBox : NetworkBehaviour
Debug.Log("Player exited box, state is: " + this.state);
if (this.state == ElevatorState.AwaitingPassengers)
if (this.state == ElevatorState.OpenDoorsStationary)
{
if (IsElevatorEmpty())
{
@@ -278,27 +279,30 @@ public class ElevatorBox : NetworkBehaviour
audioSource.Play();
}
private IEnumerator LiftTransferSequence()
public IEnumerator LiftTransferSequence()
{
Debug.Log("Started Lidt transfer sequence");
if (state != ElevatorState.Moving);
Debug.Log("Started Lift transfer sequence");
yield return StartCoroutine(CloseDoors());
yield return StartCoroutine(MoveToFloor(targetFloor));
}
public IEnumerator MoveToFloor(int floorNumber)
{
Debug.Log("Moving to floor " + floorNumber);
if (floorDict.TryGetValue(floorNumber, out Transform target))
{
yield return StartCoroutine(MoveElevator(target.position));
SetCurrentFloor(targetFloor);
}
else
{
Debug.LogWarning($"No floor defined for number {floorNumber}");
}
if (state == ElevatorState.CloseDoorsStationary) {
//Debug.Log("Moving to floor " + floorNumber);
if (floorDict.TryGetValue(floorNumber, out Transform target))
{
yield return StartCoroutine(MoveElevator(target.position));
SetCurrentFloor(targetFloor);
}
else
{
Debug.LogWarning($"No floor defined for number {floorNumber}");
}
state = ElevatorState.CloseDoorsStationary;
yield return StartCoroutine(OpenDoors());
yield return StartCoroutine(OpenDoors());
}
}
private IEnumerator CloseAndStationary()
{
@@ -307,12 +311,14 @@ public class ElevatorBox : NetworkBehaviour
yield return StartCoroutine(CloseDoors());
// After doors have closed, set state
SetState(ElevatorState.Stationary);
SetState(ElevatorState.CloseDoorsStationary);
}
private IEnumerator CloseDoors()
public IEnumerator CloseDoors()
{
//if (state == ElevatorState.AwaitingPassengers) {
Debug.Log(state);
if (state != ElevatorState.OpeningDoors && state != ElevatorState.CloseDoorsStationary && state != ElevatorState.ClosingDoors)
{
Debug.Log("Closing doors");
if (Vector3.Distance(leftDoor.transform.position, leftDoorClosedPos.position) < 0.01f)
@@ -328,14 +334,15 @@ public class ElevatorBox : NetworkBehaviour
if (currentCaller != null) currentCaller.CloseDoors();
yield return StartCoroutine(MoveDoors(leftDoor, leftDoorClosedPos, rightDoor, rightDoorClosedPos, doorCloseTime));
//}
SetState(ElevatorState.CloseDoorsStationary);
}
}
private IEnumerator OpenDoors()
public IEnumerator OpenDoors()
{
if (state != ElevatorState.AwaitingPassengers) {
//Debug.Log("Opening doors");
if (state != ElevatorState.OpeningDoors && state != ElevatorState.OpenDoorsStationary && state != ElevatorState.Moving) {
Debug.Log("Opening doors");
SetState(ElevatorState.OpeningDoors);
playRandomAudioClipFrom(openDoorsClips);
@@ -349,7 +356,7 @@ public class ElevatorBox : NetworkBehaviour
yield return StartCoroutine(MoveDoors(leftDoor, leftDoorOpenPos, rightDoor, rightDoorOpenPos, doorOpenTime));
SetState(ElevatorState.AwaitingPassengers);
SetState(ElevatorState.OpenDoorsStationary);
yield return new WaitForSeconds(doorStayOpenTime); // wait for passengers
}
}
@@ -376,30 +383,31 @@ public class ElevatorBox : NetworkBehaviour
private IEnumerator MoveElevator(Vector3 targetPos)
{
if (targetFloor > currentFloor)
foreach (ElevatorOuter caller in callers)
caller.statusPlate.SetMoveState(ElevatorMoveState.Ascending);
if (targetFloor != currentFloor) {
if (targetFloor > currentFloor)
foreach (ElevatorOuter caller in callers)
caller.statusPlate.SetMoveState(ElevatorMoveState.Ascending);
else
foreach (ElevatorOuter caller in callers)
caller.statusPlate.SetMoveState(ElevatorMoveState.Decending);
else
foreach (ElevatorOuter caller in callers)
caller.statusPlate.SetMoveState(ElevatorMoveState.Decending);
Vector3 startPos = transform.position;
float t = 0;
Vector3 startPos = transform.position;
float t = 0;
SetState(ElevatorState.Moving);
playRandomAudioClipFrom(moveElevatorClips);
SetState(ElevatorState.Moving);
playRandomAudioClipFrom(moveElevatorClips);
AudioManager.Instance.PlayAttachedInstance(FMODEvents.Instance.ElevatorMovement, gameObject); //fmod attached 3d oneshot event
AudioManager.Instance.PlayAttachedInstance(FMODEvents.Instance.ElevatorMovement, gameObject); //fmod attached 3d oneshot event
while (t < 1f)
{
t += Time.deltaTime / floorMoveTime;
float easedT = Mathf.SmoothStep(0f, 1f, t); // ease in/out
transform.position = Vector3.Lerp(startPos, targetPos, easedT);
yield return null;
while (t < 1f)
{
t += Time.deltaTime / floorMoveTime;
float easedT = Mathf.SmoothStep(0f, 1f, t); // ease in/out
transform.position = Vector3.Lerp(startPos, targetPos, easedT);
yield return null;
}
}
foreach (ElevatorOuter caller in callers)
caller.statusPlate.SetMoveState(ElevatorMoveState.NotMoving);
}