diff --git a/Fika.Core/Coop/FreeCamera/FreeCameraController.cs b/Fika.Core/Coop/FreeCamera/FreeCameraController.cs index 8c7d2d9e..b5e8cca0 100644 --- a/Fika.Core/Coop/FreeCamera/FreeCameraController.cs +++ b/Fika.Core/Coop/FreeCamera/FreeCameraController.cs @@ -6,10 +6,13 @@ using Fika.Core.Coop.GameMode; using Fika.Core.Coop.Players; using Fika.Core.UI; +using HarmonyLib; using Koenigz.PerfectCulling; using Koenigz.PerfectCulling.EFT; +using System.Collections; using System.Collections.Generic; using System.Linq; +using System.Reflection; using TMPro; using UnityEngine; @@ -31,7 +34,8 @@ public class FreeCameraController : MonoBehaviour private bool _effectsCleared = false; private GamePlayerOwner _gamePlayerOwner; - private Player _player => Singleton.Instance.MainPlayer; + private CoopPlayer _player => (CoopPlayer)Singleton.Instance.MainPlayer; + private CoopHandler coopHandler; public GameObject CameraParent; public Camera CameraFreeCamera { get; private set; } @@ -41,7 +45,6 @@ public class FreeCameraController : MonoBehaviour private bool extracted = false; private DeathFade deathFade; private bool deathFadeEnabled; - private float DeadTime = 0f; private DisablerCullingObjectBase[] allCullingObjects; private List previouslyActiveBakeGroups; @@ -80,6 +83,26 @@ protected void Start() allCullingObjects = FindObjectsOfType(); previouslyActiveBakeGroups = []; + + _player.ActiveHealthController.DiedEvent += MainPlayer_DiedEvent; + + if (CoopHandler.TryGetCoopHandler(out CoopHandler cHandler)) + { + coopHandler = cHandler; + } + } + + private void MainPlayer_DiedEvent(EDamageType obj) + { + _player.ActiveHealthController.DiedEvent -= MainPlayer_DiedEvent; + + if (!deathFadeEnabled) + { + deathFade.EnableEffect(); + deathFadeEnabled = true; + } + + StartCoroutine(DeathRoutine()); } protected void Update() @@ -97,18 +120,7 @@ protected void Update() if (_player.PlayerHealthController == null) { return; - } - - if (!CoopHandler.TryGetCoopHandler(out CoopHandler coopHandler)) - { - return; - } - - CoopGame coopGame = (CoopGame)coopHandler.LocalGameInstance; - if (coopGame == null) - { - return; - } + } CoopHandler.EQuitState quitState = coopHandler.GetQuitState(); @@ -134,7 +146,8 @@ protected void Update() if (quitState == CoopHandler.EQuitState.YouHaveExtracted && !extracted) { - if (coopGame.ExtractedPlayers.Contains(((CoopPlayer)_player).NetId)) + CoopGame coopGame = (CoopGame)coopHandler.LocalGameInstance; + if (coopGame.ExtractedPlayers.Contains((_player).NetId)) { extracted = true; ShowExtractMessage(); @@ -161,64 +174,95 @@ protected void Update() _effectsCleared = true; } } + } - if (!_player.HealthController.IsAlive) + private IEnumerator DeathRoutine() + { + yield return new WaitForSeconds(5); + + CameraClass cameraClassInstance = CameraClass.Instance; + if (cameraClassInstance == null) { - DeadTime += Time.deltaTime; - if (!deathFadeEnabled) - { - deathFade.EnableEffect(); - deathFadeEnabled = true; - } + yield break; } - // Player is dead. Remove all effects! - if (!_player.HealthController.IsAlive && !_freeCamScript.IsActive && DeadTime > 5f) + if (cameraClassInstance.EffectsController == null) { - CameraClass cameraClassInstance = CameraClass.Instance; - if (cameraClassInstance == null) - { - return; - } + yield break; + } + + if (cameraClassInstance.Camera != null) + { + cameraClassInstance.Camera.fieldOfView = Singleton.Instance.Game.Settings.FieldOfView; + } + + // Disable the DeathFade effect & Toggle the Camera + deathFade.DisableEffect(); + if (!_freeCamScript.IsActive) + { + ToggleCamera(); + ToggleUi(); + } + ShowExtractMessage(); - if (cameraClassInstance.EffectsController == null) + if (!_effectsCleared) + { + if (_player != null) { - return; + _player.Muffled = false; + _player.HeavyBreath = false; } - if (cameraClassInstance.Camera != null) + if (CameraClass.Exist) { - cameraClassInstance.Camera.fieldOfView = Singleton.Instance.Game.Settings.FieldOfView; + ClearEffects(); } + _effectsCleared = true; + } + } - // Disable the DeathFade effect & Toggle the Camera - deathFade.DisableEffect(); - ToggleCamera(); - ToggleUi(); - ShowExtractMessage(); + private void ClearEffects() + { + CameraClass cameraClass = CameraClass.Instance; + cameraClass.EffectsController.method_4(false); - if (!_effectsCleared) + Traverse effectsController = Traverse.Create(cameraClass.EffectsController); + + BloodOnScreen bloodOnScreen = effectsController.Field("bloodOnScreen_0").GetValue(); + if (bloodOnScreen != null) + { + Destroy(bloodOnScreen); + } + + List effectsManagerList = effectsController.Field("list_0").GetValue>(); + if (effectsManagerList != null) + { + foreach (EffectsController.Class566 effectsManager in effectsManagerList) { - if (_player != null) + while (effectsManager.ActiveEffects.Count > 0) { - _player.Muffled = false; - _player.HeavyBreath = false; + IEffect effect = effectsManager.ActiveEffects[0]; + effectsManager.DeleteEffect(effect); } + } + effectsManagerList.Clear(); + } - if (CameraClass.Exist) - { - ClearEffects(); - } - _effectsCleared = true; + CC_Wiggle wiggleEffect = cameraClass.Camera.gameObject.GetComponent(); + if (wiggleEffect != null) + { + wiggleEffect.enabled = false; + } + + CC_Blend[] blendEffects = cameraClass.Camera.gameObject.GetComponents(); + if (blendEffects.Length > 0) + { + foreach (CC_Blend blendEffect in blendEffects) + { + blendEffect.enabled = false; } } - } - private void ClearEffects() - { - CameraClass cameraClass = CameraClass.Instance; - cameraClass.EffectsController.method_4(false); - cameraClass.EffectsController.enabled = false; Destroy(cameraClass.EffectsController); cameraClass.VisorEffect.Clear(); Destroy(cameraClass.VisorEffect); @@ -432,11 +476,11 @@ public void OnDestroy() // Destroy FreeCamScript before FreeCamController if exists Destroy(_freeCamScript); - Destroy(this); if (extractText != null) { Destroy(extractText); } + Destroy(this); } } }