camera weights added
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using Cinemachine;
|
||||
using FMODUnity;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Xml.Serialization;
|
||||
@@ -16,8 +17,11 @@ public class CameraManager : MonoBehaviour
|
||||
|
||||
private List<GameObject> m_CameraObjects = new List<GameObject>();
|
||||
|
||||
private int m_SameCameraTypeCount;
|
||||
private List<CameraSettings> m_CameraSettings = new List<CameraSettings>();
|
||||
|
||||
private float m_TotalWeight = 0;
|
||||
|
||||
private int m_SameCameraTypeCount;
|
||||
|
||||
[Header("Layer Masks")]
|
||||
public LayerMask m_Everything;
|
||||
@@ -84,7 +88,7 @@ public class CameraManager : MonoBehaviour
|
||||
if(Input.GetKeyDown(KeyCode.Alpha0 + ( i - ( m_CameraIndex * 10 ) ))){
|
||||
foreach(GameObject obj in m_CameraObjects){
|
||||
if(count == i){
|
||||
Debug.Log($"Swapping to:{obj.name}");
|
||||
Debug.Log($"Swapping to:{obj.name}, {m_CameraSettings[i].cameraWeight}");
|
||||
SetCamera(obj);
|
||||
break;
|
||||
}
|
||||
@@ -130,44 +134,45 @@ public class CameraManager : MonoBehaviour
|
||||
|
||||
private void SetNewCamera()
|
||||
{
|
||||
// Get all objects with 0 prio
|
||||
// Get all objects with 0 priority
|
||||
List<int> deactiveCameras = new List<int>();
|
||||
bool switchCameraType = false;
|
||||
|
||||
if (m_SameCameraTypeCount >= 3)
|
||||
switchCameraType = true;
|
||||
|
||||
// Count through camera objects, check if 0 priority (off), we add to
|
||||
// list of deactivated cameras.
|
||||
for(int i = 0; i < m_CameraObjects.Count; i++)
|
||||
{
|
||||
GameObject t = m_CameraObjects[i];
|
||||
CinemachineVirtualCamera virtualCamera = t.GetComponent<CinemachineVirtualCamera>();
|
||||
if (virtualCamera != null && virtualCamera.Priority == 0)
|
||||
{
|
||||
if (switchCameraType)
|
||||
{
|
||||
if(t.tag != m_ActiveCamera.tag)
|
||||
deactiveCameras.Add(i);
|
||||
}
|
||||
else
|
||||
{
|
||||
deactiveCameras.Add(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int randomIndex = deactiveCameras[Random.Range(0, deactiveCameras.Count - 1)];
|
||||
GameObject selectedCamera = m_CameraObjects[randomIndex];
|
||||
deactiveCameras.Add(i);
|
||||
|
||||
if (selectedCamera.tag == m_ActiveCamera.tag)
|
||||
{
|
||||
m_SameCameraTypeCount++;
|
||||
SetCamera(selectedCamera);
|
||||
}
|
||||
else
|
||||
{
|
||||
m_SameCameraTypeCount = 0;
|
||||
SetCamera(selectedCamera);
|
||||
}
|
||||
|
||||
List<int> shuffledList = ShuffleList(deactiveCameras);
|
||||
|
||||
float totalWeight = CalculateNewTotalWeight(shuffledList);
|
||||
|
||||
float randomIndex = Random.Range(0f, totalWeight);
|
||||
|
||||
Debug.Log($"Random Number: {randomIndex}");
|
||||
|
||||
GameObject selectedCamera = null;
|
||||
int selectedCameraIndex = 0;
|
||||
|
||||
for (int i = 0; i < shuffledList.Count; i++){
|
||||
randomIndex -= m_CameraSettings[shuffledList[i]].cameraWeight;
|
||||
Debug.Log($"Adj Index: {randomIndex}, Weight: {totalWeight}, {m_CameraSettings[shuffledList[i]].cameraWeight}, Name: {m_CameraObjects[shuffledList[i]].name}");
|
||||
if(randomIndex <= 0){
|
||||
// Activate Selected Camera
|
||||
selectedCamera = m_CameraObjects[shuffledList[i]];
|
||||
selectedCameraIndex = shuffledList[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Debug.Log($"Selected Camera: {selectedCamera.name}, {m_CameraSettings[selectedCameraIndex].cameraWeight}");
|
||||
|
||||
SetCamera(selectedCamera);
|
||||
}
|
||||
|
||||
private void SetCamera(GameObject camera)
|
||||
@@ -243,9 +248,42 @@ public class CameraManager : MonoBehaviour
|
||||
}
|
||||
|
||||
Debug.Log($"Camera Count: {m_CameraObjects.Count}");
|
||||
|
||||
CreateCameraSettingsList();
|
||||
|
||||
}
|
||||
|
||||
private void CreateCameraSettingsList(){
|
||||
|
||||
VirtualCameraSettings camSettings = null;
|
||||
|
||||
foreach(GameObject cam in m_CameraObjects){
|
||||
camSettings = null;
|
||||
|
||||
try{
|
||||
camSettings = cam.GetComponent<VirtualCameraSettings>();
|
||||
} catch {
|
||||
Debug.LogError("No Camera Settings found.");
|
||||
}
|
||||
|
||||
if(camSettings != null){
|
||||
m_CameraSettings.Add(camSettings.m_CameraSettings);
|
||||
m_TotalWeight += camSettings.m_CameraSettings.cameraWeight;
|
||||
}
|
||||
}
|
||||
|
||||
Debug.Log($"Total Weight: {m_TotalWeight}");
|
||||
}
|
||||
|
||||
private float CalculateNewTotalWeight(List<int> deactiveCameras){
|
||||
|
||||
float totalWeight = 0;
|
||||
foreach(int index in deactiveCameras){
|
||||
totalWeight += m_CameraSettings[index].cameraWeight;
|
||||
}
|
||||
return totalWeight;
|
||||
}
|
||||
|
||||
void AddActiveChildrenRecursively(Transform parentTransform)
|
||||
{
|
||||
foreach (Transform child in parentTransform)
|
||||
@@ -260,7 +298,15 @@ public class CameraManager : MonoBehaviour
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private List<int> ShuffleList(List<int> list){
|
||||
|
||||
for (int i = list.Count - 1; i > 0; i--){
|
||||
int j = Random.Range(0, i + 1);
|
||||
int temp = list[i];
|
||||
list[i] = list[j];
|
||||
list[j] = temp;
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user