using System; using UnityEngine; using UnityEngine.VFX; using RR.Common.Audio; using RR.Game2D.Core.Managers; namespace RR.Game2D.Entities { [RequireComponent(typeof(Collider2D))] public class Checkpoint : MonoBehaviour { public string checkpointId; public Vector2 spawnForward = Vector2.right; [SerializeField] private string playerTag = "Player"; [Header("FX (необязательно)")] public VisualEffect activateVfx; public SoundEffect activateSfx; private bool _activated; private Collider2D _col; private void Awake() { _col = GetComponent(); _col.isTrigger = true; if (String.IsNullOrWhiteSpace(checkpointId)) checkpointId = gameObject.name; if (!activateVfx.enabled) activateVfx.enabled = true; activateVfx.Stop(); } private void OnTriggerEnter2D(Collider2D other) { if (_activated) return; // Определяем объект-источник (учтём Rigidbody, если он есть) var source = other.attachedRigidbody ? other.attachedRigidbody.gameObject : other.gameObject; if (!source.CompareTag(playerTag)) return; _activated = true; if (CheckpointSystem.Instance) CheckpointSystem.Instance.SetActive(this); if (activateVfx) { activateVfx.transform.position = _col.bounds.center; activateVfx.Play(); } if (activateSfx.sound != null) GameManager.Instance.Audio.PlaySfxAt(activateSfx.sound, _col.bounds.center, activateSfx.volume, activateSfx.pitch); } private void OnTriggerExit2D(Collider2D other) { var source = other.attachedRigidbody ? other.attachedRigidbody.gameObject : other.gameObject; if (!source.CompareTag(playerTag)) return; if (activateVfx) activateVfx.Stop(); } public Vector3 GetSpawnPosition() => transform.position; public Vector2 GetForward() => spawnForward.sqrMagnitude > 0.0001f ? spawnForward.normalized : Vector2.right; // Для визуалки можно сделать метод Highlight(bool on) и менять спрайт/свет } }