Made the elevator button-functional. Needs some fine-tuning and audio work.
This commit is contained in:
@@ -1,9 +1,13 @@
|
||||
using UnityEngine;
|
||||
|
||||
[RequireComponent(typeof(Rigidbody))]
|
||||
public class SpringyButtonPhysics : MonoBehaviour {
|
||||
public class ElevatorSpringyButton : MonoBehaviour {
|
||||
public Transform buttonDirectionRoot;
|
||||
|
||||
public ElevatorOuter ElevatorOuter;
|
||||
public ElevatorBox box;
|
||||
|
||||
[Header("Movement limits")]
|
||||
public float minX = 0f;
|
||||
public float maxX = 0f;
|
||||
public float minY = 0f;
|
||||
@@ -11,14 +15,31 @@ public class SpringyButtonPhysics : MonoBehaviour {
|
||||
public float minZ = 0.005f;
|
||||
public float maxZ = 0f;
|
||||
|
||||
[Header("Visuals")]
|
||||
public Renderer[] targetRenderers;
|
||||
public Material defaultMaterial;
|
||||
public Material pressedMaterial;
|
||||
|
||||
[Header("Spring Settings")]
|
||||
public float springForce = 300f; // how strong it returns
|
||||
public float damping = 8f; // prevents vibration
|
||||
|
||||
[Header("Button Type Settings")]
|
||||
public bool isFloorButton;
|
||||
public bool isInsideButton;
|
||||
public bool isCloseDoorsButton;
|
||||
public int designatedFloor;
|
||||
|
||||
|
||||
private Rigidbody rb;
|
||||
private Vector3 restLocalPos;
|
||||
|
||||
public bool disabled;
|
||||
|
||||
private bool isAwaitingResponse = false;
|
||||
|
||||
|
||||
|
||||
void Start()
|
||||
{
|
||||
rb = GetComponent<Rigidbody>();
|
||||
@@ -58,9 +79,94 @@ public class SpringyButtonPhysics : MonoBehaviour {
|
||||
// --- 5. Convert back to world and move via Rigidbody ---
|
||||
Vector3 worldPos = buttonDirectionRoot.TransformPoint(newLocalPos);
|
||||
rb.MovePosition(worldPos);
|
||||
|
||||
//Keeping the buttons firing until their needs have been met
|
||||
|
||||
if (!isAwaitingResponse || disabled) return;
|
||||
if (!isInsideButton && box != null)
|
||||
{
|
||||
if (box.currentFloor == designatedFloor) StartCoroutine(DeactivateAfterDelay(0.5f));
|
||||
else Pressed();
|
||||
return;
|
||||
}
|
||||
if (isInsideButton && box != null)
|
||||
{
|
||||
|
||||
if (isFloorButton)
|
||||
{
|
||||
if (designatedFloor == box.currentFloor) StartCoroutine(DeactivateAfterDelay(0.5f));
|
||||
else Pressed();
|
||||
return;
|
||||
}
|
||||
if (isCloseDoorsButton)
|
||||
{
|
||||
if (box.state == ElevatorState.ClosingDoors|| box.state == ElevatorState.Moving) StartCoroutine(DeactivateAfterDelay(0.5f));
|
||||
else Pressed();
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (box.state == ElevatorState.OpeningDoors || box.state == ElevatorState.Moving) StartCoroutine(DeactivateAfterDelay(0.5f));
|
||||
else Pressed();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
public void Pressed()
|
||||
{
|
||||
Debug.Log("Button is pressed");
|
||||
isAwaitingResponse = true;
|
||||
|
||||
foreach (Renderer renderer in targetRenderers)
|
||||
{
|
||||
renderer.material = pressedMaterial;
|
||||
}
|
||||
//Debug.Log("Button is pressed");
|
||||
if (ElevatorOuter != null && !isInsideButton) ElevatorOuter.CallElevator();
|
||||
if (disabled) StartCoroutine(DeactivateAfterDelay(0.5f));
|
||||
if (box != null && isInsideButton)
|
||||
{
|
||||
if (isFloorButton)
|
||||
{
|
||||
box.targetFloor = designatedFloor;
|
||||
if (box.targetFloor != box.currentFloor) {
|
||||
Debug.Log("Lift transfer sequence");
|
||||
StartCoroutine(box.LiftTransferSequence());
|
||||
|
||||
}
|
||||
else StartCoroutine(box.OpenDoors());
|
||||
return;
|
||||
|
||||
}
|
||||
if (isCloseDoorsButton)
|
||||
{
|
||||
//Debug.Log("Closing Doors");
|
||||
StartCoroutine(box.CloseDoors());
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
StartCoroutine(box.OpenDoors());
|
||||
//Debug.Log("Opening Doors");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
public void Deactivate()
|
||||
{
|
||||
Debug.Log("Deactivating");
|
||||
//if (isAwaitingResponse) return;
|
||||
isAwaitingResponse = false;
|
||||
foreach (Renderer renderer in targetRenderers)
|
||||
{
|
||||
renderer.material = defaultMaterial;
|
||||
}
|
||||
|
||||
}
|
||||
private System.Collections.IEnumerator DeactivateAfterDelay(float delay)
|
||||
{
|
||||
yield return new WaitForSeconds(delay);
|
||||
//isAwaitingResponse = false;
|
||||
Deactivate();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user