2024-08-10 02:25:46 +08:00
|
|
|
/*
|
|
|
|
* author: ryan lin
|
|
|
|
* date: 8/8/2024
|
|
|
|
* description: door interaction and animation
|
|
|
|
*/
|
|
|
|
|
2024-08-08 14:22:58 +08:00
|
|
|
using System;
|
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using UnityEngine;
|
|
|
|
|
2024-08-10 02:25:46 +08:00
|
|
|
/// <summary>
|
2024-08-11 05:40:22 +08:00
|
|
|
/// door interaction and animation
|
2024-08-10 02:25:46 +08:00
|
|
|
/// </summary>
|
|
|
|
public class Door : CommonInteractable
|
2024-08-08 14:22:58 +08:00
|
|
|
{
|
2024-08-10 02:25:46 +08:00
|
|
|
/// <summary>
|
2024-08-11 05:40:22 +08:00
|
|
|
/// the type of door that is being used
|
2024-08-10 02:25:46 +08:00
|
|
|
/// </summary>
|
2024-08-08 14:22:58 +08:00
|
|
|
public enum DoorType
|
|
|
|
{
|
|
|
|
Sliding,
|
|
|
|
Rotating
|
|
|
|
}
|
|
|
|
|
2024-08-10 02:25:46 +08:00
|
|
|
/// <summary>
|
2024-08-11 05:40:22 +08:00
|
|
|
/// the list of doors that are being used
|
2024-08-10 02:25:46 +08:00
|
|
|
/// </summary>
|
2024-08-08 14:22:58 +08:00
|
|
|
public List<DoorInput> doors;
|
|
|
|
|
2024-08-10 02:25:46 +08:00
|
|
|
/// <summary>
|
2024-08-11 05:40:22 +08:00
|
|
|
/// the duration of the door opening
|
2024-08-10 02:25:46 +08:00
|
|
|
/// </summary>
|
2024-08-08 14:22:58 +08:00
|
|
|
public float duration;
|
2024-08-10 13:14:03 +08:00
|
|
|
|
2024-08-10 02:25:46 +08:00
|
|
|
/// <summary>
|
2024-08-11 05:40:22 +08:00
|
|
|
/// the animation curve of the door opening
|
2024-08-10 02:25:46 +08:00
|
|
|
/// </summary>
|
2024-08-08 14:22:58 +08:00
|
|
|
public AnimationCurve curve;
|
2024-08-10 13:14:03 +08:00
|
|
|
|
2024-08-10 02:25:46 +08:00
|
|
|
/// <summary>
|
2024-08-11 05:40:22 +08:00
|
|
|
/// a bool to check if the door is open
|
2024-08-10 02:25:46 +08:00
|
|
|
/// </summary>
|
2024-08-08 14:22:58 +08:00
|
|
|
private bool _isOpen;
|
2024-08-10 13:14:03 +08:00
|
|
|
|
2024-08-10 02:25:46 +08:00
|
|
|
/// <summary>
|
2024-08-11 05:40:22 +08:00
|
|
|
/// a bool to check if the door is opening
|
2024-08-10 02:25:46 +08:00
|
|
|
/// </summary>
|
2024-08-08 14:22:58 +08:00
|
|
|
private bool _opening;
|
|
|
|
|
2024-08-10 02:25:46 +08:00
|
|
|
/// <summary>
|
2024-08-11 05:40:22 +08:00
|
|
|
/// to interact with the door
|
2024-08-10 02:25:46 +08:00
|
|
|
/// </summary>
|
2024-08-08 14:22:58 +08:00
|
|
|
public override void Interact()
|
|
|
|
{
|
|
|
|
if (!_opening) StartCoroutine(OpenDoor());
|
|
|
|
}
|
|
|
|
|
2024-08-10 02:25:46 +08:00
|
|
|
/// <summary>
|
2024-08-11 05:40:22 +08:00
|
|
|
/// to open the door
|
2024-08-10 02:25:46 +08:00
|
|
|
/// </summary>
|
2024-08-08 14:22:58 +08:00
|
|
|
private IEnumerator OpenDoor()
|
|
|
|
{
|
|
|
|
_opening = true;
|
|
|
|
float currentDuration = 0;
|
|
|
|
while (_opening)
|
|
|
|
{
|
|
|
|
currentDuration += Time.deltaTime;
|
|
|
|
var t = currentDuration / duration;
|
|
|
|
foreach (var variable in doors)
|
|
|
|
if (!_isOpen)
|
|
|
|
{
|
|
|
|
if (variable.type == DoorType.Sliding)
|
|
|
|
variable.doorObject.transform.localPosition = Vector3.Lerp(variable.startPosition,
|
|
|
|
variable.endPosition,
|
|
|
|
curve.Evaluate(t));
|
|
|
|
else if (variable.type == DoorType.Rotating)
|
|
|
|
variable.doorObject.transform.localEulerAngles = Vector3.Slerp(variable.startPosition,
|
|
|
|
variable.endPosition,
|
|
|
|
curve.Evaluate(t));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (variable.type == DoorType.Sliding)
|
|
|
|
variable.doorObject.transform.localPosition = Vector3.Lerp(variable.endPosition,
|
|
|
|
variable.startPosition,
|
|
|
|
curve.Evaluate(t));
|
|
|
|
else if (variable.type == DoorType.Rotating)
|
|
|
|
variable.doorObject.transform.localEulerAngles = Vector3.Slerp(variable.endPosition,
|
|
|
|
variable.startPosition,
|
|
|
|
curve.Evaluate(t));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (currentDuration >= duration)
|
|
|
|
{
|
|
|
|
currentDuration = 0;
|
|
|
|
_opening = false;
|
2024-08-10 02:28:59 +08:00
|
|
|
_isOpen = !_isOpen;
|
2024-08-08 14:22:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
yield return new WaitForEndOfFrame();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-10 02:25:46 +08:00
|
|
|
/// <summary>
|
2024-08-11 05:40:22 +08:00
|
|
|
/// a struct to hold the door input
|
2024-08-10 02:25:46 +08:00
|
|
|
/// </summary>
|
2024-08-08 14:22:58 +08:00
|
|
|
[Serializable]
|
|
|
|
public struct DoorInput
|
|
|
|
{
|
|
|
|
public GameObject doorObject;
|
|
|
|
public Vector3 startPosition;
|
|
|
|
public DoorType type;
|
|
|
|
public Vector3 endPosition;
|
|
|
|
}
|
|
|
|
}
|