Files
ichni_Official/Assets/Scripts/Game/GameElements/Notes/NoteObjects/Flick.cs

178 lines
6.5 KiB
C#
Raw Normal View History

2025-06-03 02:42:28 -04:00
using System;
using System.Collections;
using System.Collections.Generic;
2025-07-21 05:42:20 -04:00
using System.Linq;
2025-06-03 02:42:28 -04:00
using Dreamteck.Splines;
using Ichni.RhythmGame.Beatmap;
using Lean.Pool;
2025-07-21 05:42:20 -04:00
using UniRx;
2025-06-03 02:42:28 -04:00
using Unity.VisualScripting;
using UnityEngine;
namespace Ichni.RhythmGame
{
public partial class Flick : NoteBase
{
public List<Vector2> availableFlickDirections;
2025-07-21 05:42:20 -04:00
public float flickBuffer = 0.5f;
2025-06-03 02:42:28 -04:00
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;
2025-07-21 05:42:20 -04:00
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);
2025-06-03 02:42:28 -04:00
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;
}
2025-07-21 05:42:20 -04:00
2025-06-03 02:42:28 -04:00
return flick;
}
2025-07-21 05:42:20 -04:00
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();
}
protected override void RemoveFromCheckingList()
{
GameManager.instance.inputManager.checkingFlickList.Remove(this);
}
public override void ExecuteStartJudge()
{
base.ExecuteStartJudge();
isFinalJudged = true;
if (GameManager.instance.inputManager.checkingFlickList.Contains(this))
{
GameManager.instance.inputManager.checkingFlickList.Remove(this);
}
}
}
public partial class Flick
{
public bool CheckJudgeAvailability(InputUnitSwipe inputUnitSwipe)
2025-06-03 02:42:28 -04:00
{
2025-07-21 05:42:20 -04:00
return noteJudgeSubmodule.judgeUnitList.All(judgeUnit => judgeUnit.CheckJudgeAvailability(inputUnitSwipe));
}
public bool CheckSwipeDirection(Vector2 screenSwipeDirection)
{
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(screenSwipeDirection, noteScreenDirection);
// 4. 检查点积是否满足阈值
if (dotProduct >= flickBuffer)
{
// 匹配成功!无需再检查其他方向。
Debug.Log($"匹配成功! 输入方向 {screenSwipeDirection} 匹配了本地方向 {localDir} (屏幕投影: {noteScreenDirection}), 点积: {dotProduct}");
return true;
}
}
2025-06-03 02:42:28 -04:00
2025-07-21 05:42:20 -04:00
Debug.Log($"匹配失败. 输入方向 {screenSwipeDirection} 未匹配任何允许的方向。");
return false;
2025-06-03 02:42:28 -04:00
}
}
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);
}
public override GameElement DuplicateBM(GameElement parent)
{
return Flick.GenerateElement(elementName, Guid.NewGuid(), tags, false, parent,
exactJudgeTime, availableFlickDirections);
}
}
}
}