/*
* author: ryan lin
* date: 8/8/2024
* description: door interaction and animation
*/
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
///
/// door interaction and animation
///
public class Door : CommonInteractable
{
///
/// the type of door that is being used
///
public enum DoorType
{
Sliding,
Rotating
}
///
/// the list of doors that are being used
///
public List doors;
///
/// the duration of the door opening
///
public float duration;
///
/// the animation curve of the door opening
///
public AnimationCurve curve;
///
/// a bool to check if the door is open
///
[HideInInspector] public bool isOpen=false;
///
/// a bool to check if the door is opening
///
[HideInInspector] public bool opening;
///
/// set the interaction prompt
///
private void Awake()
{
interactionPrompt = "Press [E] to open the door";
}
///
/// to interact with the door
///
public override void Interact()
{
if (!opening) StartCoroutine(OpenDoor());
}
///
/// to open the door
///
public 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;
isOpen = !isOpen;
}
yield return new WaitForEndOfFrame();
}
}
///
/// a struct to hold the door input
///
[Serializable]
public struct DoorInput
{
public GameObject doorObject;
public Vector3 startPosition;
public DoorType type;
public Vector3 endPosition;
}
}