Files
Inter-Face-Off/Assets/Scripts/ScoreTracker.cs

74 lines
2.2 KiB
C#

using System.Collections.Generic;
using InterfaceOff.MainMenu;
using UnityEngine;
using UnityEngine.UI;
namespace InterfaceOff.WorldScene
{
[System.Serializable] public struct PlayerScore
{
public string PlayerName;
public float Score;
}
public class ScoreTracker : MonoBehaviour
{
[field: SerializeField] private bool GameAngliaVersion { get; set; }
[field: SerializeField] private Text ScoreText { get; set; }
[field: SerializeField] private Text FinalScoreText { get; set; }
[field: SerializeField] private WindowSpawner Spawner { get; set; }
private static List<PlayerScore> PlayerScores { get; } = new();
private static ScoreTracker Instance { get; set; }
private string CurrentPlayerName { get; set; } = string.Empty;
private bool StoredCurrentScore = false;
private float Score;
public static float CurrentScore()
{
if (DebugUtils.IsNull(Instance))
{
return 0;
}
return Instance.Score;
}
private void Awake() => Instance = this;
private void Update()
{
if (Spawner.AutoSpawn)
{
StoredCurrentScore = false;
Score = Time.timeSinceLevelLoad * DifficultyManager.DifficultyEffect;
ScoreText.text = $"Score: {Score:F1}";
}
else
{
FinalScoreText.text = $"Your final score is: {Score:F1}";
ScoreText.text = null;
}
}
public void AddCurrentScoreToLeaderboard()
{
if (CurrentPlayerName != string.Empty && !StoredCurrentScore)
{
PlayerScore score = new();
score.PlayerName = CurrentPlayerName;
score.Score = CurrentScore();
StoredCurrentScore = true;
PlayerScores.Add(score);
Debug.Log($"Added score [{score.PlayerName} | {score.Score}]");
}
}
public void SetPlayerName(string input) => CurrentPlayerName = input;
}
}