80 lines
2.4 KiB
C#
80 lines
2.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using Code.Example.Race.Racer.Player;
|
|
using Code.Example.Race.Track;
|
|
using Code.Infrastructure.EventBus;
|
|
using Reflex.Attributes;
|
|
|
|
namespace Code.Example.Race
|
|
{
|
|
public class RaceScores : IRacerPassedCheckpointHandler, IDisposable, IPlayerPassedCheckpointHandler
|
|
{
|
|
private Dictionary<Racer.Racer, ScoreData> _scores = new();
|
|
private EventBus _eventBus;
|
|
|
|
public RaceScores(EventBus eventBus)
|
|
{
|
|
_eventBus = eventBus;
|
|
_eventBus.Subscribe(this);
|
|
}
|
|
|
|
public void SubscribeRacer(Racer.Racer racer)
|
|
{
|
|
_scores.Add(racer, new ScoreData());
|
|
}
|
|
|
|
public void ChangeScore(Racer.Racer racer, int additionalScore)
|
|
{
|
|
_scores[racer].checkpointsPassed += additionalScore;
|
|
_eventBus.RaiseEvent<IScoreChangedHandle>(x=> x.ScoreChanged(racer,_scores[racer], GetScores()));
|
|
}
|
|
|
|
public void LapPassed(Racer.Racer racer)
|
|
{
|
|
_scores[racer].lapsPassed++;
|
|
_eventBus.RaiseEvent<IScoreChangedHandle>(x=> x.ScoreChanged(racer,_scores[racer], GetScores()));
|
|
if (_scores[racer].lapsPassed >= _raceTrackData.trackData.LapCount)
|
|
_eventBus.RaiseEvent<IRaceGameHandle>(x=> x.RaceGameEnded(this));
|
|
}
|
|
|
|
public void UnsubscribeRacer(Racer.Racer racer)
|
|
{
|
|
_scores.Remove(racer);
|
|
}
|
|
|
|
public List<KeyValuePair<Racer.Racer, ScoreData>> GetScores()
|
|
{
|
|
return _scores.OrderByDescending(x => x.Value.checkpointsPassed).ToList();
|
|
}
|
|
|
|
public void OnRacerPassedCheckpoint(Racer.Racer racer) => ChangeScore(racer,1);
|
|
|
|
public void Dispose()
|
|
{
|
|
_eventBus.Unsubscribe(this);
|
|
}
|
|
|
|
[Inject] private RaceTrackData _raceTrackData;
|
|
|
|
public void OnPlayerPassedCheckpoint(Racer.Racer racer)
|
|
{
|
|
var player = (PlayerRacer)racer;
|
|
_raceTrackData.trackData.Checkpoints[player.CurrentWaypointIndex].Show();
|
|
}
|
|
|
|
public void OnPlayerLapPassed(Racer.Racer racer)
|
|
{
|
|
}
|
|
}
|
|
|
|
public class ScoreData
|
|
{
|
|
public int checkpointsPassed;
|
|
public int lapsPassed;
|
|
}
|
|
public interface IScoreChangedHandle : IGlobalSubscriber
|
|
{
|
|
void ScoreChanged(Racer.Racer racer, ScoreData score, List<KeyValuePair<Racer.Racer, ScoreData>> scores);
|
|
}
|
|
} |