201 lines
5.7 KiB
C#
201 lines
5.7 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using Assets.Scripts;
|
|
using System;
|
|
using static GameController;
|
|
using static PlayersHandler;
|
|
using static GameHandler;
|
|
using System.Linq;
|
|
|
|
public class ScoreHandler : SingletonMB<ScoreHandler>
|
|
{
|
|
#region PROPERTIES
|
|
|
|
private Dictionary<int, PlayerScore> PlayersScores { get; set; }
|
|
private Dictionary<int, PlayerScore> OuttedPlayers { get; set; }
|
|
|
|
#endregion
|
|
#region VARIABLES
|
|
|
|
[SerializeField] private GameObject ScorePanelPrefab;
|
|
[SerializeField] private GameObject UiDocument;
|
|
private GameController _gameController;
|
|
private PlayersHandler _playersHandler;
|
|
private GameHandler _gameHandler;
|
|
private DataBearer _dataBearer;
|
|
|
|
#endregion
|
|
#region EVENTS
|
|
|
|
public event EventHandler<ScoreEventArgs> OnScoreReachingZero;
|
|
|
|
public class ScoreEventArgs : EventArgs
|
|
{
|
|
public int playerId;
|
|
public ScoreEventArgs(int id)
|
|
{
|
|
playerId = id;
|
|
}
|
|
}
|
|
internal void RemovePlayer_OnPlayerFinished(object sender, PlayerEventArgs args)
|
|
{
|
|
if (PlayersScores.TryGetValue(args.player.Id, out PlayerScore looser))
|
|
{
|
|
_dataBearer.RecordPlayerDistance(looser.PlayerName);
|
|
_dataBearer.RecordPlayerScore(looser.PlayerId, looser.PlayerName);
|
|
_dataBearer.RecordPlayerAttemptDuration(looser.PlayerName);
|
|
|
|
OuttedPlayers.Add(looser.PlayerId, looser);
|
|
PlayersScores.Remove(looser.PlayerId);
|
|
}
|
|
|
|
|
|
}
|
|
private void RemoveScores_OnAttemptEnded(object sender, AttemptEventArgs args)
|
|
{
|
|
List<int> keys;
|
|
if (PlayersScores.Count > 0)
|
|
{
|
|
keys = new List<int>(PlayersScores.Keys);
|
|
foreach(var key in keys)
|
|
{
|
|
_dataBearer.RecordPlayerDistance(PlayersScores[key].PlayerName);
|
|
_dataBearer.RecordPlayerScore(key, PlayersScores[key].PlayerName);
|
|
_dataBearer.RecordPlayerAttemptDuration(PlayersScores[key].PlayerName);
|
|
|
|
OuttedPlayers.Add(key, PlayersScores[key]);
|
|
_playersHandler.RemovePlayer(key);
|
|
}
|
|
PlayersScores.Clear();
|
|
}
|
|
keys = new List<int>(OuttedPlayers.Keys);
|
|
foreach (var key in keys)
|
|
{
|
|
_dataBearer.RecordPlayerFinalScore((int)OuttedPlayers[key].Score, OuttedPlayers[key].PlayerName);
|
|
|
|
|
|
OuttedPlayers[key].ScorePanel.Dispose();
|
|
}
|
|
OuttedPlayers = new();
|
|
PlayersScores = new();
|
|
}
|
|
|
|
#endregion
|
|
#region ENDPOINTS
|
|
|
|
public void AddScorePanel(string playerName, int playerId)
|
|
{
|
|
var newScorePanel = Instantiate(ScorePanelPrefab, UiDocument.transform);
|
|
var scoreScript = newScorePanel.GetComponent<ScorePannelScript>();
|
|
scoreScript.SetPlayerName(playerName);
|
|
PlayerScore newRecord = new PlayerScore(playerName, playerId, scoreScript);
|
|
PlayersScores.Add(playerId, newRecord);
|
|
}
|
|
|
|
public void AddScorePanel(string playerName, int playerId, Color playerColor)
|
|
{
|
|
var newScorePanel = Instantiate(ScorePanelPrefab, UiDocument.transform);
|
|
var scoreScript = newScorePanel.GetComponent<ScorePannelScript>();
|
|
scoreScript.SetColor(playerColor);
|
|
scoreScript.SetPlayerName(playerName);
|
|
PlayerScore newRecord = new PlayerScore(playerName, playerId, scoreScript);
|
|
PlayersScores.Add(playerId, newRecord);
|
|
}
|
|
|
|
public void SetScore(float score, int playerId)
|
|
{
|
|
if (PlayersScores.TryGetValue(playerId, out PlayerScore updatedScore))
|
|
{
|
|
updatedScore.Score = score;
|
|
updatedScore.ScorePanel.SetScore(score);
|
|
PlayersScores[playerId] = updatedScore;
|
|
}
|
|
|
|
if ( score <= 0) //false &&
|
|
{
|
|
OnScoreReachingZero.Invoke(this, new ScoreEventArgs(playerId));
|
|
}
|
|
}
|
|
|
|
public void AddToScore(float score, int playerId)
|
|
{
|
|
if (PlayersScores.TryGetValue(playerId, out PlayerScore updatedScore))
|
|
{
|
|
updatedScore.Score += score;
|
|
SetScore(updatedScore.Score, playerId);
|
|
}
|
|
}
|
|
|
|
public void AddScoreToAllAlivePlayers(float scoreToAdd)
|
|
{
|
|
var keys = new List<int>(PlayersScores.Keys);
|
|
foreach (var key in keys)
|
|
{
|
|
AddToScore(scoreToAdd, key);
|
|
}
|
|
}
|
|
|
|
|
|
public float GetPlayerScore(int playerId)
|
|
{
|
|
return PlayersScores.TryGetValue(playerId, out PlayerScore pscore)
|
|
? pscore.Score
|
|
: OuttedPlayers.TryGetValue(playerId, out PlayerScore oscore)
|
|
? oscore.Score
|
|
: 0;
|
|
}
|
|
|
|
|
|
#endregion
|
|
#region METHODS
|
|
#endregion
|
|
#region LIFECYCLE
|
|
|
|
protected override void Awake()
|
|
{
|
|
base.Awake();
|
|
_gameController = GameController.Instance;
|
|
_playersHandler = PlayersHandler.Instance;
|
|
_gameHandler = GameHandler.Instance;
|
|
_dataBearer = DataBearer.Instance;
|
|
|
|
_playersHandler.OnPlayerFinished += RemovePlayer_OnPlayerFinished;
|
|
_gameHandler.OnAttemptEnding += RemoveScores_OnAttemptEnded;
|
|
|
|
PlayersScores = new();
|
|
OuttedPlayers = new();
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
struct PlayerScore
|
|
{
|
|
|
|
internal string PlayerName { get; set; }
|
|
internal int PlayerId { get; set; }
|
|
internal float Score { get; set; }
|
|
internal ScorePannelScript ScorePanel { get; set; }
|
|
public PlayerScore(string playerName, int playerId, ScorePannelScript scorePannel)
|
|
{
|
|
PlayerName = playerName;
|
|
PlayerId = playerId;
|
|
Score = 0f;
|
|
ScorePanel = scorePannel;
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
#region PROPERTIES
|
|
#endregion
|
|
#region VARIABLES
|
|
#endregion
|
|
#region EVENTS
|
|
#endregion
|
|
#region METHODS
|
|
#endregion
|
|
#region LIFECYCLE
|
|
#endregion |