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

118 lines
3 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>
/// TODO
/// </summary>
public class Door : CommonInteractable
{
/// <summary>
/// TODO
/// </summary>
public enum DoorType
{
Sliding,
Rotating
}
/// <summary>
/// TODO
/// </summary>
public List<DoorInput> doors;
/// <summary>
/// TODO
/// </summary>
public float duration;
2024-08-10 05:14:03 +00:00
/// <summary>
/// TODO
/// </summary>
public AnimationCurve curve;
2024-08-10 05:14:03 +00:00
/// <summary>
/// TODO
/// </summary>
private bool _isOpen;
2024-08-10 05:14:03 +00:00
/// <summary>
/// TODO
/// </summary>
private bool _opening;
/// <summary>
/// TODO
/// </summary>
public override void Interact()
{
if (!_opening) StartCoroutine(OpenDoor());
}
/// <summary>
/// TODO
/// </summary>
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-09 18:28:59 +00:00
_isOpen = !_isOpen;
}
yield return new WaitForEndOfFrame();
}
}
/// <summary>
/// TODO
/// </summary>
// NOTE: why is this at the bottom lol
[Serializable]
public struct DoorInput
{
public GameObject doorObject;
public Vector3 startPosition;
public DoorType type;
public Vector3 endPosition;
}
}