using System.Collections;
using Unity.Cinemachine;
using UnityEngine;
using NightclubArcadia.Core;
namespace NightclubArcadia.Cinematics
{
///
/// Points a CinemachineCamera at the player once the player exists.
///
/// Level cameras — the reveal cameras in particular — used to hold a serialized
/// reference to the PlayerArmature. The player now lives in the Systems scene, so
/// that reference cannot be serialized any more and has to be bound at runtime.
///
[RequireComponent(typeof(CinemachineCamera))]
public sealed class CinemachineFollowsPlayer : MonoBehaviour
{
[Tooltip("Also drive LookAt, not just Follow.")]
[SerializeField] bool alsoLookAt;
[Tooltip("Seconds to keep retrying while the Systems scene finishes loading.")]
[SerializeField] float resolveTimeout = 5f;
IEnumerator Start()
{
var cam = GetComponent();
var deadline = Time.unscaledTime + resolveTimeout;
while (SceneServices.Player == null && Time.unscaledTime < deadline)
{
yield return null;
}
var player = SceneServices.Player;
if (player == null)
{
Debug.LogError($"[CinemachineFollowsPlayer] {name}: no object tagged Player appeared " +
$"within {resolveTimeout}s.", this);
yield break;
}
cam.Follow = player;
if (alsoLookAt)
{
cam.LookAt = player;
}
}
}
}