using System; using System.Collections; using System.Collections.Generic; using System.Linq; using Dreamteck.Splines; using Ichni.RhythmGame.Beatmap; using Lean.Pool; using UniRx; using Unity.VisualScripting; using UnityEngine; namespace Ichni.RhythmGame { public partial class Flick : NoteBase { public NoteJudgeType preJudgeType; public List availableFlickDirections; public float flickBuffer = 0.5f; public static Flick GenerateElement(string elementName, Guid id, List tags, bool isFirstGenerated, GameElement parentElement, float exactJudgeTime, List directions) { Flick flick = Instantiate(GameManager.instance.basePrefabs.flickNote, parentElement.transform).GetComponent(); flick.Initialize(elementName, id, tags, isFirstGenerated, parentElement); flick.exactJudgeTime = exactJudgeTime; flick.availableFlickDirections = new List() { Vector2.left, Vector2.right }; flick.judgeIntervals = new NoteJudgeIntervals( new TimeInterval(-0.15f, -0.15f), new TimeInterval(-0.15f, -0.15f), new TimeInterval(-0.15f, -0.15f), new TimeInterval(-0.15f, 0.15f), new TimeInterval(0.15f, 0.15f), new TimeInterval(0.15f, 0.15f), 0.15f); if (parentElement.TryGetComponent(out Track track)) { if (track.trackTimeSubmodule != null) { flick.track = track; flick.trackPositioner = flick.AddComponent(); flick.trackPositioner.spline = track.trackPathSubmodule.path; flick.isOnTrack = true; flick.UpdateNoteInTrack(); } else { throw new System.Exception("如果Note要生成在Track上,Track必须有TrackTimeSubmodule组件。"); } } else { flick.track = null; flick.isOnTrack = false; } return flick; } protected override void Update() { float songTime = GameManager.instance.songTime; if (!isFirstJudged && !isDuringJudging && songTime >= exactJudgeTime + judgeIntervals.beforeMiss.intervalStart && !GameManager.instance.inputManager.checkingFlickList.Contains(this)) { isDuringJudging = true; GameManager.instance.inputManager.checkingFlickList.Add(this); } base.Update(); ExecuteFinalJudge(songTime); } protected override void RemoveFromCheckingList() { if (GameManager.instance.inputManager.checkingFlickList.Contains(this)) { GameManager.instance.inputManager.checkingFlickList.Remove(this); } } public override void ExecuteStartJudge() { float triggerTime = GameManager.instance.songTime; float timeDifference = triggerTime - exactJudgeTime; NoteJudgeType startJudgeType = GetStartJudgeType(timeDifference); if (startJudgeType != NoteJudgeType.Perfect) { return; } preJudgeType = startJudgeType; isFirstJudged = true; RemoveFromCheckingList(); } public void ExecuteFinalJudge(float triggerTime) { if (isFirstJudged && !isFinalJudged && preJudgeType != NoteJudgeType.NotJudged && GameManager.instance.songTime >= exactJudgeTime) { if (preJudgeType == NoteJudgeType.Perfect) { Perfect(triggerTime); } else if (preJudgeType == NoteJudgeType.Good) { Good(triggerTime); } else if (preJudgeType == NoteJudgeType.Bad) { Bad(triggerTime); } else if (preJudgeType == NoteJudgeType.Miss) { Miss(triggerTime); } if (preJudgeType != NoteJudgeType.Miss) { noteAudioSubmodule.PlayGeneralJudgeAudios(); } isFinalJudged = true; } } } public partial class Flick { public bool CheckJudgeAvailability(InputUnitSwipe inputUnitSwipe) { return noteJudgeSubmodule.judgeUnitList.All(judgeUnit => judgeUnit.CheckJudgeAvailability(inputUnitSwipe)); } public bool CheckSwipeDirection(InputUnitSwipe inputUnitSwipe) { if (inputUnitSwipe.isGeneric) { Debug.Log($"输入方向 {inputUnitSwipe.swipeDirection} 是通用的,直接匹配成功。"); return true; } Camera gameCamera = GameManager.instance.cameraManager.gameCamera.gameCamera; foreach (Vector2 localDir in availableFlickDirections) { Vector3 worldDirection = noteVisual.transform.TransformDirection(localDir.normalized); Vector3 noteOriginWorld = noteVisual.transform.position; Vector3 noteTargetWorld = noteOriginWorld + worldDirection; Vector3 screenOrigin = gameCamera.WorldToScreenPoint(noteOriginWorld); Vector3 screenTarget = gameCamera.WorldToScreenPoint(noteTargetWorld); Vector2 noteScreenDirection = new Vector2(screenTarget.x - screenOrigin.x, screenTarget.y - screenOrigin.y).normalized; if (noteScreenDirection.sqrMagnitude < 0.01f) { continue; } float dotProduct = Vector2.Dot(inputUnitSwipe.swipeDirection, noteScreenDirection); // 4. 检查点积是否满足阈值 if (dotProduct >= flickBuffer) { // 匹配成功!无需再检查其他方向。 Debug.Log($"匹配成功! 输入方向 {inputUnitSwipe.swipeDirection} 匹配了本地方向 {localDir} (屏幕投影: {noteScreenDirection}), 点积: {dotProduct}"); return true; } } Debug.Log($"匹配失败. 输入方向 {inputUnitSwipe.swipeDirection} 未匹配任何允许的方向。"); return false; } } public partial class Flick { public override void SetDefaultSubmodules() { base.SetDefaultSubmodules(); noteAudioSubmodule = new NoteAudioSubmodule(this, "DefaultStay"); } public override void SaveBM() { matchedBM = new Flick_BM(elementName, elementGuid, tags, parentElement.matchedBM as GameElement_BM, exactJudgeTime, availableFlickDirections); } } namespace Beatmap { public class Flick_BM : NoteBase_BM { public List availableFlickDirections; public Flick_BM() { } public Flick_BM(string elementName, Guid elementGuid, List tags, GameElement_BM attachedElement, float exactJudgeTime, List directions) : base(elementName, elementGuid, tags, attachedElement, exactJudgeTime) { availableFlickDirections = directions; } public override void ExecuteBM() { matchedElement = Flick.GenerateElement(elementName, elementGuid, tags, false, GetElement(attachedElementGuid), exactJudgeTime, availableFlickDirections); } public override GameElement DuplicateBM(GameElement parent) { return Flick.GenerateElement(elementName, Guid.NewGuid(), tags, false, parent, exactJudgeTime, availableFlickDirections); } } } }