From 112a91b7084a5365291c712fabcb8ceeb4c644ce Mon Sep 17 00:00:00 2001 From: Jakob Stendahl Date: Sun, 8 Oct 2017 22:43:20 +0200 Subject: Added game over screen --- td/Assets/Scripts/EnemySpawner.cs | 8 +-- td/Assets/Scripts/gameStats.cs | 39 ++++++++------ td/Assets/Scripts/mainGUI.cs | 111 +++++++++++++++++++++++++++----------- td/Assets/Scripts/player.cs | 14 +++-- 4 files changed, 118 insertions(+), 54 deletions(-) (limited to 'td/Assets/Scripts') diff --git a/td/Assets/Scripts/EnemySpawner.cs b/td/Assets/Scripts/EnemySpawner.cs index 7ee8680..5b0249d 100644 --- a/td/Assets/Scripts/EnemySpawner.cs +++ b/td/Assets/Scripts/EnemySpawner.cs @@ -9,13 +9,13 @@ public class EnemySpawner : MonoBehaviour { public Enemy EnemyPrefab; public Transform PathWay; [Header("Scripting vars")] - public Player Player; // Reference to the player object, should be set when instantiating + public Player Player; // Reference to the player object, should be set in designer private Transform _parentObject; - List _waypoints = new List(); - int _next = 1; - int _n = 0; + private List _waypoints = new List(); + private int _next = 1; + private int _n = 0; void Awake() { foreach (Transform child in PathWay) { diff --git a/td/Assets/Scripts/gameStats.cs b/td/Assets/Scripts/gameStats.cs index 7c305fa..731c276 100644 --- a/td/Assets/Scripts/gameStats.cs +++ b/td/Assets/Scripts/gameStats.cs @@ -6,22 +6,23 @@ using UnityEngine; public class GameStats : MonoBehaviour { public Player Player; - GameObject _canvas; - Text _txtMoney; - Text _txtScore; - Text _txtHp; - int _displayedScore; - int _displayedMoney; - int _displayedHealth; - - void Start() { + private GameObject _canvas; + private Text _txtMoney; + private Text _txtScore; + private Text _txtHp; + private Slider _sldHp; + private int _displayedScore; + private int _displayedMoney; + private int _displayedHealth; + + private void Start() { _canvas = transform.GetChild (0).gameObject; _txtMoney = _canvas.transform.Find ("playerMoney").gameObject.GetComponent (); _txtScore = _canvas.transform.Find ("playerScore").gameObject.GetComponent (); - _txtHp = _canvas.transform.Find ("playerHealth").gameObject.GetComponent (); + _sldHp = _canvas.transform.Find("playerHealth").gameObject.GetComponent(); } - void Update () { + private void Update () { if (Player.Money () != _displayedMoney) { _displayedMoney = Player.Money (); @@ -33,23 +34,27 @@ public class GameStats : MonoBehaviour { UpdateScore (_displayedScore); } - if (Player.Health () != _displayedHealth) { - _displayedHealth = Player.Health (); + if (Mathf.RoundToInt(Player.HealthAsPercentage()) != Mathf.RoundToInt(_displayedHealth)) { + _displayedHealth = Player.HealthAsPercentage(); UpdateHealth (_displayedHealth); + + if (_displayedHealth <= 10) { + _txtScore.color = Color.red; + } } } - void UpdateScore(int newScore) { + private void UpdateScore(int newScore) { _txtScore.text = ("Score: " + newScore.ToString ()); } - void UpdateMoney(int newMoney) { + private void UpdateMoney(int newMoney) { _txtMoney.text = ("Money: " + newMoney.ToString () + "$"); } - void UpdateHealth(int newHp) { - _txtHp.text = ("HP: " + newHp.ToString ()); + private void UpdateHealth(int newHp) { + _sldHp.value = newHp; } } diff --git a/td/Assets/Scripts/mainGUI.cs b/td/Assets/Scripts/mainGUI.cs index 1c18a17..35d3948 100644 --- a/td/Assets/Scripts/mainGUI.cs +++ b/td/Assets/Scripts/mainGUI.cs @@ -1,33 +1,37 @@ using System; -using System.Collections; -using System.Collections.Generic; using UnityEngine.UI; using UnityEngine; public class MainGui : MonoBehaviour { - - GameObject _pnlMenu; - GameObject _pnlSidebar; - GameObject _pnlSettings; - RectTransform _pnlSidebarTransform; - Button _btnToggleSidebar; - Button _btnPauseGame; - Button _btnResumeGame; - Button _btnExitGame; - Button _btnSettings; - Button _btnSettingsDiscard; - Button _btnSettingsSave; - - bool _sidebarExpanded; - float[] _sidebarStates = new float[2] {0f, -202.4f}; // The x position of the sidebar expanded or collapsed - - bool _menuActive; - - void Awake() { + private GameObject _pnlMenu; + private GameObject _pnlSidebar; + private GameObject _pnlSettings; + private GameObject _pnlGameOver; + private RectTransform _pnlSidebarTransform; + private Button _btnToggleSidebar; + private Button _btnPauseGame; + private Button _btnResumeGame; + private Button _btnExitGame; + private Button _btnSettings; + private Button _btnSettingsDiscard; + private Button _btnSettingsSave; + private Button _btnGoRetry; + private Button _btnGoMenu; + private Text _txtGoScore; + private Text _txtGoHighScore; + private Text _txtGoNewHighScore; + + private bool _sidebarExpanded; + private readonly float[] _sidebarStates = new float[2] {0f, -202.4f}; // The x position of the sidebar expanded or collapsed + + private bool _menuActive; + + private void Awake() { /* Panels */ _pnlMenu = transform.Find ("menu").gameObject; _pnlSidebar = transform.Find ("sidebarWrapper").gameObject; _pnlSettings = transform.Find ("settings").gameObject; + _pnlGameOver = transform.Find("GameOver").gameObject; _pnlSidebarTransform = _pnlSidebar.GetComponent (); /* Buttons */ @@ -38,6 +42,8 @@ public class MainGui : MonoBehaviour { _btnSettings = _pnlMenu.transform.Find ("settings").gameObject.GetComponent