Files
DougDiggem/Assets/Scripts/Management/TeleportHandler.cs
T

80 lines
2.7 KiB
C#

using System.Collections;
using UnityEngine;
using UnityEngine.Rendering.Universal;
using UnityEngine.UI;
// Handle room and scene transitions
public class TeleportHandler : MonoBehaviour
{
Image blackScreenObject;
PlayerController playerController;
private void Start()
{
blackScreenObject = GameManager.Instance.GetBlackScreen();
playerController = GetComponent<PlayerController>();
}
/// <summary>
/// Triggers the room entrance coroutine, only works if the character is not controllable (keeps from double transitioning)
/// </summary>
public void EnterRoom(RoomDoor door)
{
if (playerController.GetCharacterHasControl())
StartCoroutine(EnterRoomCoroutine(door));
}
/// <summary>
/// Handle teleportation and timing for entering rooms within a scene
/// </summary>
IEnumerator EnterRoomCoroutine(RoomDoor door)
{
float fadeDuration = 0.2f; // how long to fade to/from black
float moveDuration = 0.4f; // how long to auto walk into room from the door
float fadeTime = 0;
Color blackScreenColor = Color.black;
playerController.SetCharacterControl(false);
// Play door open animation
if (door.requiresInteraction)
yield return new WaitForSeconds(0.2f);
while (fadeTime < fadeDuration)
{
blackScreenColor.a = Mathf.Lerp(0, 1, fadeTime / fadeDuration);
blackScreenObject.color = blackScreenColor;
fadeTime += Time.deltaTime;
yield return null;
}
fadeTime = 0;
playerController.CharacterControllerMove(door.linkedDoor.transform.position - transform.position);
playerController.DougBody.transform.rotation = door.linkedDoor.transform.rotation;
while (fadeTime < fadeDuration)
{
blackScreenColor.a = Mathf.Lerp(1, 0, fadeTime / fadeDuration);
blackScreenObject.color = blackScreenColor;
playerController.WalkInDirection((door.linkedDoor.WalkDirection.position - playerController.transform.position).normalized);
fadeTime += Time.deltaTime;
yield return null;
}
// move character a little more
while (fadeTime < moveDuration)
{
playerController.WalkInDirection((door.linkedDoor.WalkDirection.position - playerController.transform.position).normalized);
fadeTime += Time.deltaTime;
yield return null;
}
fadeTime = 0;
// Play exit animation for linked door (if it exists)
door.linkedDoor.CloseDoor();
yield return new WaitForSeconds(0.2f);
playerController.SetCharacterControl(true);
}
}