/*
* authors: ryan lin, mark joshwel
* date: 10/8/2024
* description: lift behaviour implementation
*/
using System.Collections;
using UnityEngine;
///
/// lift-ting behaviour for lifts
///
public class LiftController : MonoBehaviour
{
///
/// height of each level
///
public float levelHeight;
///
/// the time it takes for the lift to move from one floor to another
///
public float duration;
///
/// a bool to check if the lift is moving
///
[HideInInspector] public bool moving;
///
/// the current time of the lift moving for lerps
///
private float _currentTime;
///
/// the door of the lift
///
private Door _liftDoor;
///
/// the start position of the lift
///
private Vector3 _start;
///
/// the target position of the lift
///
private Vector3 _target;
///
/// initialisation function
///
private void Start()
{
_liftDoor = transform.parent.GetComponentInChildren();
DoorCloser();
}
///
/// to check if the door is open and call a function to close it
///
public void DoorCloser()
{
if (_liftDoor.isOpen && !_liftDoor.opening)
Invoke(nameof(CloseDoor), 2f);
Invoke(nameof(DoorCloser), 2f);
}
///
/// to close the door
///
public void CloseDoor()
{
_liftDoor.Interact();
}
///
/// to move the lift
///
public IEnumerator Move(int floorsMoved)
{
moving = true;
_start = transform.position;
_target = new Vector3(_start.x, _start.y + levelHeight * floorsMoved, _start.z);
_currentTime = 0;
while (_currentTime < duration)
{
_currentTime += Time.deltaTime;
transform.position = Vector3.Lerp(_start, _target, _currentTime / duration);
yield return null;
}
_liftDoor.StartCoroutine("OpenDoor");
moving = false;
}
}