-Add LevelSpecificPlayerSFX prefabs for levels 1-1 and 1-2 -Configure checkpoints to activate sound effects, including pitch, volume, and audio editing -Rename the checkpoint effect object to Checkpoint VFX -Update the audio manager, the default value of the music target volume is 0.2 -Add SoundEffect structure to support sound parameter configuration -Optimize audio source creation and crossfade logic -Remove the old music control components and use the new audio management method
75 lines
2.6 KiB
C#
75 lines
2.6 KiB
C#
using System;
|
|
using UnityEngine;
|
|
using UnityEngine.VFX;
|
|
|
|
using RR.Common.Audio;
|
|
using RR.Game2D.Core.Managers;
|
|
using Unity.VisualScripting;
|
|
|
|
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<Collider2D>();
|
|
_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) и менять спрайт/свет
|
|
}
|
|
} |