2024-08-10 20:11:35 +08:00
|
|
|
/*
|
2024-08-11 13:51:29 +08:00
|
|
|
* authors: ryan lin, mark joshwel
|
2024-08-10 20:11:35 +08:00
|
|
|
* date: 10/8/2024
|
|
|
|
* description: lift behaviour implementation
|
|
|
|
*/
|
|
|
|
|
2024-08-11 13:51:29 +08:00
|
|
|
using System.Collections;
|
2024-08-10 20:11:35 +08:00
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// lift-ting behaviour for lifts
|
|
|
|
/// </summary>
|
|
|
|
public class LiftController : MonoBehaviour
|
|
|
|
{
|
2024-08-11 13:51:29 +08:00
|
|
|
/// <summary>
|
|
|
|
/// height of each level
|
|
|
|
/// </summary>
|
|
|
|
public float levelHeight;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// the time it takes for the lift to move from one floor to another
|
|
|
|
/// </summary>
|
|
|
|
public float duration;
|
2024-08-11 19:57:50 +08:00
|
|
|
|
2024-08-11 13:51:29 +08:00
|
|
|
/// <summary>
|
|
|
|
/// a bool to check if the lift is moving
|
|
|
|
/// </summary>
|
|
|
|
[HideInInspector] public bool moving;
|
2024-08-11 19:57:50 +08:00
|
|
|
|
2024-08-11 13:51:29 +08:00
|
|
|
/// <summary>
|
|
|
|
/// the current time of the lift moving for lerps
|
|
|
|
/// </summary>
|
|
|
|
private float _currentTime;
|
2024-08-11 19:57:50 +08:00
|
|
|
|
2024-08-11 13:51:29 +08:00
|
|
|
/// <summary>
|
2024-08-11 19:57:50 +08:00
|
|
|
/// the door of the lift
|
2024-08-11 13:51:29 +08:00
|
|
|
/// </summary>
|
|
|
|
private Door _liftDoor;
|
2024-08-11 19:57:50 +08:00
|
|
|
|
2024-08-11 13:51:29 +08:00
|
|
|
/// <summary>
|
|
|
|
/// the start position of the lift
|
|
|
|
/// </summary>
|
|
|
|
private Vector3 _start;
|
2024-08-11 19:57:50 +08:00
|
|
|
|
2024-08-11 13:51:29 +08:00
|
|
|
/// <summary>
|
|
|
|
/// the target position of the lift
|
|
|
|
/// </summary>
|
|
|
|
private Vector3 _target;
|
2024-08-11 19:57:50 +08:00
|
|
|
|
2024-08-11 13:51:29 +08:00
|
|
|
/// <summary>
|
|
|
|
/// initialisation function
|
|
|
|
/// </summary>
|
|
|
|
private void Start()
|
|
|
|
{
|
|
|
|
_liftDoor = transform.parent.GetComponentInChildren<Door>();
|
|
|
|
DoorCloser();
|
|
|
|
}
|
2024-08-11 19:57:50 +08:00
|
|
|
|
2024-08-11 13:51:29 +08:00
|
|
|
/// <summary>
|
|
|
|
/// to check if the door is open and call a function to close it
|
|
|
|
/// </summary>
|
|
|
|
public void DoorCloser()
|
|
|
|
{
|
|
|
|
Debug.Log("DoorCloser");
|
|
|
|
if (_liftDoor.isOpen && !_liftDoor.opening)
|
|
|
|
Invoke(nameof(CloseDoor), 2f);
|
|
|
|
Invoke(nameof(DoorCloser), 2f);
|
|
|
|
}
|
2024-08-11 19:57:50 +08:00
|
|
|
|
2024-08-11 13:51:29 +08:00
|
|
|
/// <summary>
|
2024-08-11 19:57:50 +08:00
|
|
|
/// to close the door
|
2024-08-11 13:51:29 +08:00
|
|
|
/// </summary>
|
|
|
|
public void CloseDoor()
|
|
|
|
{
|
|
|
|
_liftDoor.Interact();
|
|
|
|
}
|
2024-08-11 19:57:50 +08:00
|
|
|
|
2024-08-11 13:51:29 +08:00
|
|
|
/// <summary>
|
2024-08-11 19:57:50 +08:00
|
|
|
/// to move the lift
|
2024-08-11 13:51:29 +08:00
|
|
|
/// </summary>
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
moving = false;
|
|
|
|
}
|
|
|
|
}
|