Контроль музыки, пробел
This commit is contained in:
1 parent
a45223d0e7
commit
cd56b1969c
12 files changed
+344
-95
No files matched your search
@@ -65,7 +65,7 @@ namespace Code.Example.Race
|
||||
_raceTrackData.trackData.RacerPoints[i].transform).GetComponent<Racer.AI.Racer>());
|
||||
PlacedRacers[^1].SetData(_racerPrefabs[_randomizedTypes[i]].Data);
|
||||
PlacedRacers[^1].Id = i;
|
||||
_scores.SubscribeRacer(i);
|
||||
_scores.SubscribeRacer(PlacedRacers[^1]);
|
||||
}
|
||||
|
||||
foreach (var racer in PlacedRacers)
|
||||
|
||||
@@ -8,7 +8,7 @@ namespace Code.Example.Race
|
||||
{
|
||||
public class RaceScores : IRacerPassedCheckpointHandler, IDisposable
|
||||
{
|
||||
private Dictionary<int, int> _scores = new();
|
||||
private Dictionary<Racer.AI.Racer, int> _scores = new();
|
||||
private EventBus _eventBus;
|
||||
|
||||
public RaceScores(EventBus eventBus)
|
||||
@@ -16,28 +16,28 @@ namespace Code.Example.Race
|
||||
_eventBus = eventBus;
|
||||
_eventBus.Subscribe(this);
|
||||
}
|
||||
public void SubscribeRacer(int racer)
|
||||
public void SubscribeRacer(Racer.AI.Racer racer)
|
||||
{
|
||||
_scores.Add(racer, 0);
|
||||
}
|
||||
|
||||
public void ChangeScore(int racer, int additionalScore)
|
||||
public void ChangeScore(Racer.AI.Racer racer, int additionalScore)
|
||||
{
|
||||
_scores[racer] += additionalScore;
|
||||
_eventBus.RaiseEvent<IScoreChangedHandle>(x=> x.ScoreChanged(racer,additionalScore, GetScores()));
|
||||
}
|
||||
|
||||
public void UnsubscribeRacer(int racer)
|
||||
public void UnsubscribeRacer(Racer.AI.Racer racer)
|
||||
{
|
||||
_scores.Remove(racer);
|
||||
}
|
||||
|
||||
public List<KeyValuePair<int, int>> GetScores()
|
||||
public List<KeyValuePair<Racer.AI.Racer, int>> GetScores()
|
||||
{
|
||||
return _scores.OrderByDescending(x => x.Value).ToList();
|
||||
}
|
||||
|
||||
public void OnRacerPassedCheckpoint(Racer.AI.Racer racer) => ChangeScore(racer.Id,1);
|
||||
public void OnRacerPassedCheckpoint(Racer.AI.Racer racer) => ChangeScore(racer,1);
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
@@ -47,6 +47,6 @@ namespace Code.Example.Race
|
||||
|
||||
public interface IScoreChangedHandle : IGlobalSubscriber
|
||||
{
|
||||
void ScoreChanged(int racer, int additionalScore, List<KeyValuePair<int, int>> scores);
|
||||
void ScoreChanged(Racer.AI.Racer racer, int additionalScore, List<KeyValuePair<Racer.AI.Racer, int>> scores);
|
||||
}
|
||||
}
|
||||
@@ -25,7 +25,7 @@ namespace Code.Example.Race.Racer.Player
|
||||
|
||||
private void MovementEnded(InputAction.CallbackContext context) => MovementInput = Vector2.zero;
|
||||
|
||||
private void BrakePerformed(InputAction.CallbackContext context) => Brake = context.ReadValue<bool>();
|
||||
private void BrakePerformed(InputAction.CallbackContext context) => Brake = true;
|
||||
|
||||
private void BrakeEnded(InputAction.CallbackContext context) => Brake = false;
|
||||
|
||||
|
||||
@@ -38,11 +38,11 @@ namespace Code.Example.Race.UI
|
||||
{
|
||||
}
|
||||
|
||||
public void ScoreChanged(int racer, int additionalScore, List<KeyValuePair<int,int>> scores)
|
||||
public void ScoreChanged(Racer.AI.Racer racer, int additionalScore, List<KeyValuePair<Racer.AI.Racer,int>> scores)
|
||||
{
|
||||
// if (racer.Data.type == RacerType.Player)
|
||||
string output = "";
|
||||
foreach (var score in scores) output += $@"{score.Key}: {score.Value}\n";
|
||||
foreach (var score in scores) output += $@"{score.Key.Data.name}: {score.Value}\n";
|
||||
playerScore.text = output;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,8 +41,7 @@ namespace Code.Infrastructure.App.States
|
||||
}
|
||||
public void OnReadyToPlay(AudioNames groupName)
|
||||
{
|
||||
Context.AudioService.PlayRepeatedly(AudioNames.UISounds,0,new CancellationTokenSource());
|
||||
|
||||
Context.AudioService.PlayRepeatedly(AudioNames.UISounds,0);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@ using Code.Example.Race;
|
||||
using Code.Example.Race.Racer;
|
||||
using Code.Example.Race.Racer.AI;
|
||||
using Code.Example.Race.Track;
|
||||
using Code.Infrastructure.Audio;
|
||||
using Code.Infrastructure.Camera;
|
||||
using Code.Infrastructure.Camera.CameraStates;
|
||||
using Code.Infrastructure.Generators;
|
||||
@@ -53,6 +54,8 @@ namespace Code.Infrastructure.App.States
|
||||
|
||||
private void GameplayLoadingCompleted()
|
||||
{
|
||||
Context.AudioService.CancelMusic(AudioNames.UISounds, 0);
|
||||
|
||||
_raceGame.PlaceRacers();
|
||||
|
||||
Context.Camera.SwitchState(CameraMode.ThirdPerson).AllowStateTransition(CameraMode.None);
|
||||
|
||||
@@ -17,9 +17,11 @@ namespace Code.Infrastructure.Audio
|
||||
AudioSource audioSource = null);
|
||||
|
||||
UniTaskVoid PlayRepeatedly(AudioNames groupName, int id,
|
||||
CancellationTokenSource cancellationTokenSource, AudioSourceType sourceType = AudioSourceType.NoBlend, AudioSource audioSource = null);
|
||||
AudioSourceType sourceType = AudioSourceType.NoBlend, AudioSource audioSource = null, CancellationTokenSource cancellationTokenSource = null);
|
||||
|
||||
bool CheckClip(AudioNames groupName, int id);
|
||||
void CancelMusic(AudioNames groupName, int id);
|
||||
void ReleaseCancellationSource(AudioNames groupName, int id);
|
||||
}
|
||||
|
||||
public partial class AudioService : IAudioClipsEventHandle, IAudioService
|
||||
@@ -67,12 +69,22 @@ namespace Code.Infrastructure.Audio
|
||||
|
||||
_pool.ReleaseSource(audioSource);
|
||||
}
|
||||
|
||||
|
||||
private Dictionary<KeyValuePair<AudioNames, int>, CancellationTokenSource> _cancellationTokenSources = new();
|
||||
|
||||
public void CancelMusic(AudioNames groupName, int id) => _cancellationTokenSources[new KeyValuePair<AudioNames, int>(groupName, id)].Cancel();
|
||||
public void ReleaseCancellationSource(AudioNames groupName, int id) => _cancellationTokenSources[new KeyValuePair<AudioNames, int>(groupName, id)] = null;
|
||||
public async UniTaskVoid PlayRepeatedly(AudioNames groupName, int id,
|
||||
CancellationTokenSource cancellationTokenSource, AudioSourceType sourceType = AudioSourceType.NoBlend, AudioSource audioSource = null)
|
||||
AudioSourceType sourceType = AudioSourceType.NoBlend, AudioSource audioSource = null, CancellationTokenSource cancellationTokenSource = null)
|
||||
{
|
||||
if (!CheckClip(groupName, id)) return;
|
||||
|
||||
if(cancellationTokenSource == null)
|
||||
{
|
||||
cancellationTokenSource = new CancellationTokenSource();
|
||||
_cancellationTokenSources[new KeyValuePair<AudioNames, int>(groupName, id)] = cancellationTokenSource;
|
||||
}
|
||||
|
||||
if (!audioSource) audioSource = _pool.GetSource(sourceType);
|
||||
if(!audioSource.resource) audioSource.resource = GetClip(groupName, id);
|
||||
|
||||
@@ -90,10 +102,12 @@ namespace Code.Infrastructure.Audio
|
||||
|
||||
_pool.ReleaseSource(audioSource);
|
||||
|
||||
ReleaseCancellationSource(groupName,id);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
PlayRepeatedly(groupName, id, cancellationTokenSource, sourceType, audioSource).Forget();
|
||||
PlayRepeatedly(groupName, id, sourceType, audioSource, cancellationTokenSource).Forget();
|
||||
}
|
||||
|
||||
public AudioClip GetClip(AudioNames groupName, int id) => _audioClips[groupName][id];
|
||||
|
||||
Reference in new issue
Block a user