This repository has been archived on 2024-08-16. You can view files and clone it, but cannot push or open issues or pull requests.
sota/RunningLateGame/Assets/Scripts/Door.cs

117 lines
3.2 KiB
C#
Raw Normal View History

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