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,29 @@
|
||||
using System;
|
||||
|
||||
namespace Code.Game.Minigames.MusicGame
|
||||
{
|
||||
|
||||
public enum BeatType
|
||||
{
|
||||
Attack,
|
||||
Defense
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
public struct BeatNode : IEquatable<BeatNode>
|
||||
{
|
||||
public int sampleIndex;
|
||||
public BeatType type;
|
||||
public int laneIndex;
|
||||
|
||||
public bool Equals(BeatNode other)
|
||||
{
|
||||
return sampleIndex == other.sampleIndex && type == other.type && laneIndex == other.laneIndex;
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return HashCode.Combine(sampleIndex, (int)type, laneIndex);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 506777c42d2147028276b91d6e4d4408
|
||||
timeCreated: 1794669849
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a475c060be054ad08b76afd33cdb5d69
|
||||
timeCreated: 1794674475
|
||||
@@ -0,0 +1,10 @@
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Code.Game.Minigames.MusicGame
|
||||
{
|
||||
public class BeatSeems : MonoBehaviour
|
||||
{
|
||||
public TMP_Text beatText;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4f197cd566974606a49ec121033f81ff
|
||||
timeCreated: 1794754948
|
||||
@@ -0,0 +1,95 @@
|
||||
using System;
|
||||
|
||||
namespace Code.Game.Minigames.MusicGame
|
||||
{
|
||||
public static class BeatTimelineUtility
|
||||
{
|
||||
// Перевод секунд (например, времени подлета ноты) в семплы
|
||||
public static int SecondsToSamples(float seconds, int sampleRate)
|
||||
{
|
||||
return (int)Math.Round(seconds * sampleRate);
|
||||
}
|
||||
|
||||
// Извлекает все биты, которым пора заспавниться на текущем семпле.
|
||||
// Вызывается в цикле while в Update, передвигая внешний индекс nextBeatIndex.
|
||||
public static bool TryPopNextBeat(
|
||||
BeatNode[] beatMap,
|
||||
int currentSample,
|
||||
int leadTimeSamples,
|
||||
ref int nextBeatIndex,
|
||||
out BeatNode beat)
|
||||
{
|
||||
if (beatMap != null &&
|
||||
nextBeatIndex < beatMap.Length &&
|
||||
(nextBeatIndex == 0
|
||||
|| currentSample >= beatMap[nextBeatIndex].sampleIndex - leadTimeSamples))
|
||||
{
|
||||
beat = beatMap[nextBeatIndex];
|
||||
nextBeatIndex++;
|
||||
return true;
|
||||
}
|
||||
|
||||
beat = default;
|
||||
return false;
|
||||
}
|
||||
public static bool TryDefineNextBeat(
|
||||
int currentSample,
|
||||
BeatNode[] beatMap,
|
||||
int leadTimeSamples,
|
||||
ref int nextBeatIndex)
|
||||
{
|
||||
if (beatMap != null &&
|
||||
nextBeatIndex < beatMap.Length &&
|
||||
(nextBeatIndex == 0
|
||||
|| currentSample >= beatMap[nextBeatIndex].sampleIndex - leadTimeSamples))
|
||||
{
|
||||
nextBeatIndex++;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Возвращает нормализованный прогресс движения ноты по дорожке:
|
||||
// 0.0f — точка спавна, 1.0f — линия удара, больше 1.0f — нота пролетела за линию удара
|
||||
public static float GetNormalizedApproachProgress(int currentSample, int targetSample, int leadTimeSamples)
|
||||
{
|
||||
if (leadTimeSamples <= 0) return 1f;
|
||||
int spawnSample = targetSample - leadTimeSamples;
|
||||
return (currentSample - spawnSample) / (float)leadTimeSamples;
|
||||
}
|
||||
|
||||
// Проверяет, попал ли игрок в окно тайминга (например, +-50 мс от targetSample)
|
||||
public static bool IsWithinHitWindow(
|
||||
int currentSample,
|
||||
int targetSample,
|
||||
int hitWindowSamples,
|
||||
out int signedSampleError)
|
||||
{
|
||||
signedSampleError = currentSample - targetSample;
|
||||
int absError = signedSampleError < 0 ? -signedSampleError : signedSampleError;
|
||||
return absError <= hitWindowSamples;
|
||||
}
|
||||
|
||||
// Принудительный поиск индекса ближайшего будущего бита (полезно при перемотке трека)
|
||||
public static int FindBeatIndexAtSample(BeatNode[] beatMap, int currentSample, int leadTimeSamples)
|
||||
{
|
||||
if (beatMap == null || beatMap.Length == 0) return 0;
|
||||
|
||||
int targetSpawnThreshold = currentSample + leadTimeSamples;
|
||||
int low = 0;
|
||||
int high = beatMap.Length - 1;
|
||||
|
||||
while (low <= high)
|
||||
{
|
||||
int mid = low + ((high - low) >> 1);
|
||||
if (beatMap[mid].sampleIndex < targetSpawnThreshold)
|
||||
low = mid + 1;
|
||||
else
|
||||
high = mid - 1;
|
||||
}
|
||||
|
||||
return low;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e37c2d1dfb5e432ca576522dbe2312d7
|
||||
timeCreated: 1794670392
|
||||
@@ -0,0 +1,230 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Code.Game.Minigames.MusicGame
|
||||
{
|
||||
public static class BitwigMidiUtility
|
||||
{
|
||||
private struct RawNote
|
||||
{
|
||||
public long tick;
|
||||
public int noteNumber;
|
||||
}
|
||||
|
||||
private struct TempoChange
|
||||
{
|
||||
public long tick;
|
||||
public int microsecondsPerQuarterNote;
|
||||
}
|
||||
|
||||
public static BeatNode[] ParseMidi(
|
||||
byte[] midiBytes,
|
||||
int sampleRate,
|
||||
double fallbackBpm = 120.0,
|
||||
int attackBaseNote = 60,
|
||||
int defenseBaseNote = 72,
|
||||
int maxLanes = 12)
|
||||
{
|
||||
if (midiBytes == null || midiBytes.Length < 14)
|
||||
return Array.Empty<BeatNode>();
|
||||
|
||||
int offset = 0;
|
||||
|
||||
// Проверка заголовка "MThd" (0x4D546864)
|
||||
if (ReadInt32BigEndian(midiBytes, ref offset) != 0x4D546864)
|
||||
throw new ArgumentException("Некорректный заголовок MIDI-файла (ожидался MThd).");
|
||||
|
||||
int headerLength = ReadInt32BigEndian(midiBytes, ref offset);
|
||||
int headerEnd = offset + headerLength;
|
||||
|
||||
ReadUInt16BigEndian(midiBytes, ref offset); // Формат MIDI (0 или 1)
|
||||
int tracksCount = ReadUInt16BigEndian(midiBytes, ref offset);
|
||||
int division = ReadUInt16BigEndian(midiBytes, ref offset);
|
||||
|
||||
if ((division & 0x8000) != 0)
|
||||
throw new NotSupportedException(
|
||||
"SMPTE таймкод не поддерживается, используйте стандартный PPQ в Bitwig.");
|
||||
|
||||
int ticksPerQuarterNote = division;
|
||||
offset = headerEnd;
|
||||
|
||||
List<RawNote> rawNotes = new List<RawNote>(256);
|
||||
List<TempoChange> tempoMap = new List<TempoChange>(8);
|
||||
|
||||
for (int t = 0; t < tracksCount && offset + 8 <= midiBytes.Length; t++)
|
||||
{
|
||||
int chunkId = ReadInt32BigEndian(midiBytes, ref offset);
|
||||
int chunkLength = ReadInt32BigEndian(midiBytes, ref offset);
|
||||
int trackEnd = offset + chunkLength;
|
||||
|
||||
// Пропускаем нестандартные чанки, если это не "MTrk" (0x4D54726B)
|
||||
if (chunkId != 0x4D54726B)
|
||||
{
|
||||
offset = trackEnd;
|
||||
continue;
|
||||
}
|
||||
|
||||
long currentTick = 0;
|
||||
byte runningStatus = 0;
|
||||
|
||||
while (offset < trackEnd)
|
||||
{
|
||||
currentTick += ReadVarLen(midiBytes, ref offset);
|
||||
byte status = midiBytes[offset];
|
||||
|
||||
if (status < 0x80)
|
||||
{
|
||||
status = runningStatus;
|
||||
}
|
||||
else
|
||||
{
|
||||
offset++;
|
||||
if (status < 0xF0)
|
||||
runningStatus = status;
|
||||
}
|
||||
|
||||
int messageType = status & 0xF0;
|
||||
|
||||
if (messageType == 0x90) // Note On
|
||||
{
|
||||
byte note = midiBytes[offset++];
|
||||
byte velocity = midiBytes[offset++];
|
||||
|
||||
if (velocity > 0)
|
||||
{
|
||||
rawNotes.Add(new RawNote { tick = currentTick, noteNumber = note });
|
||||
}
|
||||
}
|
||||
else if (messageType == 0x80 || messageType == 0xA0 || messageType == 0xB0 || messageType == 0xE0)
|
||||
{
|
||||
offset += 2;
|
||||
}
|
||||
else if (messageType == 0xC0 || messageType == 0xD0)
|
||||
{
|
||||
offset += 1;
|
||||
}
|
||||
else if (status == 0xFF) // Meta Event
|
||||
{
|
||||
byte metaType = midiBytes[offset++];
|
||||
int metaLength = (int)ReadVarLen(midiBytes, ref offset);
|
||||
|
||||
// 0x51 = Set Tempo (3 байта: микросекунды на четвертную ноту)
|
||||
if (metaType == 0x51 && metaLength == 3)
|
||||
{
|
||||
int mpqn = (midiBytes[offset] << 16) | (midiBytes[offset + 1] << 8) | midiBytes[offset + 2];
|
||||
tempoMap.Add(new TempoChange { tick = currentTick, microsecondsPerQuarterNote = mpqn });
|
||||
}
|
||||
|
||||
offset += metaLength;
|
||||
}
|
||||
else if (status == 0xF0 || status == 0xF7) // SysEx
|
||||
{
|
||||
int sysexLength = (int)ReadVarLen(midiBytes, ref offset);
|
||||
offset += sysexLength;
|
||||
}
|
||||
}
|
||||
|
||||
offset = trackEnd;
|
||||
}
|
||||
|
||||
tempoMap.Sort((a, b) => a.tick.CompareTo(b.tick));
|
||||
int defaultMpqn = (int)Math.Round(60_000_000.0 / fallbackBpm);
|
||||
|
||||
List<BeatNode> beatNodes = new List<BeatNode>(rawNotes.Count);
|
||||
|
||||
for (int i = 0; i < rawNotes.Count; i++)
|
||||
{
|
||||
RawNote raw = rawNotes[i];
|
||||
BeatType type;
|
||||
int lane;
|
||||
|
||||
if (raw.noteNumber >= attackBaseNote && raw.noteNumber < attackBaseNote + maxLanes)
|
||||
{
|
||||
type = BeatType.Attack;
|
||||
lane = raw.noteNumber - attackBaseNote;
|
||||
}
|
||||
else if (raw.noteNumber >= defenseBaseNote && raw.noteNumber < defenseBaseNote + maxLanes)
|
||||
{
|
||||
type = BeatType.Defense;
|
||||
lane = raw.noteNumber - defenseBaseNote;
|
||||
}
|
||||
else
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
double timeSeconds = TicksToSeconds(raw.tick, ticksPerQuarterNote, tempoMap, defaultMpqn);
|
||||
int sampleIndex = (int)Math.Round(timeSeconds * sampleRate);
|
||||
|
||||
beatNodes.Add(new BeatNode
|
||||
{
|
||||
sampleIndex = sampleIndex,
|
||||
type = type,
|
||||
laneIndex = lane
|
||||
});
|
||||
}
|
||||
|
||||
beatNodes.Sort((a, b) => a.sampleIndex.CompareTo(b.sampleIndex));
|
||||
return beatNodes.ToArray();
|
||||
}
|
||||
|
||||
private static double TicksToSeconds(long targetTick, int ticksPerQuarterNote, List<TempoChange> tempoMap,
|
||||
int defaultMpqn)
|
||||
{
|
||||
double totalSeconds = 0.0;
|
||||
long lastTick = 0;
|
||||
int currentMpqn = defaultMpqn;
|
||||
|
||||
for (int i = 0; i < tempoMap.Count; i++)
|
||||
{
|
||||
TempoChange change = tempoMap[i];
|
||||
if (change.tick >= targetTick)
|
||||
break;
|
||||
|
||||
long deltaTicks = change.tick - lastTick;
|
||||
if (deltaTicks > 0)
|
||||
{
|
||||
totalSeconds += (deltaTicks / (double)ticksPerQuarterNote) * (currentMpqn / 1_000_000.0);
|
||||
lastTick = change.tick;
|
||||
}
|
||||
|
||||
currentMpqn = change.microsecondsPerQuarterNote;
|
||||
}
|
||||
|
||||
long remainingTicks = targetTick - lastTick;
|
||||
if (remainingTicks > 0)
|
||||
{
|
||||
totalSeconds += (remainingTicks / (double)ticksPerQuarterNote) * (currentMpqn / 1_000_000.0);
|
||||
}
|
||||
|
||||
return totalSeconds;
|
||||
}
|
||||
|
||||
private static int ReadInt32BigEndian(byte[] data, ref int offset)
|
||||
{
|
||||
int value = (data[offset] << 24) | (data[offset + 1] << 16) | (data[offset + 2] << 8) | data[offset + 3];
|
||||
offset += 4;
|
||||
return value;
|
||||
}
|
||||
|
||||
private static int ReadUInt16BigEndian(byte[] data, ref int offset)
|
||||
{
|
||||
int value = (data[offset] << 8) | data[offset + 1];
|
||||
offset += 2;
|
||||
return value;
|
||||
}
|
||||
|
||||
private static long ReadVarLen(byte[] data, ref int offset)
|
||||
{
|
||||
long value = 0;
|
||||
byte b;
|
||||
do
|
||||
{
|
||||
b = data[offset++];
|
||||
value = (value << 7) | (uint)(b & 0x7F);
|
||||
} while ((b & 0x80) != 0);
|
||||
|
||||
return value;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 197cdb075ed1495bbcbbccd933f7806c
|
||||
timeCreated: 1794669948
|
||||
@@ -0,0 +1,15 @@
|
||||
using System.Collections.Generic;
|
||||
using Code.Infrastructure.Generators;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Code.Game.Minigames.MusicGame
|
||||
{
|
||||
[Config("Configs/Music Minigame Config")]
|
||||
[CreateAssetMenu(fileName = "MusicMinigameConfig", menuName = "Configs/Music Minigame Config")]
|
||||
public class MusicMinigameConfig : ScriptableObject
|
||||
{
|
||||
// public Transform beatPrefab;
|
||||
|
||||
[SerializeField] public Dictionary<BeatType, Transform> beatPrefabs;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ac1e8cb247f54fceb18065f749bb513c
|
||||
timeCreated: 1794675969
|
||||
@@ -0,0 +1,39 @@
|
||||
using System;
|
||||
using Code.Infrastructure.SceneLoader;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using R3;
|
||||
using Reflex.Core;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Code.Game.Minigames.MusicGame
|
||||
{
|
||||
public class MusicMinigameSceneInstaller: MonoBehaviour, IInstaller
|
||||
{
|
||||
private IDisposable _onContainerBuildDisposable;
|
||||
|
||||
public void InstallBindings(ContainerBuilder containerBuilder)
|
||||
{
|
||||
_onContainerBuildDisposable = Observable
|
||||
.FromEvent<Container>(
|
||||
h => containerBuilder.OnContainerBuilt += h,
|
||||
h => containerBuilder.OnContainerBuilt -= h)
|
||||
.Subscribe(x => OnContainerBuilt(x).Forget());
|
||||
}
|
||||
|
||||
private ISceneContainerManager _sceneContainerManager;
|
||||
private async UniTaskVoid OnContainerBuilt(Container container)
|
||||
{
|
||||
await UniTask.Yield();
|
||||
|
||||
_onContainerBuildDisposable.Dispose();
|
||||
|
||||
_sceneContainerManager = container.Resolve<ISceneContainerManager>();
|
||||
_sceneContainerManager.ScenesContainers.Add(SceneId.MusicMinigame, container);
|
||||
}
|
||||
|
||||
public void OnDestroy()
|
||||
{
|
||||
_sceneContainerManager.ScenesContainers.Remove(SceneId.MusicMinigame);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: edefcd12d6e34b4095a4ce2db069073d
|
||||
timeCreated: 1794658908
|
||||
@@ -0,0 +1,106 @@
|
||||
using Code.Infrastructure.App;
|
||||
using Code.Infrastructure.Generators;
|
||||
using Code.Infrastructure.Input;
|
||||
using Code.Infrastructure.SceneLoader;
|
||||
using Code.Infrastructure.UI;
|
||||
using Code.Infrastructure.Utils;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using ObservableCollections;
|
||||
using R3;
|
||||
using Reflex.Core;
|
||||
using Reflex.Enums;
|
||||
using UnityEngine.InputSystem;
|
||||
using UnityEngine.SceneManagement;
|
||||
|
||||
namespace Code.Game.Minigames.MusicGame
|
||||
{
|
||||
[State(typeof(AppStates), typeof(AppStateMachineContext))]
|
||||
public partial class MusicMinigameState
|
||||
{
|
||||
public MusicMinigameState(AppStateMachineContext context) : base(context)
|
||||
{
|
||||
}
|
||||
|
||||
public override AppStates MyState => AppStates.MusicMinigame;
|
||||
|
||||
public override async UniTask EnterState()
|
||||
{
|
||||
Context.InputService.EnableMap(ActionMappings.MusicMinigame);
|
||||
|
||||
#if UNITY_EDITOR
|
||||
Context.InputService.EnableMap(ActionMappings.Debug);
|
||||
|
||||
Context.InputService.BindActionPerformed((int)ActionMappings.Debug, (int)DebugActions.ToggleMinigames,
|
||||
ToggleMinigames);
|
||||
#endif
|
||||
// await Context.Camera.SwitchState(CameraMode.None);
|
||||
|
||||
var waitForContainer =
|
||||
Context.SceneContainerManager.ScenesContainers
|
||||
.ObserveDictionaryAdd()
|
||||
.Where(x => x.Key == SceneId.MusicMinigame)
|
||||
.Take(1)
|
||||
.WaitAsync()
|
||||
.AsUniTask();
|
||||
|
||||
var gameplayContainer = Context.SceneContainerManager.ScenesContainers[SceneId.Testing];
|
||||
|
||||
var musicTrackConfigs = new MultipleConfig<MusicMinigameTrackConfig>();
|
||||
var minigameConfig = new SingleConfig<MusicMinigameConfig>();
|
||||
|
||||
await UniTask.WhenAll(
|
||||
musicTrackConfigs.LoadConfigs(),
|
||||
minigameConfig.LoadConfig()
|
||||
);
|
||||
|
||||
var panelLoading = await Context.UIFactory.LoadPanel(UIPanelType.MusicMinigame, false);
|
||||
|
||||
await UniTask.WhenAll(
|
||||
Context.SceneLoader
|
||||
.LoadSceneAsync(
|
||||
SceneId.MusicMinigame,
|
||||
LoadSceneMode.Additive,
|
||||
true,
|
||||
InstallExtra),
|
||||
waitForContainer
|
||||
);
|
||||
|
||||
var container = Context.SceneContainerManager.ScenesContainers[SceneId.MusicMinigame];
|
||||
|
||||
var beatManager = container.Resolve<BeatManager>();
|
||||
|
||||
beatManager.Mount(3);
|
||||
|
||||
beatManager.StartPlay(MusicMinigameTrackType.OverworldDefault);
|
||||
|
||||
|
||||
void InstallExtra(Scene scene, ContainerBuilder builder)
|
||||
{
|
||||
builder.SetParent(gameplayContainer);
|
||||
|
||||
builder.RegisterValue(musicTrackConfigs);
|
||||
builder.RegisterValue(minigameConfig);
|
||||
builder.RegisterValue(panelLoading);
|
||||
|
||||
builder.RegisterType(typeof(BeatManager), Lifetime.Singleton, Resolution.Eager);
|
||||
}
|
||||
}
|
||||
|
||||
private void ToggleMinigames(InputAction.CallbackContext obj)
|
||||
{
|
||||
Context.StateMachine.SwitchState(AppStates.Testing).Forget();
|
||||
}
|
||||
|
||||
public override async UniTask ExitState()
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
Context.InputService.DisableMap(ActionMappings.Debug);
|
||||
Context.InputService.UnbindActionPerformed((int)ActionMappings.Debug, (int)DebugActions.ToggleMinigames,
|
||||
ToggleMinigames);
|
||||
#endif
|
||||
Context.InputService.DisableMap(ActionMappings.MusicMinigame);
|
||||
|
||||
await Context.SceneLoader.UnloadSceneAsync(SceneId.MusicMinigame);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f66c4511c7084da3b103335ba43e0963
|
||||
timeCreated: 1794658894
|
||||
@@ -0,0 +1,20 @@
|
||||
using Code.Infrastructure.Generators;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Code.Game.Minigames.MusicGame
|
||||
{
|
||||
[Config("Configs/Minigame Track Config", true)]
|
||||
[CreateAssetMenu(fileName = "MinigameTrackConfig", menuName = "Configs/Minigame Track Config")]
|
||||
public class MusicMinigameTrackConfig : ScriptableObject
|
||||
{
|
||||
public MusicMinigameTrackType Type;
|
||||
|
||||
public AudioClip Clip;
|
||||
public int hitWindowSamples;
|
||||
public int leadTimeSamples;
|
||||
public TextAsset midiFile;
|
||||
|
||||
public int bpm;
|
||||
public int maxEnemyHealth;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: bb7b46e42e7d43e5a883d60a072d5fe1
|
||||
timeCreated: 1794674756
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace Code.Game.Minigames.MusicGame
|
||||
{
|
||||
public enum MusicMinigameTrackType
|
||||
{
|
||||
OverworldDefault
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ccf201beab9840f18d8affca3a4f0baa
|
||||
timeCreated: 1794675058
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ded13bd2fa6b4ffd828ca291993672f0
|
||||
timeCreated: 1794671154
|
||||
@@ -0,0 +1,13 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Code.Game.Minigames.MusicGame.UI
|
||||
{
|
||||
public class BeatLine : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private Transform startPosition;
|
||||
[SerializeField] private Transform spotPosition;
|
||||
|
||||
public Vector3 StartPosition => startPosition.position;
|
||||
public Vector3 SpotPosition => spotPosition.position;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a5b224e8ccea4322bbe71c479a72582f
|
||||
timeCreated: 1794690076
|
||||
@@ -0,0 +1,9 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Code.Game.Minigames.MusicGame.UI
|
||||
{
|
||||
public class BeatLineZone : MonoBehaviour
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2d93103cfed541bd9580655c518a2496
|
||||
timeCreated: 1794778337
|
||||
@@ -0,0 +1,26 @@
|
||||
using Cysharp.Threading.Tasks;
|
||||
using LitMotion;
|
||||
using LitMotion.Extensions;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Code.Game.Minigames.MusicGame.UI
|
||||
{
|
||||
public class MusicMinigameComboText : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private TMP_Text comboText;
|
||||
|
||||
[SerializeField] private float shakeStrength;
|
||||
[SerializeField] private float shakeDuration;
|
||||
|
||||
public async UniTaskVoid SetComboText(int combo)
|
||||
{
|
||||
LMotion.Shake
|
||||
.Create(1, shakeStrength, shakeDuration)
|
||||
.BindToLocalScaleXYZ(comboText.transform);
|
||||
|
||||
// StringBuilder
|
||||
comboText.SetText('x' + combo.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c5f82f4d138540fdb4a8e050e53a5924
|
||||
timeCreated: 1794695037
|
||||
@@ -0,0 +1,15 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace Code.Game.Minigames.MusicGame.UI
|
||||
{
|
||||
public class MusicMinigameEnemyHealth : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private Image health;
|
||||
|
||||
public void SetEnemyHealth(float healthPercent)
|
||||
{
|
||||
health.fillAmount = healthPercent;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5079a8f7043e457490e20a72a13926a5
|
||||
timeCreated: 1794694475
|
||||
@@ -0,0 +1,76 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
using Alchemy.Inspector;
|
||||
using Code.Infrastructure.UI;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using LitMotion;
|
||||
using LitMotion.Extensions;
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem.Utilities;
|
||||
|
||||
namespace Code.Game.Minigames.MusicGame.UI
|
||||
{
|
||||
public class MusicMinigamePanel : UIPanel
|
||||
{
|
||||
[TabGroup("OpenClose")]
|
||||
[SerializeField] private CanvasGroup canvasGroup;
|
||||
[TabGroup("OpenClose")]
|
||||
[SerializeField] private float leadTime;
|
||||
[TabGroup("OpenClose")]
|
||||
[SerializeField] private float starterScale;
|
||||
|
||||
[TabGroup("SubElements")]
|
||||
[SerializeField] private BeatLine[] beatLines;
|
||||
[TabGroup("SubElements")]
|
||||
[SerializeField] private MusicMinigamePlayerHealth playerHealth;
|
||||
[TabGroup("SubElements")]
|
||||
[SerializeField] private MusicMinigameEnemyHealth enemyHealth;
|
||||
[TabGroup("SubElements")]
|
||||
[SerializeField] private MusicMinigameComboText comboText;
|
||||
[TabGroup("SubElements")]
|
||||
[SerializeField] private RectTransform beatLineZone;
|
||||
|
||||
public ReadOnlyArray<BeatLine> BeatLines { get; private set; }
|
||||
public MusicMinigamePlayerHealth PlayerHealth => playerHealth;
|
||||
public MusicMinigameEnemyHealth EnemyHealth => enemyHealth;
|
||||
public MusicMinigameComboText MusicMinigameComboText => comboText;
|
||||
public RectTransform BeatLineZone => beatLineZone;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
BeatLines = beatLines;
|
||||
}
|
||||
|
||||
public override async UniTask Open(CancellationToken cts = default)
|
||||
{
|
||||
var canvasGroupMotion = LMotion
|
||||
.Create(0, 1f, leadTime)
|
||||
.BindToAlpha(canvasGroup);
|
||||
|
||||
var scale = LMotion
|
||||
.Create(starterScale, 1f, leadTime)
|
||||
.WithEase(Ease.InBounce)
|
||||
.BindToLocalScaleXYZ(transform);
|
||||
|
||||
await UniTask.WhenAll(
|
||||
canvasGroupMotion.ToUniTask(cts),
|
||||
scale.ToUniTask(cts));
|
||||
}
|
||||
|
||||
public override async UniTask Close(CancellationToken cts = default)
|
||||
{
|
||||
var canvasGroupMotion = LMotion
|
||||
.Create(1f, 0, leadTime)
|
||||
.BindToAlpha(canvasGroup);
|
||||
|
||||
var scale = LMotion
|
||||
.Create(1f, starterScale, leadTime)
|
||||
.WithEase(Ease.InBounce)
|
||||
.BindToLocalScaleXYZ(transform);
|
||||
|
||||
await UniTask.WhenAll(
|
||||
canvasGroupMotion.ToUniTask(cts),
|
||||
scale.ToUniTask(cts));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 186e32a47b28436aa814d067cee27ad5
|
||||
timeCreated: 1794689064
|
||||
@@ -0,0 +1,36 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Code.Game.Minigames.MusicGame.UI
|
||||
{
|
||||
public class MusicMinigamePlayerHealth : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private GameObject heartPrefab;
|
||||
|
||||
private List<GameObject> _hearts = new();
|
||||
|
||||
// sbe
|
||||
public void SetHealth(int count)
|
||||
{
|
||||
if (_hearts.Count < count)
|
||||
for (; count > 0; count--)
|
||||
_hearts.Add(Instantiate(heartPrefab, transform));
|
||||
else if (count >= 0)
|
||||
{
|
||||
for (var toDelete = _hearts.Count - count; toDelete > 0; toDelete--)
|
||||
{
|
||||
Destroy(_hearts[toDelete - 1]);
|
||||
_hearts.RemoveAt(toDelete - 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void DecreaseHealth()
|
||||
{
|
||||
if (_hearts.Count <= 0) return;
|
||||
|
||||
Destroy(_hearts[0]);
|
||||
_hearts.RemoveAt(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8a7df6486b9b42529f373f1a54a58f52
|
||||
timeCreated: 1794690928
|
||||
Reference in new issue
Block a user