diff --git a/Assets/Gothic-VR/Scripts/Adapters/Player/VRSwimDive.cs b/Assets/Gothic-VR/Scripts/Adapters/Player/VRSwimDive.cs index 929752dbe..edfd4c5bb 100644 --- a/Assets/Gothic-VR/Scripts/Adapters/Player/VRSwimDive.cs +++ b/Assets/Gothic-VR/Scripts/Adapters/Player/VRSwimDive.cs @@ -53,6 +53,8 @@ public class VRSwimDive : MonoBehaviour, IMarvinPropertyCollector // Swim + Dive properties private Vector3 _currentVelocity; + // The hero died (HeroDied): swimming, diving and the bobbing stop until a game is loaded (LoadGameStart). + private bool _isStoppedByDeath; // If we lift the grips, we set it to false again to re-enable sounds later. private bool _isSwimDiveForceLeftJustStarted; private bool _isSwimDiveForceRightJustStarted; @@ -105,6 +107,9 @@ private void Start() { Shader.SetGlobalInt(Constants.ShaderPropertyWaterEffectToggle, 0); + GlobalEventDispatcher.HeroDied.AddListener(OnHeroDied); + GlobalEventDispatcher.LoadGameStart.AddListener(OnLoadGameStart); + GlobalEventDispatcher.ZenKitBootstrapped.AddListener(() => { // G2's Humans.mds has the same swim animations and sounds as G1's. @@ -176,9 +181,15 @@ SfxModel FindSwimSfx(string animName) _initialFallSpeed = _rig.MaxFallSpeed; } + private void OnDestroy() + { + GlobalEventDispatcher.HeroDied.RemoveListener(OnHeroDied); + GlobalEventDispatcher.LoadGameStart.RemoveListener(OnLoadGameStart); + } + private void Update() { - if (_playerAi == null) + if (_playerAi == null || _isStoppedByDeath) return; if (_mode == VmGothicEnums.WalkMode.Swim) @@ -195,7 +206,7 @@ private void FixedUpdate() * 3. Check if we need to swim as w are chest deep in water. */ - if (_playerVob == null) + if (_playerVob == null || _isStoppedByDeath) return; // FIXME - As we have different sizes of people in VR, we should use the size based on real heights. @@ -541,6 +552,44 @@ private void HandleSwim() } } + /// + /// A dead hero doesn't swim: no strokes (the hands moving for the death menu pushed the corpse and the menu + /// with it), no bobbing, no dive (its dark fade ends), no switch of the water controls. The mode stays as it + /// was - loading a game starts over (LoadGameStart, then WorldSceneLoaded leaves the water if needed). + /// + private void OnHeroDied(NpcContainer hero) + { + if (_isStoppedByDeath) + return; + + _isStoppedByDeath = true; + _currentVelocity = Vector3.zero; + if (_waterBobbingCoroutine != null) + { + StopCoroutine(_waterBobbingCoroutine); + _waterBobbingCoroutine = null; + } + if (_mode == VmGothicEnums.WalkMode.Dive) + { + _playerService.StopDiving(); + _rig.Fade(0, float.MaxValue); + } + + Logger.Log($"[VRSwimDive] The hero died ({_mode}) - swimming stops until a game is loaded.", LogCat.VR); + } + + /// + /// A game is loaded: swimming works again, and the old world's hero isn't read while the new one loads + /// (WorldSceneLoaded sets him again). + /// + private void OnLoadGameStart() + { + _isStoppedByDeath = false; + _heroContainer = null; + _playerVob = null; + _playerAi = null; + } + private bool IsHandBelowWater(Transform handTransform) { RaycastHit hit;