using UnityEngine; using RR.Common.Audio; using RR.Game2D.Core.Managers; using RR.Game2D.Behaviour; using RR.Game2D.Behaviour.ScriptableObjects; using UnityEngine.VFX; namespace RR.Game2D.Animations { public class PlayerAnimationEvents : MonoBehaviour, CreatureAnimationEvents { // --- VFX Assets --- [SerializeField] private VisualEffectAsset deathVFX; [SerializeField] private Vector3 deathVFXOffset = Vector3.zero; [SerializeField] private VisualEffectAsset runVFX; [SerializeField] private Vector3 runVFXOffset = Vector3.zero; // --- Private Fields --- // SFX Assets private SFXAsset _stepSounds; private SFXAsset _hurtSounds; private SFXAsset _jumpSounds; // Components private Rigidbody2D _rb; private Collider2D _col; private PlayerController _controller; // VFX Spawn Points private Vector3 _deathVFXSpawnPoint; private Vector3 _runVFXSpawnPoint; private void OnEnable() => LevelPlayerSFX.Instance.SFXChanged += UpdateSFX; private void OnDisable() => LevelPlayerSFX.Instance.SFXChanged -= UpdateSFX; private void Awake() { _controller = GetComponent(); UpdateSFX(); _rb = GetComponent(); _col = GetComponent(); // Учитываем поворот объекта при вычислении смещения Vector3 deathRotatedOffset = transform.rotation * deathVFXOffset; Vector3 runRotatedOffset = transform.rotation * runVFXOffset; _deathVFXSpawnPoint = (_col ? _col.bounds.center : transform.position) + deathRotatedOffset; _runVFXSpawnPoint = (_col ? _col.bounds.center : transform.position) + runRotatedOffset; } public void UpdateSFX() { PlayerController controller = GetComponent(); _stepSounds = controller.stepSounds; _hurtSounds = controller.hurtSounds; _jumpSounds = controller.jumpSounds; } public void OnDeath() {} public void OnDeathBegin() { if (_hurtSounds && _hurtSounds.soundEffects.Length > 0) GameManager.Instance.Audio.PlayRandomSfxAt(_hurtSounds.soundEffects, transform.position); PlayerFireScript fireScript = GetComponent(); if (fireScript.enableFire) fireScript.enableFire = false; if (!_rb) _rb = GetComponent(); _rb.linearVelocity = Vector2.zero; } public void OnDeathEnd() { if (!_col) _col = GetComponent(); Vector3 rotatedOffset = transform.rotation * deathVFXOffset; _deathVFXSpawnPoint = _col ? _col.bounds.center : transform.position + rotatedOffset; if (deathVFX) VFXPool.Instance.Spawn(deathVFX, _deathVFXSpawnPoint, 1f); gameObject.layer = LayerMask.NameToLayer("Default"); // _rb.constraints = RigidbodyConstraints2D.FreezeRotation; _controller.isDeadAtMoment = false; PlayerFireScript fireScript = GetComponent(); if (!fireScript.enableFire) fireScript.enableFire = true; GameManager.Instance.RespawnPlayer(); } public void OnStep() { if (_stepSounds && _stepSounds.soundEffects.Length > 0) GameManager.Instance.Audio.PlayRandomSfxAt(_stepSounds.soundEffects, transform.position); if (!_col) _col = GetComponent(); Vector3 rotatedOffset = transform.rotation * runVFXOffset; _runVFXSpawnPoint = _col ? _col.bounds.center : transform.position + rotatedOffset; if (deathVFX) VFXPool.Instance.Spawn(runVFX, _runVFXSpawnPoint, 1f); } public void OnJump() { if (_jumpSounds && _jumpSounds.soundEffects.Length > 0) GameManager.Instance.Audio.PlayRandomSfxAt(_jumpSounds.soundEffects, transform.position); } } }