OldBlueWater/BlueWater/Assets/02.Scripts/CombatCamera.cs

149 lines
4.2 KiB
C#
Raw Normal View History

using System.Collections;
using System.Collections.Generic;
using Cinemachine;
using Sirenix.OdinInspector;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;
// ReSharper disable once CheckNamespace
namespace BlueWaterProject
{
public class CombatCamera : MonoBehaviour
{
[Title("초기화 방식")]
[SerializeField] private bool autoInit = true;
[field: Title("카메라")]
[field: SerializeField] public CinemachineVirtualCamera BaseCombatCamera { get; private set; }
private GameObject cinemachineCameras;
private List<CinemachineVirtualCamera> cineCamList;
private Vignette vignette;
private Coroutine lowHpVignetteCoroutine;
private float originalRotation;
private const int CINE_CAM_NUM = 1;
private void Awake()
{
if (autoInit)
{
Init();
}
CameraManager.Inst.CombatCamera = this;
CameraManager.Inst.MainCam = Camera.main;
vignette = GetEffect<Vignette>();
vignette.active = false;
}
[Button("셋팅 초기화")]
private void Init()
{
cinemachineCameras = GameObject.Find("CinemachineCameras");
if (!cinemachineCameras)
{
Debug.LogError("cineCams is null error");
return;
}
BaseCombatCamera = cinemachineCameras.transform.Find("BaseCombatCamera")?.GetComponent<CinemachineVirtualCamera>();
if (!BaseCombatCamera)
{
Debug.LogError("BaseShipCam is null error");
return;
}
cineCamList = new List<CinemachineVirtualCamera>(CINE_CAM_NUM)
{
BaseCombatCamera
};
foreach (var cam in cineCamList)
{
cam.Priority = 0;
}
BaseCombatCamera.Priority = 1;
CameraManager.Inst.MainCam = Camera.main;
}
public void SetFollowAndLookAt(Transform target)
{
BaseCombatCamera.Follow = target;
//BaseCombatCamera.LookAt = target;
}
#region PostProcessing
public void ToggleEffect<T>(bool value) where T : VolumeComponent
{
var effect = GetEffect<T>();
if (effect == null)
{
print(typeof(T) + "효과가 없습니다.");
return;
}
effect.active = value;
}
private T GetEffect<T>() where T : VolumeComponent
{
var postProcessVolume = FindAnyObjectByType<Volume>();
if (postProcessVolume == null)
{
print("Volume 컴포넌트를 가진 오브젝트가 없습니다.");
return null;
}
postProcessVolume.profile.TryGet(out T effect);
return effect;
}
public void LowHpVignette()
{
if (lowHpVignetteCoroutine == null)
{
lowHpVignetteCoroutine = StartCoroutine(LowHpVignetteCoroutine());
}
}
public void StopLowHpVignette()
{
if (lowHpVignetteCoroutine != null)
{
StopCoroutine(lowHpVignetteCoroutine);
lowHpVignetteCoroutine = null;
vignette.active = false;
}
}
private IEnumerator LowHpVignetteCoroutine()
{
var startValue = 0.2f;
var endValue = 0.3f;
var time = 0f;
vignette.intensity.value = startValue;
vignette.active = true;
while (true)
{
time += Time.deltaTime * 2f;
vignette.intensity.value = Mathf.Lerp(startValue, endValue, time);
if (time >= 1f)
{
(startValue, endValue) = (endValue, startValue);
time = 0f;
}
yield return null;
}
}
#endregion
}
}