Развитие гоночек
This commit is contained in:
1 parent
ec60c47652
commit
b54083521f
532 files changed
+16091
-28231
No files matched your search
@@ -0,0 +1,106 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Code.Example.Race.Racer;
|
||||
using Code.Example.Race.Track;
|
||||
using Code.Infrastructure.EventBus;
|
||||
using Code.Infrastructure.Loadings;
|
||||
using Reflex.Attributes;
|
||||
|
||||
namespace Code.Example.Race
|
||||
{
|
||||
public class RaceGame : IRacerLoadedHandle, IDisposable
|
||||
{
|
||||
private RaceScores _scores;
|
||||
|
||||
private IRacerFactory _racerFactory;
|
||||
private RaceTrackData _raceTrackData;
|
||||
private EventBus _eventBus;
|
||||
|
||||
public RaceGame(EventBus eventBus, IRacerFactory racerFactory, RaceTrackData raceTrackData, RaceScores scores)
|
||||
{
|
||||
_eventBus = eventBus;
|
||||
_racerFactory = racerFactory;
|
||||
_raceTrackData = raceTrackData;
|
||||
_eventBus.Subscribe(this);
|
||||
|
||||
_scores = scores;
|
||||
|
||||
RandomizeRacers(raceTrackData.trackData.RacerPoints.Count);
|
||||
}
|
||||
|
||||
public void StartRacers()
|
||||
{
|
||||
foreach (var racer in PlacedRacers)
|
||||
{
|
||||
racer.StartRacer();
|
||||
}
|
||||
}
|
||||
|
||||
// DEBUG
|
||||
private List<RacerType> _randomizedTypes = new();
|
||||
|
||||
private void RandomizeRacers(int count)
|
||||
{
|
||||
_raceGameLoading = new Loading(count, RacersLoadCompleted);
|
||||
|
||||
_randomizedTypes.Add(RacerType.Player);
|
||||
|
||||
for (var i = 0; i < count - 1; i++)
|
||||
_randomizedTypes.Add((RacerType)UnityEngine.Random.Range(1, 3));
|
||||
|
||||
foreach (var type in _randomizedTypes) _racerFactory.LoadRacer(type);
|
||||
}
|
||||
|
||||
private void RacersLoadCompleted() =>
|
||||
_eventBus.RaiseEvent<IRaceGameInitializedHandle>(x => x.RaceGameReady());
|
||||
|
||||
public List<Racer.AI.Racer> PlacedRacers { get; } = new();
|
||||
|
||||
public void PlaceRacers()
|
||||
{
|
||||
for (var i = 0; i < _raceTrackData.trackData.RacerPoints.Count; i++)
|
||||
{
|
||||
PlacedRacers.Add(UnityEngine.Object.Instantiate(
|
||||
_racerPrefabs[_randomizedTypes[i]],
|
||||
_raceTrackData.trackData.RacerPoints[i].transform).GetComponent<Racer.AI.Racer>());
|
||||
PlacedRacers[^1].SetData(_racerPrefabs[_randomizedTypes[i]].Data);
|
||||
PlacedRacers[^1].Id = i;
|
||||
_scores.SubscribeRacer(i);
|
||||
}
|
||||
|
||||
foreach (var racer in PlacedRacers)
|
||||
{
|
||||
racer.Initialize(_raceTrackData.trackData.Checkpoints);
|
||||
}
|
||||
}
|
||||
|
||||
private Dictionary<RacerType, Racer.AI.Racer> _racerPrefabs = new();
|
||||
private Loading _raceGameLoading;
|
||||
|
||||
public void RacerLoaded(RacerType type, Racer.AI.Racer racer)
|
||||
{
|
||||
if (racer)
|
||||
_racerPrefabs.Add(type, racer);
|
||||
|
||||
_raceGameLoading.DoProgress();
|
||||
}
|
||||
|
||||
// private Dictionary<RacerType, Racer.AI.Racer> _spawnedRacers = new();
|
||||
public void RacerCreated(RacerType type, Racer.AI.Racer racer)
|
||||
{
|
||||
// _spawnedRacers.Add(type, racer);
|
||||
// _raceGameLoading.DoProgress();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_eventBus.Unsubscribe(this);
|
||||
_racerPrefabs.Clear();
|
||||
}
|
||||
}
|
||||
|
||||
public interface IRaceGameInitializedHandle : IGlobalSubscriber
|
||||
{
|
||||
void RaceGameReady();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user