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/LiftController.cs

96 lines
2.2 KiB
C#
Raw Normal View History

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