Rhytm minigame base
This commit is contained in:
1 parent
258900d1d6
commit
329cc98ade
7696 files changed
+238891
-619017
No files matched your search
@@ -0,0 +1,416 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using Code.Game.Minigames.MusicGame.UI;
|
||||
using Code.Infrastructure.Audio;
|
||||
using Code.Infrastructure.Input;
|
||||
using Code.Infrastructure.Utils;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using Cysharp.Threading.Tasks.Triggers;
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
|
||||
namespace Code.Game.Minigames.MusicGame
|
||||
{
|
||||
public class BeatManager : IDisposable
|
||||
{
|
||||
private bool _isMounted;
|
||||
|
||||
private int _leadTimeSamples;
|
||||
private int _hitWindowSamples;
|
||||
|
||||
private AudioSource _beatSource;
|
||||
|
||||
private int _combo;
|
||||
private int _currentEnemyHealth, _maxEnemyHealth;
|
||||
private int _currentPlayerHealth;
|
||||
|
||||
private MusicMinigameTrackType _currentTrackType;
|
||||
private BeatNode[] _currentTrackBeats;
|
||||
|
||||
private Vector2 _beatNodeSize;
|
||||
|
||||
private readonly Dictionary<int, Queue<BeatNode>> _poppedOutBeats = new();
|
||||
private readonly Dictionary<BeatNode, Transform> _spawnedBeats = new();
|
||||
private readonly Dictionary<BeatType,UltraGenericPool<Transform>> _pools;
|
||||
|
||||
private int _nextBeatIndex;
|
||||
|
||||
private readonly Dictionary<MusicMinigameTrackType, MusicMinigameTrackConfig> _configs;
|
||||
private readonly IAudioSourcePool _audioSourcePool;
|
||||
private readonly MusicMinigamePanel _minigamePanel;
|
||||
private readonly MusicMinigameConfig _minigameConfig;
|
||||
private readonly IInputService _inputService;
|
||||
|
||||
private IDisposable _minigameDisposable;
|
||||
private CancellationTokenSource _minigameCancellationTokenSource;
|
||||
|
||||
public BeatManager(
|
||||
MultipleConfig<MusicMinigameTrackConfig> configs,
|
||||
IAudioSourcePool audioSourcePool,
|
||||
MusicMinigamePanel minigamePanel,
|
||||
SingleConfig<MusicMinigameConfig> minigameConfig,
|
||||
IInputService inputService)
|
||||
{
|
||||
_inputService = inputService;
|
||||
_minigameConfig = minigameConfig.Config;
|
||||
_minigamePanel = minigamePanel;
|
||||
_audioSourcePool = audioSourcePool;
|
||||
|
||||
_beatSource = audioSourcePool.GetSource(AudioSourceType.Full3D);
|
||||
_configs = configs.Configs.ToDictionary(x => x.Type);
|
||||
|
||||
_pools = new();
|
||||
|
||||
foreach (var (beatType, prefab) in _minigameConfig.beatPrefabs)
|
||||
{
|
||||
_pools[beatType] = new();
|
||||
_pools[beatType].Prefab = prefab;
|
||||
|
||||
Debug.Log($"Created pool {beatType}");
|
||||
}
|
||||
|
||||
_isMounted = false;
|
||||
}
|
||||
|
||||
public void Mount(int maxPlayerHealth)
|
||||
{
|
||||
if (_isMounted) return;
|
||||
|
||||
_combo = 0;
|
||||
_nextBeatIndex = 0;
|
||||
_currentPlayerHealth = maxPlayerHealth;
|
||||
|
||||
_inputService.BindActionPerformed((int)ActionMappings.MusicMinigame, (int)MisucMinigameActions.AttackLeft, AttackingHitLeft);
|
||||
_inputService.BindActionPerformed((int)ActionMappings.MusicMinigame, (int)MisucMinigameActions.AttackUp, AttackingHitUp);
|
||||
_inputService.BindActionPerformed((int)ActionMappings.MusicMinigame, (int)MisucMinigameActions.AttackDown, AttackingHitDown);
|
||||
_inputService.BindActionPerformed((int)ActionMappings.MusicMinigame, (int)MisucMinigameActions.AttackRight, AttackingHitRight);
|
||||
|
||||
_inputService.BindActionPerformed((int)ActionMappings.MusicMinigame, (int)MisucMinigameActions.DefendLeft, DefendingHitLeft);
|
||||
_inputService.BindActionPerformed((int)ActionMappings.MusicMinigame, (int)MisucMinigameActions.DefendUp, DefendingHitUp);
|
||||
_inputService.BindActionPerformed((int)ActionMappings.MusicMinigame, (int)MisucMinigameActions.DefendDown, DefendingHitDown);
|
||||
_inputService.BindActionPerformed((int)ActionMappings.MusicMinigame, (int)MisucMinigameActions.DefendRight, DefendingHitRight);
|
||||
|
||||
_isMounted = true;
|
||||
}
|
||||
|
||||
public void Unmount()
|
||||
{
|
||||
if (!_isMounted) return;
|
||||
|
||||
StopPlayInternal();
|
||||
|
||||
_combo = 0;
|
||||
_currentPlayerHealth = 0;
|
||||
_maxEnemyHealth = 0;
|
||||
_currentEnemyHealth = 0;
|
||||
_nextBeatIndex = 0;
|
||||
|
||||
_inputService.UnbindActionPerformed((int)ActionMappings.MusicMinigame, (int)MisucMinigameActions.AttackLeft, AttackingHitLeft);
|
||||
_inputService.UnbindActionPerformed((int)ActionMappings.MusicMinigame, (int)MisucMinigameActions.AttackUp, AttackingHitUp);
|
||||
_inputService.UnbindActionPerformed((int)ActionMappings.MusicMinigame, (int)MisucMinigameActions.AttackDown, AttackingHitDown);
|
||||
_inputService.UnbindActionPerformed((int)ActionMappings.MusicMinigame, (int)MisucMinigameActions.AttackRight, AttackingHitRight);
|
||||
|
||||
_inputService.UnbindActionPerformed((int)ActionMappings.MusicMinigame, (int)MisucMinigameActions.DefendLeft, DefendingHitLeft);
|
||||
_inputService.UnbindActionPerformed((int)ActionMappings.MusicMinigame, (int)MisucMinigameActions.DefendUp, DefendingHitUp);
|
||||
_inputService.UnbindActionPerformed((int)ActionMappings.MusicMinigame, (int)MisucMinigameActions.DefendDown, DefendingHitDown);
|
||||
_inputService.UnbindActionPerformed((int)ActionMappings.MusicMinigame, (int)MisucMinigameActions.DefendRight, DefendingHitRight);
|
||||
|
||||
_isMounted = false;
|
||||
}
|
||||
|
||||
private void AttackingHitLeft(InputAction.CallbackContext obj) => TryHitLane(0,true);
|
||||
private void AttackingHitUp(InputAction.CallbackContext obj) => TryHitLane(1,true);
|
||||
private void AttackingHitDown(InputAction.CallbackContext obj) => TryHitLane(2, true);
|
||||
private void AttackingHitRight(InputAction.CallbackContext obj) => TryHitLane(3, true);
|
||||
|
||||
private void DefendingHitLeft(InputAction.CallbackContext obj) => TryHitLane(0,false);
|
||||
private void DefendingHitUp(InputAction.CallbackContext obj) => TryHitLane(1,false);
|
||||
private void DefendingHitDown(InputAction.CallbackContext obj) => TryHitLane(2, false);
|
||||
private void DefendingHitRight(InputAction.CallbackContext obj) => TryHitLane(3, false);
|
||||
|
||||
private void TryHitLane(int laneIndex, bool isAttacking)
|
||||
{
|
||||
if (!_poppedOutBeats.TryGetValue(laneIndex, out var queue) || queue.Count == 0) return;
|
||||
|
||||
ResolveBeat(queue, isTimeoutMiss: false, attackingHit: isAttacking);
|
||||
}
|
||||
|
||||
public void StartPlay(MusicMinigameTrackType trackType)
|
||||
{
|
||||
if (!_isMounted) throw new InvalidOperationException("Music minigame is not mounted");
|
||||
|
||||
StopPlayInternal();
|
||||
|
||||
_minigameCancellationTokenSource = new CancellationTokenSource();
|
||||
_currentTrackType = trackType;
|
||||
|
||||
var trackConfig = _configs[_currentTrackType];
|
||||
|
||||
_currentTrackBeats = BitwigMidiUtility.ParseMidi(
|
||||
trackConfig.midiFile.bytes,
|
||||
trackConfig.Clip.frequency,
|
||||
trackConfig.bpm,
|
||||
40,
|
||||
36,
|
||||
4);
|
||||
|
||||
Debug.Log($"Read {trackConfig.midiFile.bytes} bytes got {_currentTrackBeats.Length} beats");
|
||||
|
||||
_leadTimeSamples = trackConfig.leadTimeSamples;
|
||||
_hitWindowSamples = trackConfig.hitWindowSamples;
|
||||
|
||||
var beatLine = _minigamePanel.BeatLines.FirstOrDefault();
|
||||
|
||||
// nullpointer Бомба
|
||||
var beatLineVector = beatLine.StartPosition - beatLine.SpotPosition;
|
||||
|
||||
float nodeBoxSize = (_hitWindowSamples / (float) _leadTimeSamples) * beatLineVector.magnitude;
|
||||
|
||||
_minigamePanel.BeatLineZone.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, nodeBoxSize);
|
||||
|
||||
Vector3 beatLineZonePosition = _minigamePanel.BeatLineZone.position;
|
||||
|
||||
beatLineZonePosition.y = beatLine.SpotPosition.y;
|
||||
|
||||
_minigamePanel.BeatLineZone.position = beatLineZonePosition;
|
||||
|
||||
_beatNodeSize = new(75, nodeBoxSize);
|
||||
|
||||
// ((RectTransform)prefab).SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, );
|
||||
|
||||
_maxEnemyHealth = trackConfig.maxEnemyHealth;
|
||||
_currentEnemyHealth = _maxEnemyHealth;
|
||||
_nextBeatIndex = 0;
|
||||
|
||||
_minigamePanel.EnemyHealth.SetEnemyHealth(_currentEnemyHealth/(float)_maxEnemyHealth);
|
||||
|
||||
_minigamePanel.PlayerHealth.SetHealth(_currentPlayerHealth);
|
||||
|
||||
_minigamePanel.MusicMinigameComboText.SetComboText(_combo).Forget();
|
||||
|
||||
_beatSource.clip = trackConfig.Clip;
|
||||
_beatSource.Play();
|
||||
|
||||
PollBeatsAsync(_minigameCancellationTokenSource.Token).Forget();
|
||||
}
|
||||
|
||||
private async UniTaskVoid PollBeatsAsync(CancellationToken ct)
|
||||
{
|
||||
while (!ct.IsCancellationRequested)
|
||||
{
|
||||
bool isCancelled = await UniTask.Yield(PlayerLoopTiming.Update, ct).SuppressCancellationThrow();
|
||||
if (isCancelled) return;
|
||||
|
||||
if (!_beatSource.isPlaying) continue;
|
||||
|
||||
int currentSample = _beatSource.timeSamples;
|
||||
|
||||
while (BeatTimelineUtility.TryPopNextBeat(
|
||||
_currentTrackBeats,
|
||||
currentSample,
|
||||
_leadTimeSamples,
|
||||
ref _nextBeatIndex,
|
||||
out var nextBeat))
|
||||
{
|
||||
SpawnBeat(nextBeat);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void PollBeats()
|
||||
{
|
||||
int currentSample = _beatSource.timeSamples;
|
||||
|
||||
while (BeatTimelineUtility.TryPopNextBeat(
|
||||
_currentTrackBeats,
|
||||
currentSample,
|
||||
_leadTimeSamples,
|
||||
ref _nextBeatIndex,
|
||||
out var nextBeat))
|
||||
{
|
||||
SpawnBeat(nextBeat);
|
||||
}
|
||||
}
|
||||
|
||||
private void SpawnBeat(BeatNode nextBeat)
|
||||
{
|
||||
if (!_poppedOutBeats.TryGetValue(nextBeat.laneIndex, out var beatQueue))
|
||||
{
|
||||
beatQueue = new Queue<BeatNode>();
|
||||
_poppedOutBeats[nextBeat.laneIndex] = beatQueue;
|
||||
}
|
||||
|
||||
beatQueue.Enqueue(nextBeat);
|
||||
|
||||
var beatLine = _minigamePanel.BeatLines[nextBeat.laneIndex];
|
||||
var beat = _pools[nextBeat.type].Get();
|
||||
|
||||
beat.SetParent(beatLine.transform, false);
|
||||
beat.position = beatLine.StartPosition;
|
||||
|
||||
((RectTransform)beat).sizeDelta = _beatNodeSize;
|
||||
|
||||
_spawnedBeats[nextBeat] = beat;
|
||||
|
||||
RunBeat(nextBeat, beat, beatLine, _minigameCancellationTokenSource.Token).Forget();
|
||||
}
|
||||
|
||||
private async UniTaskVoid RunBeat(
|
||||
BeatNode node,
|
||||
Transform beatTransform,
|
||||
BeatLine beatLine,
|
||||
CancellationToken ct)
|
||||
{
|
||||
int missSample = node.sampleIndex + _hitWindowSamples;// / 2;
|
||||
|
||||
while (!ct.IsCancellationRequested && _beatSource.timeSamples <= missSample)
|
||||
{
|
||||
bool isCancelled = await UniTask.Yield(PlayerLoopTiming.Update, ct).SuppressCancellationThrow();
|
||||
if (isCancelled || !beatTransform) return;
|
||||
|
||||
if (!_spawnedBeats.ContainsKey(node)) return;
|
||||
|
||||
float progress = BeatTimelineUtility.GetNormalizedApproachProgress(
|
||||
_beatSource.timeSamples,
|
||||
node.sampleIndex,
|
||||
_leadTimeSamples);
|
||||
|
||||
beatTransform.position = Vector3.LerpUnclamped(
|
||||
beatLine.StartPosition,
|
||||
beatLine.SpotPosition,
|
||||
progress);
|
||||
}
|
||||
|
||||
if (ct.IsCancellationRequested || !_spawnedBeats.ContainsKey(node)) return;
|
||||
|
||||
if (!_poppedOutBeats.TryGetValue(node.laneIndex, out var queue) || queue.Count <= 0) return;
|
||||
|
||||
ResolveBeat(queue, isTimeoutMiss: true, false);
|
||||
}
|
||||
|
||||
private void ResolveBeat(Queue<BeatNode> queue, bool isTimeoutMiss, bool attackingHit)
|
||||
{
|
||||
bool inTimeWindow = !isTimeoutMiss;
|
||||
bool canBeDeleted = true;
|
||||
|
||||
BeatNode node = isTimeoutMiss ? queue.Dequeue() : queue.Peek();
|
||||
|
||||
if (inTimeWindow)
|
||||
{
|
||||
inTimeWindow = BeatTimelineUtility.IsWithinHitWindow(
|
||||
_beatSource.timeSamples,
|
||||
node.sampleIndex,
|
||||
_hitWindowSamples,
|
||||
out int error);
|
||||
|
||||
if (!inTimeWindow && error < 0)
|
||||
{
|
||||
Debug.Log("Cannot be deleted");
|
||||
canBeDeleted = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.Log("Can be deleted");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.Log("Will be deleted");
|
||||
}
|
||||
|
||||
Transform beatTransform = null;
|
||||
|
||||
if (canBeDeleted)
|
||||
if (!_spawnedBeats.Remove(node, out beatTransform)) return;
|
||||
|
||||
bool isCorrectStance = attackingHit
|
||||
? node.type == BeatType.Attack
|
||||
: node.type == BeatType.Defense;
|
||||
|
||||
if (!inTimeWindow || !isCorrectStance) HitMistake(node);
|
||||
else SuccesfulHit(node);
|
||||
|
||||
if(canBeDeleted && beatTransform != null)
|
||||
{
|
||||
if(!isTimeoutMiss) queue.Dequeue();
|
||||
|
||||
_pools[node.type].Release(beatTransform);
|
||||
}
|
||||
}
|
||||
|
||||
private void HitMistake(BeatNode node)
|
||||
{
|
||||
_combo = 0;
|
||||
_minigamePanel.MusicMinigameComboText.SetComboText(_combo).Forget();
|
||||
|
||||
if (node.type == BeatType.Defense)
|
||||
{
|
||||
_currentPlayerHealth--;
|
||||
|
||||
_minigamePanel.PlayerHealth.DecreaseHealth();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void SuccesfulHit(BeatNode node)
|
||||
{
|
||||
_combo++;
|
||||
_minigamePanel.MusicMinigameComboText.SetComboText(_combo).Forget();
|
||||
|
||||
if (node.type == BeatType.Attack)
|
||||
{
|
||||
_currentEnemyHealth = Mathf.Max(0, _currentEnemyHealth - _combo);
|
||||
float healthPercent = _maxEnemyHealth > 0
|
||||
? (float)_currentEnemyHealth / _maxEnemyHealth
|
||||
: 0f;
|
||||
|
||||
_minigamePanel.EnemyHealth.SetEnemyHealth(healthPercent);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void GameOver()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
private void StopPlayInternal()
|
||||
{
|
||||
_minigameDisposable?.Dispose();
|
||||
_minigameDisposable = null;
|
||||
|
||||
if (_minigameCancellationTokenSource != null)
|
||||
{
|
||||
_minigameCancellationTokenSource.Cancel();
|
||||
_minigameCancellationTokenSource.Dispose();
|
||||
_minigameCancellationTokenSource = null;
|
||||
}
|
||||
|
||||
if (_beatSource && _beatSource.isPlaying)
|
||||
_beatSource.Stop();
|
||||
|
||||
foreach (var (node, beat) in _spawnedBeats)
|
||||
{
|
||||
_pools[node.type].Release(beat);
|
||||
}
|
||||
|
||||
_spawnedBeats.Clear();
|
||||
_poppedOutBeats.Clear();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Unmount();
|
||||
|
||||
foreach (var (_, pool) in _pools)
|
||||
{
|
||||
pool.Clear();
|
||||
}
|
||||
|
||||
_pools.Clear();
|
||||
_audioSourcePool.ReleaseSource(_beatSource);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user