Files

102 lines
2.0 KiB
C#

using System.Collections;
using UnityEngine;
public class NoteController : MonoBehaviour
{
private float speed;
private bool isMoving = true;
private bool isInJudgeZone = false; // 判定区域标记
private ParticleSystem hitEffect;
private Note linkedNote;
private void Awake()
{
hitEffect = GetComponentInChildren<ParticleSystem>();
linkedNote = GetComponent<Note>();
}
private void Update()
{
if (isMoving)
{
transform.Translate(Vector3.down * speed * Time.deltaTime);
}
}
public void SetSpeed(float newSpeed)
{
speed = newSpeed;
}
public void StopMovement()
{
isMoving = false;
}
public void PlayHitEffect()
{
if (hitEffect != null)
{
hitEffect.Play();
}
}
public void ResetState()
{
speed = 0f;
isMoving = true;
isInJudgeZone = false;
if (hitEffect != null)
{
hitEffect.Stop();
hitEffect.Clear();
}
}
// 将 OnTriggerEnter2D 和 OnTriggerExit2D 挂载在 Controller 上
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.CompareTag("JudgmentLine"))
{
if (linkedNote != null)
{
isInJudgeZone = true;
TrackKeyManager.Instance.RegisterKey(linkedNote.GetTrackIndex(), linkedNote.GetKey());
}
}
}
private void OnTriggerExit2D(Collider2D collision)
{
if (collision.CompareTag("JudgmentLine"))
{
if (linkedNote != null)
{
isInJudgeZone = false;
TrackKeyManager.Instance.UnregisterKey(linkedNote.GetTrackIndex(), linkedNote.GetKey());
linkedNote.SetJudgeZone(isInJudgeZone);
}
}
}
/// <summary>
/// 供 Note 查询是否处于判定区域
/// </summary>
public bool IsInJudgeZone()
{
return isInJudgeZone;
}
}