Files
DougDiggem/Assets/Scripts/TeleportHandler.cs
T
2026-02-28 10:45:50 -06:00

75 lines
2.5 KiB
C#

using System.Collections;
using UnityEngine;
using UnityEngine.Rendering.Universal;
using UnityEngine.UI;
// Handle room and scene transitions
public class TeleportHandler : MonoBehaviour
{
public Image blackScreenObject;
PlayerController playerController;
private void Start()
{
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;
float fadeTime = 0;
Color blackScreenColor = Color.black;
playerController.SetCharacterControl(false);
// Don't use the Y axis to look at the door
Vector3 positionToLookAt = new Vector3(door.transform.position.x, playerController.DougBody.transform.position.y, door.transform.position.z);
playerController.DougBody.transform.LookAt(positionToLookAt);
// Play door open animation
if (door.requiresInteraction)
yield return new WaitForSeconds(0.2f);
blackScreenObject.gameObject.SetActive(true);
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;
fadeTime += Time.deltaTime;
yield return null;
}
fadeTime = 0;
blackScreenObject.gameObject.SetActive(false);
// Play exit animation for linked door (if it exists)
door.linkedDoor.CloseDoor();
yield return new WaitForSeconds(0.2f);
playerController.SetCharacterControl(true);
}
}