Files
ichni_Official/Assets/Scripts/Game/GameElements/Notes/NoteObjects/Flick.cs
SoulliesOfficial 64c84ac685 更新
2025-09-06 21:58:48 -04:00

253 lines
9.0 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 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<Vector2> availableFlickDirections;
public float flickBuffer = 0.5f;
public static Flick GenerateElement(string elementName, Guid id, List<string> tags, bool isFirstGenerated,
GameElement parentElement, float exactJudgeTime, List<Vector2> directions)
{
Flick flick = Instantiate(GameManager.instance.basePrefabs.flickNote, parentElement.transform).GetComponent<Flick>();
flick.Initialize(elementName, id, tags, isFirstGenerated, parentElement);
flick.exactJudgeTime = exactJudgeTime;
//flick.availableFlickDirections = new List<Vector2>() { 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<SplinePositioner>();
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.noteJudgeManager.checkingFlickList.Contains(this))
{
isDuringJudging = true;
GameManager.instance.noteJudgeManager.checkingFlickList.Add(this);
}
base.Update();
ExecuteFinalJudge(songTime);
}
protected override void RemoveFromCheckingList()
{
if (GameManager.instance.noteJudgeManager.checkingFlickList.Contains(this))
{
GameManager.instance.noteJudgeManager.checkingFlickList.Remove(this);
}
}
public override void ExecuteStartJudge()
{
float triggerTime = GameManager.instance.songTime;
float timeDifference = triggerTime - exactJudgeTime;
NoteJudgeType startJudgeType = GetStartJudgeType(timeDifference);
RemoveFromCheckingList();
preJudgeType = startJudgeType;
isFirstJudged = true;
}
public void ExecuteFinalJudge(float triggerTime)
{
if (isFirstJudged && !isFinalJudged && preJudgeType != NoteJudgeType.NotJudged &&
GameManager.instance.songTime >= exactJudgeTime)
{
if (preJudgeType == NoteJudgeType.Perfect)
{
Perfect(triggerTime);
GameManager.instance.playingRecorder.resultData.Add(0);
}
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 void ExecuteTapJudge()
{
RemoveFromCheckingList();
float triggerTime = GameManager.instance.songTime;
float timeDifference = triggerTime - exactJudgeTime;
NoteJudgeType startJudgeType = GetStartJudgeType(timeDifference);
if (startJudgeType == NoteJudgeType.Perfect)
{
Perfect(triggerTime);
GameManager.instance.playingRecorder.resultData.Add(0);
}
else if (startJudgeType == NoteJudgeType.Good)
{
Good(triggerTime);
}
else if (startJudgeType == NoteJudgeType.Bad)
{
Bad(triggerTime);
}
else if (startJudgeType == NoteJudgeType.Miss)
{
Miss(triggerTime);
}
if (startJudgeType != NoteJudgeType.Miss)
{
noteAudioSubmodule.PlayGeneralJudgeAudios();
}
isFirstJudged = true;
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.cam;
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<Vector2> availableFlickDirections;
public Flick_BM()
{
}
public Flick_BM(string elementName, Guid elementGuid, List<string> tags, GameElement_BM attachedElement,
float exactJudgeTime,
List<Vector2> directions) : base(elementName, elementGuid, tags, attachedElement, exactJudgeTime)
{
availableFlickDirections = directions;
}
public override void ExecuteBM()
{
matchedElement = Flick.GenerateElement(elementName, elementGuid, tags, false,
GetElement(attachedElementGuid), exactJudgeTime, availableFlickDirections);
}
}
}
}