using System;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
namespace XRMultiplayer.MiniGames
{
///
/// Represents a slot in the scoreboard for a player.
///
public class ScoreboardSlot : MonoBehaviour
{
///
/// Gets or sets the current score of the player.
///
public float currentScore
{
get => m_Score;
set => m_Score = value;
}
///
/// Internal value that gets or sets the current score of the player.
///
float m_Score;
///
/// External value that gets or sets a value indicating whether the player has finished the game.
///
public bool isFinished
{
get => m_IsFinished;
set => m_IsFinished = value;
}
///
/// Internal value that gets or sets a value indicating whether the player has finished the game.
///
bool m_IsFinished;
///
/// External value that gets or sets a value indicating whether the player is ready.
///
public bool isReady
{
get => m_IsReady;
set => m_IsReady = value;
}
///
/// Internal value that gets or sets a value indicating whether the player is ready.
///
bool m_IsReady;
///
/// The text for the place of the player.
///
[SerializeField] TMP_Text m_PlaceText;
///
/// The text for the name of the player.
///
[SerializeField] TMP_Text m_PlayerNameText;
///
/// The text for the score of the player.
///
[SerializeField] TMP_Text m_PlayerScoreText;
///
/// The icon to indicate the player is ready.
///
[SerializeField] Image m_ReadyIcon;
///
/// The icon to indicate the player is ready.
///
[SerializeField] GameObject m_PlayerIcon;
///
/// The object to display when the slot is open.
///
[SerializeField] GameObject m_OpenObject;
///
/// The object to display when the slot is closed.
///
[SerializeField] GameObject m_ClosedObject;
///
/// Sets up the player slot with the specified place and player name.
///
/// The place of the player.
/// The name of the player.
public void SetupPlayerSlot(int place, string playerName)
{
m_OpenObject.SetActive(false);
m_ClosedObject.SetActive(true);
m_PlayerIcon.SetActive(true);
m_PlaceText.enabled = false;
m_PlayerNameText.text = playerName;
UpdatePlace(place);
ToggleReady(false);
m_PlayerScoreText.text = "";
m_IsFinished = false;
m_IsReady = false;
}
///
/// Sets the slot as open.
///
public void SetSlotOpen()
{
m_OpenObject.SetActive(true);
m_ClosedObject.SetActive(false);
m_IsFinished = false;
}
///
/// Updates the score of the player.
///
/// The new score of the player.
/// The type of the game.
public void UpdateScore(float score, MiniGameBase.GameType gameType)
{
m_PlayerIcon.SetActive(false);
m_PlaceText.enabled = true;
m_PlayerScoreText.enabled = true;
m_ReadyIcon.enabled = false;
currentScore = score;
if (gameType == MiniGameBase.GameType.Time)
{
TimeSpan time = TimeSpan.FromSeconds(score);
m_PlayerScoreText.text = time.ToString("mm':'ss'.'ff");
}
else
{
m_PlayerScoreText.text = score.ToString("N0");
}
}
///
/// Updates the place of the player.
///
/// The new place of the player.
public void UpdatePlace(int place)
{
m_PlaceText.text = $"{place}{Utils.GetOrdinal(place)}";
}
///
/// Toggles the ready state of the player.
///
/// The new ready state.
public void ToggleReady(bool toggle)
{
m_IsReady = toggle;
m_PlayerScoreText.enabled = false;
m_ReadyIcon.enabled = true;
m_ReadyIcon.color = toggle ? Color.green : Color.red;
}
}
}