You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

103 lines
3.0 KiB

10 months ago
using Cinemachine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Moloch
{
public class CameraManager : MonoBehaviour
{
[SerializeField] public List<CinemachineVirtualCamera> m_VirtualCameras = new List<CinemachineVirtualCamera>();
private CinemachineBrain m_Brain;
private CinemachineBlend m_DefaultBlend;
private int m_CameraWaitPeriod;
[Range(1, 30)]
[SerializeField] public int m_MinWaitPeriod;
[Range(1, 30)]
[SerializeField] public int m_MaxWaitPeriod;
private void OnEnable()
{
m_Brain = GetComponent<CinemachineBrain>();
if (m_VirtualCameras.Count == 0)
Debug.LogError("No virtual cameras found");
}
private void Start()
{
SetInitialCameraState();
SetNewWaitPeriod();
StartCoroutine(StartCameraCycle());
}
private IEnumerator StartCameraCycle()
{
yield return new WaitForSecondsRealtime(m_CameraWaitPeriod);
SetNewWaitPeriod();
SetNewCamera();
StartCoroutine(StartCameraCycle());
}
private void SetInitialCameraState()
{
// Sets all cameras to false, bar the first one in the list
foreach (var cam in m_VirtualCameras)
{
cam.Priority = 2;
}
m_VirtualCameras[0].Priority = 1;
}
private void SetNewWaitPeriod()
{
m_CameraWaitPeriod = (int)Random.Range(m_MinWaitPeriod, m_MaxWaitPeriod);
}
private void SetNewCamera()
{
// Logic to set new camera
// List to store inactive cameras
List<CinemachineVirtualCamera> inactiveCameras = new List<CinemachineVirtualCamera>();
// Find inactive cameras
foreach (var cam in m_VirtualCameras)
{
if (cam.Priority == 2)
{
inactiveCameras.Add(cam);
}
}
// If there are inactive cameras, choose one randomly and activate it
if (inactiveCameras.Count > 0)
{
// Select a random index from the list of inactive cameras
int randomIndex = Random.Range(0, inactiveCameras.Count);
// Deactivate the currently active camera
foreach (var cam in m_VirtualCameras)
{
if (cam.Priority == 1)
{
cam.Priority = 2;
break; // Stop iterating once we deactivate one camera
}
}
// Activate the randomly chosen inactive camera
inactiveCameras[randomIndex].Priority = 1;
}
else
{
// If all cameras are active, log a warning
Debug.LogWarning("All cameras are already active.");
}
}
}
}