-
-
Save kihira/81d9813bdf4c6a2f4444 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public struct ScoreData | |
{ | |
public string name; | |
public int score; | |
public float mutliplyer; | |
public ScoreData(string name, int score, float mutliplyer) | |
{ | |
this.name = name; | |
this.score = score; | |
this.mutliplyer = mutliplyer; | |
} | |
} | |
// Defines how much score is added per frame to the display | |
public int updateRate = 2; | |
public GameObject scoreInfoPrefab; | |
private int score; | |
private int scoreToAdd; | |
private List<ScoreData> scores; | |
private List<GameObject> scoreDisplay; | |
/** | |
* MonoBehaviour Methods | |
**/ | |
void Start () | |
{ | |
scores = new List<ScoreData>(); | |
scoreDisplay = new List<GameObject>(); | |
} | |
void Update () | |
{ | |
if (scoreToAdd <= 0) return; | |
scoreToAdd -= updateRate; | |
score += updateRate; | |
// Update text display for the scoreData | |
gameObject.GetComponent<Text>().text = score.ToString().PadLeft(8, '0'); | |
} | |
void OnDestroy() | |
{ | |
// TODO For now, submit high score on scene change (which is when this is destroyed) | |
SubmitScore(); | |
} | |
/** | |
* Publically accessible methods | |
**/ | |
public void AddScore(ScoreData scoreData) | |
{ | |
// Check if player has not taken damage and if so, set multiplyer to 2x | |
PlayerPlane player = GameObject.FindWithTag("Player").GetComponent<PlayerPlane>(); | |
if (player.maxPlayerHealth == player.playerHealth) | |
{ | |
scoreData.mutliplyer = 2; | |
} | |
// Store score data and add score to score counter | |
scores.Add(scoreData); | |
scoreToAdd += (int)(scoreData.score * scoreData.mutliplyer); | |
// Add text on screen for score | |
CreateScoreText(scoreData); | |
Debug.Log("Add score:" + scoreData); | |
} | |
public void RemoveScoreInfo(GameObject gameObject) | |
{ | |
scoreDisplay.Remove(gameObject); | |
} | |
/// <summary> | |
/// Creates a GameObject on the In-Game Canvas that provides current score information | |
/// </summary> | |
/// <param name="scoreData">The ScoreData to parse and display</param> | |
/// <returns></returns> | |
private GameObject CreateScoreText(ScoreData scoreData) | |
{ | |
GameObject scoreInfo = Instantiate(scoreInfoPrefab); | |
RectTransform rectTransform = scoreInfo.GetComponent<RectTransform>(); | |
// Set the canvas as parent | |
rectTransform.SetParent(GameObject.Find("In-Game Canvas").GetComponent<RectTransform>(), false); | |
// Format text into columns and styles | |
scoreInfo.GetComponent<Text>().text = string.Format("{0, -15} {1,6:##.##x} {2,-7:#####}", scoreData.name, scoreData.mutliplyer, scoreData.score); | |
// Offset below other scores being displayed if needs be | |
if (scoreDisplay.Count > 0) | |
{ | |
rectTransform.anchoredPosition = new Vector2(rectTransform.anchoredPosition.x, scoreDisplay[scoreDisplay.Count-1].GetComponent<RectTransform>().anchoredPosition.y-17); | |
} | |
scoreDisplay.Add(scoreInfo); | |
return gameObject; | |
} | |
/// <summary> | |
/// Adds the current score to the local highscore data string | |
/// </summary> | |
private void SubmitScore() | |
{ | |
// Make sure all the score is added to the main variable | |
score += scoreToAdd; | |
scoreToAdd = 0; | |
// Due to the limitations of PlayerPrefs not supporting lists, we'll conjoin everything into a semi colon seperated string | |
// In the future, a custom serialiser could be written which can store more data in a nicer fashion | |
// Scores are seperate by semi colons, score data is seperated by colons. | |
// scoreValue:date:username; | |
String scores = PlayerPrefs.GetString("HighScores", ""); | |
scores += String.Format("{0}:{1}:{2};", score, DateTime.UtcNow.ToFileTimeUtc(), "AAA"); | |
PlayerPrefs.SetString("HighScores", scores); | |
PlayerPrefs.Save(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment