Add utils + refacto

This commit is contained in:
2026-01-30 17:39:26 +01:00
parent 9ff17b5305
commit 23d3cb5d2c
29 changed files with 675 additions and 461 deletions
@@ -1,5 +1,6 @@
using UnityEngine;
using Unheimlich.Interactions.Core;
using Unheimlich.Utility;
namespace Unheimlich.Gameplay.Environment
{
@@ -29,21 +30,17 @@ namespace Unheimlich.Gameplay.Environment
public void ToggleDoor()
{
LeanTween.cancel(gameObject);
gameObject.CancelAllTweens();
if (!IsOpen)
{
LeanTween.rotate(gameObject, _openRotation.eulerAngles, _animationDuration)
.setEase(_easeType);
TweenHelper.RotateLocal(gameObject, _openRotation.eulerAngles, _animationDuration, null, _easeType);
IsOpen = true;
InteractionText = "Close";
}
else
{
LeanTween.rotate(gameObject, _closedRotation.eulerAngles, _animationDuration)
.setEase(_easeType);
TweenHelper.RotateLocal(gameObject, _closedRotation.eulerAngles, _animationDuration, null, _easeType);
IsOpen = false;
InteractionText = "Open";
}
@@ -2,6 +2,8 @@ using System.Collections;
using UnityEngine;
using UnityEngine.InputSystem;
using Unheimlich.Audio;
using Unheimlich.Core;
using Unheimlich.Utility;
using InputManager = Unheimlich.Managers.PlayerInputManager;
namespace Unheimlich.Gameplay.Player
@@ -16,14 +18,11 @@ namespace Unheimlich.Gameplay.Player
/// - Visual feedback (vignette, cursor, particles)
/// - Preventing multiple taps on the same emitter
/// </summary>
public class DiapasonListeningHandler : MonoBehaviour
public class DiapasonListeningHandler : Singleton<DiapasonListeningHandler>
{
// Animator parameter names for diapason animations
private const string ANIM_TRIGGER_TAP = "Tap";
private const string ANIM_BOOL_VIBRATING = "IsVibrating";
public static DiapasonListeningHandler Instance { get; private set; }
[Header("References")]
[SerializeField] private GameObject _diapasonObject;
[SerializeField] private Animator _diapasonAnimator;
@@ -72,23 +71,18 @@ namespace Unheimlich.Gameplay.Player
private AudioListenable _currentTargetEmitter;
private AudioSource _playerAudioSource;
private Coroutine _listeningCoroutine;
private Coroutine _tweenCoroutine;
private Coroutine _audioFadeCoroutine;
private int _diapasonTweenId = -1;
private int _audioListenerFadeTweenId = -1;
private int _playerAudioFadeTweenId = -1;
private Vector2 _originalCameraRotation;
private float _originalAmbientVolume;
private Vector3 _diapasonEquippedPosition;
private Vector3 _diapasonHiddenPosition;
private bool _canUseDiapason;
private bool _isFadingOutConcentration;
private void Awake()
protected override void OnAwake()
{
if (Instance != null && Instance != this)
{
Destroy(this);
return;
}
Instance = this;
_playerAudioSource = GetComponent<AudioSource>();
if (_playerAudioSource == null)
_playerAudioSource = gameObject.AddComponent<AudioSource>();
@@ -217,6 +211,9 @@ namespace Unheimlich.Gameplay.Player
/// </summary>
private void StartListeningMode()
{
if (_isFadingOutConcentration)
return;
if (_listeningCoroutine != null)
StopCoroutine(_listeningCoroutine);
@@ -240,17 +237,21 @@ namespace Unheimlich.Gameplay.Player
if (_diapasonObject != null)
{
if (_tweenCoroutine != null)
StopCoroutine(_tweenCoroutine);
TweenHelper.CancelTween(ref _diapasonTweenId);
_tweenCoroutine = StartCoroutine(TweenDiapasonPosition(_diapasonEquippedPosition, _diapasonTweenDuration, () =>
{
if (_diapasonAnimator != null)
_diapasonTweenId = TweenHelper.MoveLocalWithCurve(
_diapasonObject,
_diapasonEquippedPosition,
_diapasonTweenDuration,
_diapasonTweenCurve,
() =>
{
_diapasonAnimator.enabled = true;
_diapasonAnimator.SetBool(ANIM_BOOL_VIBRATING, false);
}
}));
if (_diapasonAnimator != null)
{
_diapasonAnimator.enabled = true;
_diapasonAnimator.SetBool(ANIM_BOOL_VIBRATING, false);
}
});
}
PlayBreathIn();
@@ -294,9 +295,21 @@ namespace Unheimlich.Gameplay.Player
if (_playerAudioSource != null && _playerAudioSource.isPlaying)
{
if (_audioFadeCoroutine != null)
StopCoroutine(_audioFadeCoroutine);
_audioFadeCoroutine = StartCoroutine(FadeOutAudio(_playerAudioSource, _audioFadeDuration));
TweenHelper.CancelTween(ref _playerAudioFadeTweenId);
float startVolume = _playerAudioSource.volume;
_playerAudioFadeTweenId = TweenHelper.FadeVolume(
_playerAudioSource,
0f,
_audioFadeDuration,
() =>
{
if (_playerAudioSource != null)
{
_playerAudioSource.Stop();
_playerAudioSource.volume = startVolume;
}
});
}
if (_diapasonObject != null)
@@ -307,10 +320,13 @@ namespace Unheimlich.Gameplay.Player
_diapasonAnimator.enabled = false;
}
if (_tweenCoroutine != null)
StopCoroutine(_tweenCoroutine);
TweenHelper.CancelTween(ref _diapasonTweenId);
_tweenCoroutine = StartCoroutine(TweenDiapasonPosition(_diapasonHiddenPosition, _diapasonTweenDuration));
_diapasonTweenId = TweenHelper.MoveLocalWithCurve(
_diapasonObject,
_diapasonHiddenPosition,
_diapasonTweenDuration,
_diapasonTweenCurve);
}
if (_diapasonParticles != null)
@@ -331,13 +347,23 @@ namespace Unheimlich.Gameplay.Player
if (_currentTargetEmitter != null)
{
_currentTargetEmitter.StopListening();
_currentTargetEmitter.SetConcentrating(false);
_currentTargetEmitter.StopListening(cancelConcentrationFade: false);
_currentTargetEmitter = null;
StartCoroutine(ResetFadingFlagAfterDelay());
}
_canUseDiapason = false;
}
private IEnumerator ResetFadingFlagAfterDelay()
{
_isFadingOutConcentration = true;
yield return new WaitForSeconds(0.5f);
_isFadingOutConcentration = false;
}
/// <summary>
/// Updates cursor position and camera panning during listening mode.
/// Supports both gamepad (look input) and mouse (screen edges).
@@ -492,55 +518,20 @@ namespace Unheimlich.Gameplay.Player
#region If needed
private void AttenuateAmbientSound(bool enable)
{
if (_audioFadeCoroutine != null)
StopCoroutine(_audioFadeCoroutine);
TweenHelper.CancelTween(ref _audioListenerFadeTweenId);
_audioFadeCoroutine = StartCoroutine(FadeAmbientSound(enable));
}
private IEnumerator FadeAmbientSound(bool fadeOut)
{
//To do : Create audio tracks
AudioListener listener = FindFirstObjectByType<AudioListener>();
if (listener == null)
yield break;
return;
float startVolume = AudioListener.volume;
float targetVolume = fadeOut ? _ambientAttenuationAmount : _originalAmbientVolume;
float targetVolume = enable ? _ambientAttenuationAmount : _originalAmbientVolume;
if (!fadeOut)
_originalAmbientVolume = startVolume;
else
if (!enable)
_originalAmbientVolume = AudioListener.volume;
float elapsed = 0f;
while (elapsed < _audioFadeDuration)
{
elapsed += Time.deltaTime;
float t = elapsed / _audioFadeDuration;
AudioListener.volume = Mathf.Lerp(startVolume, targetVolume, t);
yield return null;
}
AudioListener.volume = targetVolume;
_audioListenerFadeTweenId = TweenHelper.FadeVolume(gameObject, targetVolume, _audioFadeDuration);
}
#endregion
private IEnumerator FadeOutAudio(AudioSource source, float duration)
{
float startVolume = source.volume;
float elapsed = 0f;
while (elapsed < duration)
{
elapsed += Time.deltaTime;
float t = elapsed / duration;
source.volume = Mathf.Lerp(startVolume, 0f, t);
yield return null;
}
source.Stop();
source.volume = startVolume;
}
private void EnableVignette(bool enable)
{
@@ -551,23 +542,6 @@ namespace Unheimlich.Gameplay.Player
}
}
private IEnumerator TweenDiapasonPosition(Vector3 targetPosition, float duration, System.Action onComplete = null)
{
Vector3 startPosition = _diapasonObject.transform.localPosition;
float elapsed = 0f;
while (elapsed < duration)
{
elapsed += Time.deltaTime;
float t = _diapasonTweenCurve.Evaluate(elapsed / duration);
_diapasonObject.transform.localPosition = Vector3.Lerp(startPosition, targetPosition, t);
yield return null;
}
_diapasonObject.transform.localPosition = targetPosition;
onComplete?.Invoke();
}
private void OnDrawGizmosSelected()
{
if (_playerCamera != null && IsListening)
@@ -1,22 +1,9 @@
using UnityEngine;
using Unheimlich.Core;
namespace Unheimlich.Gameplay.Player
{
/// <summary>
/// Singleton player reference accessible globally throughout the game.
/// </summary>
public class Player : MonoBehaviour
public class Player : Singleton<Player>
{
public static Player Instance { get; private set; }
private void Awake()
{
if (Instance != null && Instance != this)
{
Destroy(this);
return;
}
Instance = this;
}
}
}
@@ -1,5 +1,6 @@
using UnityEngine;
using UnityEngine.InputSystem;
using Unheimlich.Core;
using Unheimlich.Gameplay.Portals;
using InputManager = Unheimlich.Managers.PlayerInputManager;
@@ -10,12 +11,10 @@ namespace Unheimlich.Gameplay.Player
/// Supports both mouse and gamepad input with adaptive sensitivity.
/// Includes aim assist for dialogue choices and camera shake effects.
/// </summary>
public class PlayerLook : MonoBehaviour
public class PlayerLook : Singleton<PlayerLook>
{
private const float INPUT_DEADZONE = 0.1f;
private const float MOUSE_DELTA_MULTIPLIER = 0.001f;
public static PlayerLook Instance { get; private set; }
[Header("Components")]
[SerializeField] private Transform _playerCamera, _playerCameraPivot;
@@ -46,15 +45,8 @@ namespace Unheimlich.Gameplay.Player
private float _verticalRotation;
private bool _isUsingGamepad = false;
private void Awake()
protected override void OnAwake()
{
if (Instance != null && Instance != this)
{
Destroy(this);
return;
}
Instance = this;
QualitySettings.vSyncCount = 0;
Application.targetFrameRate = _frameRate;
@@ -1,16 +1,12 @@
using UnityEngine;
using Unheimlich.Core;
using Unheimlich.Managers;
namespace Unheimlich.Gameplay.Player
{
/// <summary>
/// Handles player movement, jumping, and gravity using CharacterController.
/// Provides movement state information for other systems like camera head bob.
/// </summary>
[RequireComponent(typeof(CharacterController))]
public class PlayerMovement : MonoBehaviour
public class PlayerMovement : Singleton<PlayerMovement>
{
public static PlayerMovement Instance { get; private set; }
[Header("Settings")]
[SerializeField] private float _moveSpeed = 5f;
@@ -24,15 +20,8 @@ namespace Unheimlich.Gameplay.Player
private float _ySpeed;
private Vector3 _moveDirection;
private void Awake()
protected override void OnAwake()
{
if (Instance != null && Instance != this)
{
Destroy(this);
return;
}
Instance = this;
CharacterController = GetComponent<CharacterController>();
}