Files
beyond/Assets/Scripts/Effects/FightCamera.cs

154 lines
4.8 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
using Invector.vMelee;
using Sirenix.OdinInspector;
using UnityEngine;
using UnityEngine.Rendering;
using Random = UnityEngine.Random;
namespace Beyond
{
[RequireComponent(typeof(Volume))]
public class FightCamera : MonoBehaviour
{
public enum PostProcessType
{
ON_LOCKON,
ON_COMBAT,
NONE
}
private bLockOn m_lockOn;
private vMeleeManager m_meleeManager;
[SerializeField] private Volume m_volume;
private float m_weight = 0f;
[SerializeField] private float m_transitionTime = 1f;
[SerializeField] private float m_camShakeTime = 0.2f;
[SerializeField] private float m_shakeAmp = .1f;
public PostProcessType m_postProcessType = PostProcessType.ON_COMBAT;
private Vector3 m_orginalPos;
private float m_camShakeTimer = 0f;
// Start is called before the first frame update
void Start()
{
if (m_postProcessType == PostProcessType.ON_LOCKON)
m_lockOn = Player.Instance.gameObject.GetComponent<bLockOn>();
else if (m_postProcessType == PostProcessType.ON_COMBAT)
GameStateManager.Instance.m_OnStateChanged.AddListener(OnGameStateChange);
m_meleeManager = Player.Instance.gameObject.GetComponent<vMeleeManager>();
if (m_lockOn)
{
m_lockOn.onLockOnTarget.AddListener(OnLockOn);
m_lockOn.onUnLockOnTarget.AddListener(OnUnlockOn);
}
if (m_meleeManager)
{
m_meleeManager.onDamageHit.AddListener(OnDamageHit);
m_meleeManager.onRecoilHit.AddListener(OnDamageHit);
}
if (m_volume == null)
m_volume = GetComponent<Volume>();
if (m_volume)
{
m_volume.enabled = false;
m_volume.weight = 0f;
}
}
private void OnGameStateChange(GameStateManager.State state)
{
SetCameraPostprocess(state == GameStateManager.State.COMBAT);
}
private void OnDestroy()
{
if (m_lockOn)
{
m_lockOn.onLockOnTarget.RemoveListener(OnLockOn);
m_lockOn.onLockOnTarget.RemoveListener(OnUnlockOn);
}
if (m_meleeManager)
{
m_meleeManager.onDamageHit.RemoveListener(OnDamageHit);
m_meleeManager.onRecoilHit.RemoveListener(OnDamageHit);
}
if (m_postProcessType == PostProcessType.ON_COMBAT && GameStateManager.Instance != null)
GameStateManager.Instance.m_OnStateChanged.RemoveListener(OnGameStateChange);
}
[Button]
private void OnDamageHit(vHitInfo vHitInfo)
{
m_camShakeTimer = m_camShakeTime;
}
private IEnumerator AnimateWeight(bool up)
{
float speed = 1f / m_transitionTime;
if (up && m_weight < 1f)
{
m_volume.enabled = true;
while (m_weight < 1f)
{
m_weight += Mathf.Clamp01(speed * Time.deltaTime);
m_volume.weight = m_weight;
yield return null;
}
}
else if (!up && m_weight > 0f)
{
while (m_weight > 0f)
{
m_weight -= Mathf.Clamp01(speed * Time.deltaTime);
m_volume.weight = m_weight;
yield return null;
}
m_volume.enabled = false;
}
yield return null;
}
public void SetCameraPostprocess(bool enable)
{
StopCoroutine("AnimateWeight");
StartCoroutine(AnimateWeight(enable));
}
public void OnLockOn(Transform tras)
{
SetCameraPostprocess(true);
}
public void OnUnlockOn(Transform tras)
{
SetCameraPostprocess(false);
}
// Update is called once per frame
void LateUpdate()
{
if (m_camShakeTimer > 0f)
{
//Vector3 shake = new Vector3(Random.Range(-1f, 1f), Random.Range(-1f, 1f), Random.Range(-1f, 1f));
float amp = m_shakeAmp * Mathf.Cos(Mathf.PI * m_camShakeTimer / m_camShakeTime);
var shake = Random.insideUnitSphere * amp;
m_camShakeTimer -= Time.deltaTime;
m_orginalPos = transform.position;
transform.position += shake;
}
}
private void OnPostRender()
{
transform.position = m_orginalPos;
}
}
}