diff options
author | Jakob Stendahl <jakob.stendahl@outlook.com> | 2017-10-13 22:11:33 +0200 |
---|---|---|
committer | Jakob Stendahl <jakob.stendahl@outlook.com> | 2017-10-13 22:11:33 +0200 |
commit | d77cd51742cb760f5f2e8f5f3c283e311ffffe1c (patch) | |
tree | a60aaccbbe51c2202e596c22d3741370fdc357d2 | |
parent | 112a91b7084a5365291c712fabcb8ceeb4c644ce (diff) | |
download | TD-d77cd51742cb760f5f2e8f5f3c283e311ffffe1c.tar.gz TD-d77cd51742cb760f5f2e8f5f3c283e311ffffe1c.zip |
Added new method for pausing the game
-rw-r--r-- | td/Assets/Prefabs/Enemies/Enemy.prefab | bin | 7968 -> 7968 bytes | |||
-rw-r--r-- | td/Assets/Scenes/Level 1.unity | bin | 121672 -> 122396 bytes | |||
-rw-r--r-- | td/Assets/Scripts/Enemy.cs | 2 | ||||
-rw-r--r-- | td/Assets/Scripts/EnemySpawner.cs | 1 | ||||
-rw-r--r-- | td/Assets/Scripts/cameraHandler.cs | 3 | ||||
-rw-r--r-- | td/Assets/Scripts/mainGUI.cs | 20 | ||||
-rw-r--r-- | td/Assets/Scripts/player.cs | 23 | ||||
-rw-r--r-- | td/Assets/Scripts/waveSpawner.cs | 3 |
8 files changed, 40 insertions, 12 deletions
diff --git a/td/Assets/Prefabs/Enemies/Enemy.prefab b/td/Assets/Prefabs/Enemies/Enemy.prefab Binary files differindex 212a232..ed40756 100644 --- a/td/Assets/Prefabs/Enemies/Enemy.prefab +++ b/td/Assets/Prefabs/Enemies/Enemy.prefab diff --git a/td/Assets/Scenes/Level 1.unity b/td/Assets/Scenes/Level 1.unity Binary files differindex d83c9e7..e93598e 100644 --- a/td/Assets/Scenes/Level 1.unity +++ b/td/Assets/Scenes/Level 1.unity diff --git a/td/Assets/Scripts/Enemy.cs b/td/Assets/Scripts/Enemy.cs index 3074003..20436db 100644 --- a/td/Assets/Scripts/Enemy.cs +++ b/td/Assets/Scripts/Enemy.cs @@ -17,6 +17,8 @@ public class Enemy : MonoBehaviour { private int _waypointNum = -1; // Using minus one so that first addition returns 0, first element in array void Update () { + if (Player.GameIsPaused()) { return; } // This ensures that the game stays paused + if ( (transform.position == _waypointPos && _waypointNum + 1 < Waypoints.Count) || _waypointNum == -1) { _waypointNum++; _waypointPos = new Vector3 (Waypoints [_waypointNum].x, 0.483f, Waypoints [_waypointNum].z); diff --git a/td/Assets/Scripts/EnemySpawner.cs b/td/Assets/Scripts/EnemySpawner.cs index 5b0249d..a1a6b1f 100644 --- a/td/Assets/Scripts/EnemySpawner.cs +++ b/td/Assets/Scripts/EnemySpawner.cs @@ -28,6 +28,7 @@ public class EnemySpawner : MonoBehaviour { } void Update () { + if (Player.GameIsPaused()) { return; } // This ensures that the game stays paused _n++; if (_n == _next) { diff --git a/td/Assets/Scripts/cameraHandler.cs b/td/Assets/Scripts/cameraHandler.cs index 1655509..7c2f75f 100644 --- a/td/Assets/Scripts/cameraHandler.cs +++ b/td/Assets/Scripts/cameraHandler.cs @@ -11,6 +11,8 @@ public class CameraHandler : MonoBehaviour { public static readonly float[] BoundsY = new float[]{-5f, 10f}; public static readonly float[] BoundsZ = new float[]{-8f, 8f}; public static readonly float[] ZoomBounds = new float[]{1f, 5f}; + [Header("Scripting vars")] + public Player Player; // Reference to the player object, should be set in designer private Camera _cam; @@ -26,6 +28,7 @@ public class CameraHandler : MonoBehaviour { } void Update() { + if (Player.GameIsPaused()) { return; } // If there's an open menu, or the clicker is being pressed, ignore the touch. /* if (GameManager.Instance.MenuManager.HasOpenMenu || GameManager.Instance.BitSpawnManager.IsSpawningBits) { diff --git a/td/Assets/Scripts/mainGUI.cs b/td/Assets/Scripts/mainGUI.cs index 35d3948..26ec78b 100644 --- a/td/Assets/Scripts/mainGUI.cs +++ b/td/Assets/Scripts/mainGUI.cs @@ -1,8 +1,12 @@ using System; using UnityEngine.UI; +using UnityEngine.SceneManagement; using UnityEngine; public class MainGui : MonoBehaviour { + [Header("Scripting vars")] + public Player Player; // Reference to the player object, should be set in designer + private GameObject _pnlMenu; private GameObject _pnlSidebar; private GameObject _pnlSettings; @@ -42,8 +46,8 @@ public class MainGui : MonoBehaviour { _btnSettings = _pnlMenu.transform.Find ("settings").gameObject.GetComponent <Button> (); _btnSettingsDiscard = _pnlSettings.transform.Find ("discardChanges").gameObject.GetComponent <Button> (); _btnSettingsSave = _pnlSettings.transform.Find ("saveChanges").gameObject.GetComponent <Button> (); - _btnGoMenu = _pnlGameOver.transform.Find ("menu").gameObject.GetComponent <Button> (); - _btnGoRetry = _pnlGameOver.transform.Find ("restart").gameObject.GetComponent <Button> (); + _btnGoMenu = _pnlGameOver.transform.Find ("GameOver").transform.Find("menu").gameObject.GetComponent <Button> (); + _btnGoRetry = _pnlGameOver.transform.Find ("GameOver").transform.Find("restart").gameObject.GetComponent <Button> (); if (_btnToggleSidebar != null) { _btnToggleSidebar.onClick.AddListener (toggleSidebarHandler); } if (_btnPauseGame != null) { _btnPauseGame.onClick.AddListener (pauseGameHandler); } if (_btnResumeGame != null) { _btnResumeGame.onClick.AddListener (btnResumeGameHandler); } @@ -55,9 +59,9 @@ public class MainGui : MonoBehaviour { if (_btnGoRetry != null) { _btnGoRetry.onClick.AddListener (btnGoRetryHandler); } /* Text */ - _txtGoScore = _pnlGameOver.transform.Find("score").gameObject.GetComponent<Text>(); - _txtGoHighScore = _pnlGameOver.transform.Find("highScore").gameObject.GetComponent<Text>(); - _txtGoNewHighScore = _pnlGameOver.transform.Find("newHighscore").gameObject.GetComponent<Text>(); + _txtGoScore = _pnlGameOver.transform.Find ("GameOver").transform.Find("score").gameObject.GetComponent<Text>(); + _txtGoHighScore = _pnlGameOver.transform.Find ("GameOver").transform.Find("highScore").gameObject.GetComponent<Text>(); + _txtGoNewHighScore = _pnlGameOver.transform.Find ("GameOver").transform.Find("newHighscore").gameObject.GetComponent<Text>(); /* Set up initial states */ UpdateSidebarPosandBtn (); @@ -79,6 +83,7 @@ public class MainGui : MonoBehaviour { Time.timeScale = 0.0F; _btnToggleSidebar.interactable = false; _btnPauseGame.interactable = false; + Player.PauseGame(); } private void btnResumeGameHandler() { @@ -88,6 +93,7 @@ public class MainGui : MonoBehaviour { Time.timeScale = 1.0F; _btnToggleSidebar.interactable = true; _btnPauseGame.interactable = true; + Player.ResumeGame(); } private void btnExitGameHandler() { @@ -136,6 +142,8 @@ public class MainGui : MonoBehaviour { private void btnGoRetryHandler() { /* Handler for btnGoRetry */ + SceneManager.LoadScene(SceneManager.GetActiveScene().name); + Time.timeScale = 1.0F; } private bool IntToBool(int input) { @@ -162,7 +170,7 @@ public class MainGui : MonoBehaviour { if (_sidebarExpanded) { toggleSidebarHandler(); } /* Pause game */ - Time.timeScale = 0.0F; + Player.PauseGame(); /* Activate panel */ _pnlGameOver.SetActive(true); /* Set text, score */ diff --git a/td/Assets/Scripts/player.cs b/td/Assets/Scripts/player.cs index a8106d3..cb55357 100644 --- a/td/Assets/Scripts/player.cs +++ b/td/Assets/Scripts/player.cs @@ -1,13 +1,12 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; +using UnityEngine; public class Player : MonoBehaviour { public int InitialHealth; public int StartingMoney; public MainGui MainGui; - + + private int _gameState; private GameObject[] _towers; private int _playerMoney; private int _playerScore; @@ -18,6 +17,7 @@ public class Player : MonoBehaviour { _playerMoney = StartingMoney; _playerHealth = InitialHealth; InvokeRepeating ("GameStateWatcher", 0f, 0.5f); + _gameState = 1; } #region stats @@ -67,4 +67,19 @@ public class Player : MonoBehaviour { } } + public bool GameIsPaused() { + if (_gameState == 0) { return true; } + return false; + } + + public void PauseGame() { + Time.timeScale = 0.0F; + _gameState = 0; + } + + public void ResumeGame() { + Time.timeScale = 1.0F; + _gameState = 1; + } + } diff --git a/td/Assets/Scripts/waveSpawner.cs b/td/Assets/Scripts/waveSpawner.cs index f79fb90..1d361ef 100644 --- a/td/Assets/Scripts/waveSpawner.cs +++ b/td/Assets/Scripts/waveSpawner.cs @@ -21,8 +21,7 @@ public class WaveSpawner : MonoBehaviour { void Update () { - if (EnemiesAlive > 0) - { + if (EnemiesAlive > 0) { return; } |