using System.Collections; using UnityEngine; using RR.Game2D.Behaviour; using RR.Game2D.Behaviour.ScriptableObjects; using RR.Game2D.Core.Managers; using RR.Game2D.FX; using UnityEngine.VFX; namespace RR.Game2D.Animations { public class EnemyAnimationEvents : MonoBehaviour, CreatureAnimationEvents { [SerializeField] private VisualEffectAsset deathVFX; [SerializeField] private Vector3 deathVFXOffset = Vector3.zero; private SFXAsset _stepSounds; private SFXAsset _hurtSounds; private Vector3 _vfxSpawnPoint; private bool _deathScoreRegistered; private Vector3 _creatureSize = Vector3.one; private void Awake() { // Загружаем и кэшируем звуковые эффекты из контроллера врага UpdateSFX(); Collider2D col = GetComponent(); if (col) _creatureSize = col.bounds.size; // Учитываем поворот объекта при вычислении смещения Vector3 rotatedOffset = transform.rotation * deathVFXOffset; _vfxSpawnPoint = (col ? col.bounds.center : transform.position) + rotatedOffset; } public void UpdateSFX() { // Кэшируем ссылки на звуковые эффекты из контроллера врага EnemyController controller = GetComponent(); if (controller != null && controller.sfxAsset != null) { _stepSounds = controller.sfxAsset.stepSounds; _hurtSounds = controller.sfxAsset.hurtSounds; } } public void OnDeath() {} public void OnJump() {} public void OnDeathBegin() { // Проигрываем звук получения урона if (_hurtSounds && _hurtSounds.soundEffects.Length > 0) GameManager.Instance.Audio.PlayRandomSfxAt(_hurtSounds.soundEffects, transform.position); // Переводим врага на слой "No Collision" gameObject.layer = LayerMask.NameToLayer("No Collision"); // Получаем компоненты Rigidbody2D rb = GetComponent(); Collider2D col = GetComponent(); EnemyController controller = GetComponent(); EnemyFireScript fireScript = GetComponent(); // Отключаем скрипты и коллайдер if (controller) controller.enabled = false; if (fireScript) fireScript.enabled = false; if (col) { // Учитываем поворот объекта при вычислении смещения Vector3 rotatedOffset = transform.rotation * deathVFXOffset; _vfxSpawnPoint = col.bounds.center + rotatedOffset; col.enabled = false; } if (!rb) return; // Останавливаем физику rb.linearVelocity = Vector2.zero; rb.bodyType = RigidbodyType2D.Kinematic; } public void OnDeathEnd() { if (!_deathScoreRegistered) { ScoreSystem.Instance?.RegisterEnemyKilled(); _deathScoreRegistered = true; } // Создаем визуальный эффект смерти if (deathVFX) { MvpVFXSpawnSettings spawnSettings = new MvpVFXSpawnSettings { useScale3D = true, scale3D = _creatureSize * 0.5f, rotation = transform.rotation }; MvpVFXUtility.Spawn(deathVFX, null, _vfxSpawnPoint, 1f, spawnSettings); } // Уничтожаем врага Destroy(gameObject); } public void OnStep() { // Проигрываем звук шага if (_stepSounds && _stepSounds.soundEffects.Length > 0) GameManager.Instance.Audio.PlayRandomSfxAt(_stepSounds.soundEffects, transform.position); } } }