Updated scoreboard system to show list of records via PlayerPrefs
This commit is contained in:
parent
388791ecd2
commit
e94e767912
@ -7,6 +7,7 @@ using UnityEngine.SceneManagement;
|
||||
public class GameSession : MonoBehaviour,IResettable
|
||||
{
|
||||
[SerializeField] private Player currentPlayer;
|
||||
[SerializeField] private Scoreboard scoreboard;
|
||||
public static GameSession Instance { get; private set; }
|
||||
public WorldCurver Curver { get; private set; }
|
||||
private IInputTranslator inputTranslator;
|
||||
@ -34,7 +35,7 @@ public class GameSession : MonoBehaviour,IResettable
|
||||
Curver.Tick();
|
||||
// curver.SinCurveX();
|
||||
// Curver.SinCurveY();
|
||||
Curver.TurnWorldToLeft();
|
||||
//Curver.TurnWorldToLeft();
|
||||
}
|
||||
|
||||
private void Init()
|
||||
@ -83,6 +84,11 @@ public class GameSession : MonoBehaviour,IResettable
|
||||
inputTranslator.RestictTranslation(commands, isRestricted);
|
||||
}
|
||||
|
||||
public void UpdateScoreboard(ScoreboardEntry entry)
|
||||
{
|
||||
scoreboard.AddScoreboardEntry(entry);
|
||||
}
|
||||
|
||||
public void RestartSession()
|
||||
{
|
||||
SceneManager.LoadScene("GameScene", LoadSceneMode.Single);
|
||||
|
@ -107,7 +107,8 @@ public class Player : MonoBehaviour,IResettable, ICommandTranslator
|
||||
|
||||
private void Die()
|
||||
{
|
||||
PlayerStateMachine.SetState(PlayerStateMachine.PlayerDeadState);
|
||||
PlayerStateMachine.SetState(PlayerStateMachine.PlayerDeadState);
|
||||
GameSession.Instance.UpdateScoreboard(new ScoreboardEntry(name,PlayerStatictics.Score));
|
||||
}
|
||||
|
||||
public IEnumerator GrantInvincibility()
|
||||
|
@ -13,6 +13,7 @@ public class DeadState : PlayerState
|
||||
playerSM.VerticalDeltaPosition = 0;
|
||||
playerSM.HorizontalDeltaPosition = Vector3.zero;
|
||||
GameSession.Instance.RestrictInputs(InputConstants.InGameCommands, true);
|
||||
|
||||
//Session.ShowGameOverPopUp(true);
|
||||
// Session.SetGameOverState();
|
||||
// Stats.CalculateScore();
|
||||
|
@ -8,7 +8,7 @@ public class Statistics : MonoBehaviour,IResettable
|
||||
private float distance;
|
||||
private int coinCount;
|
||||
private int coinMultiplier;
|
||||
private int score;
|
||||
public int Score { get; private set; }
|
||||
public event Action<int> OnCoinCountChanged = delegate { };
|
||||
public event Action<float> OnDistanceChanged = delegate { };
|
||||
public event Action<int> OnScoreCalculated = delegate { };
|
||||
@ -41,15 +41,15 @@ public class Statistics : MonoBehaviour,IResettable
|
||||
}
|
||||
public void CalculateScore()
|
||||
{
|
||||
score = Mathf.FloorToInt(coinCount * coinMultiplier + distance);
|
||||
OnScoreCalculated?.Invoke(score);
|
||||
PlayerHUD.UpdateScore(score.ToString());
|
||||
Score = Mathf.FloorToInt(coinCount * coinMultiplier + distance);
|
||||
OnScoreCalculated?.Invoke(Score);
|
||||
PlayerHUD.UpdateScore(Score.ToString());
|
||||
}
|
||||
|
||||
public void ResetToDefault()
|
||||
{
|
||||
distance = 0;
|
||||
score = 0;
|
||||
Score = 0;
|
||||
coinCount = 0;
|
||||
coinMultiplier = 1;
|
||||
//gameOverPopUp.gameObject.SetActive(false);
|
||||
|
@ -21,7 +21,7 @@ public class Scoreboard : MonoBehaviour, ICommandTranslator
|
||||
private void Start()
|
||||
{
|
||||
GameSession.Instance.AddCommandTranslator(this);
|
||||
string jsonScoreboardEntries = PlayerPrefs.GetString("ScoreboardEntriesTableT1"); //Binary file
|
||||
string jsonScoreboardEntries = PlayerPrefs.GetString("ScoreboardEntriesTableTest"); //Binary file
|
||||
ScoreboardEntriesTable entriesTable = JsonUtility.FromJson<ScoreboardEntriesTable>(jsonScoreboardEntries);
|
||||
if (entriesTable == null)
|
||||
return;
|
||||
@ -47,12 +47,14 @@ public class Scoreboard : MonoBehaviour, ICommandTranslator
|
||||
{
|
||||
entries.Add(entry);
|
||||
OnEntryAdded?.Invoke(entry);
|
||||
SaveScoreboardEntriesTable();
|
||||
}
|
||||
|
||||
public void SaveScoreboardEntriesTable()
|
||||
{
|
||||
ScoreboardEntriesTable scoreboardEntriesTable = new ScoreboardEntriesTable(entries);
|
||||
string jsonScoreboardEntries = JsonUtility.ToJson(scoreboardEntriesTable);
|
||||
PlayerPrefs.SetString("ScoreboardEntriesTableT1", jsonScoreboardEntries);
|
||||
PlayerPrefs.SetString("ScoreboardEntriesTableTest", jsonScoreboardEntries);
|
||||
PlayerPrefs.Save();
|
||||
}
|
||||
|
||||
|
@ -7,8 +7,8 @@ public class ScoreboardView : BaseView
|
||||
[SerializeField] private Button backButton;
|
||||
[SerializeField] private PlayerScoreboardCard cardPrefab;
|
||||
private VerticalLayoutGroup layoutGroup;
|
||||
private readonly Dictionary<string,PlayerScoreboardCard> playerCards = new Dictionary<string, PlayerScoreboardCard>();
|
||||
|
||||
//private readonly Dictionary<string,PlayerScoreboardCard> playerCards = new Dictionary<string, PlayerScoreboardCard>();
|
||||
private readonly List<PlayerScoreboardCard> playerCards = new List<PlayerScoreboardCard>();
|
||||
public override void Init()
|
||||
{
|
||||
base.Init();
|
||||
@ -29,27 +29,27 @@ public class ScoreboardView : BaseView
|
||||
|
||||
private void AddPlayerCard(PlayerScoreboardCardData cardData)
|
||||
{
|
||||
if (playerCards.ContainsKey(cardData.playerName))
|
||||
return;
|
||||
//if (playerCards.ContainsKey(cardData.playerName))
|
||||
// return;
|
||||
PlayerScoreboardCard playerScoreboardCard = Instantiate(cardPrefab);
|
||||
playerScoreboardCard.transform.SetParent(layoutGroup.transform, false);
|
||||
playerScoreboardCard.UpdateCard(cardData);
|
||||
playerCards.Add(cardData.playerName, playerScoreboardCard);
|
||||
playerCards.Add(playerScoreboardCard);
|
||||
}
|
||||
|
||||
public void RemovePlayerCard(string cardTag)
|
||||
{
|
||||
if (playerCards.ContainsKey(cardTag))
|
||||
{
|
||||
playerCards.TryGetValue(cardTag, out PlayerScoreboardCard playerScoreboardCard);
|
||||
playerScoreboardCard.gameObject.SetActive(false); //TODO: Pooling
|
||||
playerCards.Remove(cardTag);
|
||||
}
|
||||
//if (playerCards.ContainsKey(cardTag))
|
||||
//{
|
||||
//playerCards.TryGetValue(cardTag, out PlayerScoreboardCard playerScoreboardCard);
|
||||
//playerScoreboardCard.gameObject.SetActive(false); //TODO: Pooling
|
||||
//playerCards.Remove(cardTag);
|
||||
//}
|
||||
}
|
||||
|
||||
public void RefreshPlayerCard(PlayerScoreboardCardData cardData)
|
||||
{
|
||||
if (playerCards.TryGetValue(cardData.playerName, out PlayerScoreboardCard card))
|
||||
card.UpdateCard(cardData);
|
||||
//if (playerCards.TryGetValue(cardData.playerName, out PlayerScoreboardCard card))
|
||||
// card.UpdateCard(cardData);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user