using System.Collections.Generic; using TMPro; using UnityEngine; using UnityEngine.UI; using System.Linq; using System.Collections.ObjectModel; public class ScoreManager : MonoBehaviour { private int playerHeight; private int highestHeightAchieved = 0; private int itemScore = 0; private int boostAmount = 0; private int tempBoostAmount = 0, pathSwitchBoostAmount = 5, pathSwitchBoostBonusTime = 0, timesPathSwitched = 0; private int currentScore; private int boostScore = 0; private int highestScoreAchieved; private float timeSincePathSwitch = 0.0f, pathSwitchBuffBaseTime = 5.0f; private bool disableScoreSaving; private bool wentOver100InRun; //set true after going over 100. Resets on each new run. private bool pathSwitchBoostActive; private string currentUsername = ""; [SerializeField] private Color32 normalScoreDefaultColor, normalScoreFadedColor, highScoreDefaultColor, highScoreFadedColor; private List<string> savedNames = new List<string>(); private List<float> savedScores = new List<float>(); private List<float> savedTimes = new List<float>(); private List<GameObject> entries = new List<GameObject>(); private GameObject deathPanelScoreList; private GameObject playerObject; private GameObject scoreBoardList; public GameObject scoreBoardEntryPrefab; public GameObject emptyListNotification; private SessionTimer sessionT; private SoundManager soundMgr; private AchievementSystem achSys; private void Start() { deathPanelScoreList = GUIManager.GetObject(9); playerObject = ObjectManager.GetObject(0); scoreBoardList = GUIManager.GetObject(14); sessionT = GetComponent<SessionTimer>(); soundMgr = GetComponent<SoundManager>(); achSys = GetComponent<AchievementSystem>(); GUIManager.SetScoreBoostTextState(false); } private void Update() { playerHeight = Mathf.RoundToInt(playerObject.transform.position.y + 3.5f); if (playerHeight > highestHeightAchieved) { highestHeightAchieved = playerHeight; boostScore += boostAmount + tempBoostAmount; if ((boostAmount + tempBoostAmount) > 10) achSys.SetMaxScoreMultiplier(boostAmount + tempBoostAmount); currentScore = (highestHeightAchieved + itemScore + boostScore); GUIManager.ActivateScoreBurstEffect(currentScore, 1 + boostAmount + tempBoostAmount, 0); soundMgr.PlayScoreIncreaseSound(); UpdateScoreGUI(); achSys.SetMaxScore(currentScore); if (currentScore > 100 && !wentOver100InRun) { achSys.IncrementScoreAboveOneHundred(); wentOver100InRun = true; } if (currentScore > highestScoreAchieved && !disableScoreSaving) highestScoreAchieved = currentScore; } if (pathSwitchBoostActive) { timeSincePathSwitch += Time.deltaTime; if (timeSincePathSwitch >= (pathSwitchBuffBaseTime + pathSwitchBoostBonusTime)) { timeSincePathSwitch = 0.0f; RemovePathSwitchBoost(); } } if (entries.Count == 0 && !emptyListNotification.activeSelf) emptyListNotification.SetActive(true); if (entries.Count != 0 && emptyListNotification.activeSelf) emptyListNotification.SetActive(false); } public void SetScoreSavingState(bool newState) { disableScoreSaving = newState; } public int GetCurrentScore() { return currentScore; } public int GetPlayerHeight() { return playerHeight; } public int GetHighestHeight() { return highestHeightAchieved; } public int GetHighestScoreAchieved() { return highestScoreAchieved; } public void SetCurrentUserName(TextMeshProUGUI userNameText) { currentUsername = userNameText.text; } public void SetBoostAmount(int amount) { //based on boost amount, change the score text for particles to match the change boostAmount = amount; UpdateScoreGUI(); } public void ActivatePathSwitchBoost() { if (!pathSwitchBoostActive) { timesPathSwitched++; pathSwitchBoostBonusTime = timesPathSwitched * 2; pathSwitchBoostActive = true; SetTempBoostAmount(pathSwitchBoostAmount); } } public void RemovePathSwitchBoost() { pathSwitchBoostActive = false; SetTempBoostAmount(-pathSwitchBoostAmount); } public void SetTempBoostAmount(int amount) { tempBoostAmount += amount; UpdateScoreGUI(); } public void IncrementScore(int amt) //used to increment score when picking up coins { itemScore += amt; currentScore = ((highestHeightAchieved + itemScore + boostScore)); GUIManager.ActivateScoreBurstEffect(currentScore, amt, 1); soundMgr.PlayScoreIncreaseSound(); UpdateScoreGUI(); } public void UpdateScoreGUI() { GUIManager.ChangeScoreText(currentScore); if (boostAmount > 0 || tempBoostAmount > 0) { GUIManager.SetScoreBoostTextState(true); GUIManager.ChangeScoreBoostText(boostAmount + tempBoostAmount + 1); } else { GUIManager.SetScoreBoostTextState(false); } } public void ResetScore() { sessionT.ResetTimer(); currentScore = 0; boostScore = 0; itemScore = 0; highestHeightAchieved = 0; playerHeight = 0; tempBoostAmount = 0; wentOver100InRun = false; pathSwitchBoostActive = false; timesPathSwitched = 0; timeSincePathSwitch = 0.0f; UpdateScoreGUI(); } public void AddScoreToBoard() { if (!disableScoreSaving) { GameObject entry = Instantiate(scoreBoardEntryPrefab as GameObject, scoreBoardList.transform); entries.Add(entry); EntryInfo info = entry.GetComponent<EntryInfo>(); double timeDouble = sessionT.GetSessionTime(); if (currentUsername != "") info.SetUserName(currentUsername); else info.SetUserName("User"); info.SetScore(currentScore.ToString()); info.SetTime(System.Math.Round(timeDouble, 2).ToString()); if (currentUsername != "") savedNames.Add(currentUsername); else savedNames.Add("User"); savedScores.Add(currentScore); savedTimes.Add(sessionT.GetSessionTime()); SaveManager.SaveScoreBoardEntries(savedNames, savedScores, savedTimes); HighlightHighestScore(); } } public void AddSavedScoresToBoard(List<string> newNames, List<float> newScores, List<float> newTimes) { for (int i = 0; i < newScores.Count; i++) { GameObject entry = Instantiate(scoreBoardEntryPrefab as GameObject, scoreBoardList.transform); entries.Add(entry); EntryInfo info = entry.GetComponent<EntryInfo>(); double timeDouble = newTimes[i]; info.SetUserName(newNames[i]); info.SetScore(newScores[i].ToString()); info.SetTime(System.Math.Round(timeDouble, 2).ToString()); savedNames.Add(newNames[i]); savedScores.Add(newScores[i]); savedTimes.Add(newTimes[i]); } HighlightHighestScore(); } public void HighlightHighestScore() { if (entries.Count > 0) { int highestScore = -1; int highScoreIndex = -1; for (int i = 0; i < entries.Count; i++) { int entryScore = entries[i].GetComponent<EntryInfo>().GetScore(); if (entryScore > highestScore) { highestScore = entryScore; highScoreIndex = i; highestScoreAchieved = highestScore; } else { //set color for all other scores entries[i].GetComponent<Image>().color = normalScoreDefaultColor; entries[i].GetComponent<MouseOverFade>().fadedColor = normalScoreFadedColor; entries[i].GetComponent<MouseOverFade>().SetDefaultColor(normalScoreDefaultColor); } } //set color for high score entries[highScoreIndex].GetComponent<Image>().color = highScoreDefaultColor; entries[highScoreIndex].GetComponent<MouseOverFade>().fadedColor = highScoreFadedColor; entries[highScoreIndex].GetComponent<MouseOverFade>().SetDefaultColor(highScoreDefaultColor) ; } } public void SortScoreboardByUsername() { List<string> entryNames = new List<string>(); List<GameObject> newEntriesList = new List<GameObject>(); for (int i = 0; i < entries.Count; i++) { entryNames.Add(entries[i].GetComponent<EntryInfo>().GetName()); } entryNames.Sort(); int x = 0; for (int i = 0; i < entries.Count; i++) { while (entries[x].GetComponent<EntryInfo>().GetName() != entryNames[i]) { x++; } newEntriesList.Add(entries[x]); x = 0; } entries = newEntriesList; for (int i = 0; i < entries.Count; i++) { entries[i].transform.SetSiblingIndex(i); } } public void SortScoreboardByScore() { List<int> entryScores = new List<int>(); List<GameObject> newEntriesList = new List<GameObject>(); for (int i = 0; i < entries.Count; i++) { entryScores.Add(entries[i].GetComponent<EntryInfo>().GetScore()); } entryScores.Sort(); int x = 0; for (int i = 0; i < entries.Count; i++) { while (entries[x].GetComponent<EntryInfo>().GetScore() != entryScores[i]) { x++; } newEntriesList.Add(entries[x]); x = 0; } entries = newEntriesList; for (int i = 0; i < entries.Count; i++) { entries[i].transform.SetSiblingIndex(i); } } public void SortScoreboardByTime() { List<double> entryTimes = new List<double>(); List<GameObject> newEntriesList = new List<GameObject>(); for (int i = 0; i < entries.Count; i++) { entryTimes.Add(entries[i].GetComponent<EntryInfo>().GetTime()); } entryTimes.Sort(); int x = 0; for (int i = 0; i < entries.Count; i++) { while (entries[x].GetComponent<EntryInfo>().GetTime() != entryTimes[i]) { x++; } newEntriesList.Add(entries[x]); x = 0; } entries = newEntriesList; for (int i = 0; i < entries.Count; i++) { entries[i].transform.SetSiblingIndex(i); } } public void ClearScoreboardEntries() { for (int i = entries.Count - 1; i >= 0; i--) { Destroy(entries[i]); } entries.Clear(); savedNames.Clear(); savedScores.Clear(); savedTimes.Clear(); SaveManager.DeleteScoreboardEntries(); } public void SetDeathPanelInfo() { Transform[] children = deathPanelScoreList.GetComponentsInChildren<Transform>(); for (int i = 0; i < children.Length; i++) { string childName = children[i].gameObject.name; switch (childName) { case "Distance Value": children[i].gameObject.GetComponent<TextMeshProUGUI>().text = highestHeightAchieved.ToString(); break; case "Item Value": children[i].gameObject.GetComponent<TextMeshProUGUI>().text = itemScore.ToString(); break; case "Boost Value": children[i].gameObject.GetComponent<TextMeshProUGUI>().text = boostScore.ToString(); break; case "Total Value": children[i].gameObject.GetComponent<TextMeshProUGUI>().text = currentScore.ToString(); break; case "High Score Value": children[i].gameObject.GetComponent<TextMeshProUGUI>().text = highestScoreAchieved.ToString(); break; } } } }