59 lines
2.2 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.VFX;
namespace RR.Game2D.Entities
{
[RequireComponent(typeof(Collider2D))]
public class Checkpoint : MonoBehaviour
{
[Tooltip("Уникальное имя в пределах сцены (например CP_01). Если пусто — возьмём имя объекта.")]
public string checkpointId;
[Tooltip("Опционально: куда и как смотреть после респавна.")]
public Vector2 spawnForward = Vector2.right;
[SerializeField, Tooltip("Тег объекта игрока, который активирует чекпоинт")] private string playerTag = "Player";
[Header("FX (необязательно)")]
public GameObject activateVfx;
public AudioClip activateSfx;
private bool _activated;
private Collider2D _col;
void Awake()
{
_col = GetComponent<Collider2D>();
_col.isTrigger = true;
if (String.IsNullOrWhiteSpace(checkpointId))
checkpointId = gameObject.name;
}
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;
activateSfx.GetComponent<VisualEffect>().Play();
}
if (activateSfx) AudioSource.PlayClipAtPoint(activateSfx, transform.position);
}
public Vector3 GetSpawnPosition() => transform.position;
public Vector2 GetForward() => spawnForward.sqrMagnitude > 0.0001f ? spawnForward.normalized : Vector2.right;
// Для визуалки можно сделать метод Highlight(bool on) и менять спрайт/свет
}
}