aboutsummaryrefslogtreecommitdiff
path: root/td/Assets
diff options
context:
space:
mode:
Diffstat (limited to 'td/Assets')
-rw-r--r--td/Assets/Prefabs/Enemies/Enemy.prefabbin7968 -> 7968 bytes
-rw-r--r--td/Assets/Scenes/Level 1.unitybin115708 -> 121672 bytes
-rw-r--r--td/Assets/Scripts/EnemySpawner.cs8
-rw-r--r--td/Assets/Scripts/gameStats.cs39
-rw-r--r--td/Assets/Scripts/mainGUI.cs111
-rw-r--r--td/Assets/Scripts/player.cs14
6 files changed, 118 insertions, 54 deletions
diff --git a/td/Assets/Prefabs/Enemies/Enemy.prefab b/td/Assets/Prefabs/Enemies/Enemy.prefab
index 88dcbcb..212a232 100644
--- a/td/Assets/Prefabs/Enemies/Enemy.prefab
+++ b/td/Assets/Prefabs/Enemies/Enemy.prefab
Binary files differ
diff --git a/td/Assets/Scenes/Level 1.unity b/td/Assets/Scenes/Level 1.unity
index 1c880c7..d83c9e7 100644
--- a/td/Assets/Scenes/Level 1.unity
+++ b/td/Assets/Scenes/Level 1.unity
Binary files differ
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<Vector3> _waypoints = new List<Vector3>();
- int _next = 1;
- int _n = 0;
+ private List<Vector3> _waypoints = new List<Vector3>();
+ 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 <Text>();
_txtScore = _canvas.transform.Find ("playerScore").gameObject.GetComponent <Text>();
- _txtHp = _canvas.transform.Find ("playerHealth").gameObject.GetComponent <Text>();
+ _sldHp = _canvas.transform.Find("playerHealth").gameObject.GetComponent<Slider>();
}
- 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 <RectTransform> ();
/* Buttons */
@@ -38,6 +42,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> ();
if (_btnToggleSidebar != null) { _btnToggleSidebar.onClick.AddListener (toggleSidebarHandler); }
if (_btnPauseGame != null) { _btnPauseGame.onClick.AddListener (pauseGameHandler); }
if (_btnResumeGame != null) { _btnResumeGame.onClick.AddListener (btnResumeGameHandler); }
@@ -45,20 +51,28 @@ public class MainGui : MonoBehaviour {
if (_btnSettings != null) { _btnSettings.onClick.AddListener (btnSettingsHandler); }
if (_btnSettingsDiscard != null) { _btnSettingsDiscard.onClick.AddListener (btnSettingsDiscardHandler); }
if (_btnSettingsSave != null) { _btnSettingsSave.onClick.AddListener (btnSettingsSaveHandler); }
+ if (_btnGoMenu != null) { _btnGoMenu.onClick.AddListener (btnGoMenuHandler); }
+ 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>();
/* Set up initial states */
UpdateSidebarPosandBtn ();
_pnlMenu.SetActive (false);
_pnlSettings.SetActive (false);
+ _pnlGameOver.SetActive(false);
}
- void toggleSidebarHandler() {
+ private void toggleSidebarHandler() {
/* handler for btnToggleSidebar */
_sidebarExpanded = !_sidebarExpanded;
UpdateSidebarPosandBtn ();
}
- void pauseGameHandler() {
+ private void pauseGameHandler() {
/* handler for btnPauseGame */
_menuActive = true;
_pnlMenu.SetActive (_menuActive);
@@ -67,7 +81,7 @@ public class MainGui : MonoBehaviour {
_btnPauseGame.interactable = false;
}
- void btnResumeGameHandler() {
+ private void btnResumeGameHandler() {
/* handler for btnResumeGame */
_menuActive = false;
_pnlMenu.SetActive (_menuActive);
@@ -76,12 +90,12 @@ public class MainGui : MonoBehaviour {
_btnPauseGame.interactable = true;
}
- void btnExitGameHandler() {
+ private void btnExitGameHandler() {
/* handler for btnExitGame */
Application.Quit ();
}
- void btnSettingsHandler() {
+ private void btnSettingsHandler() {
/* handler for btnSettings */
_pnlMenu.SetActive (false);
_pnlSettings.SetActive (true);
@@ -91,7 +105,7 @@ public class MainGui : MonoBehaviour {
}
}
- void btnSettingsSaveHandler() {
+ private void btnSettingsSaveHandler() {
/* handler for btnSaveSettings */
_pnlMenu.SetActive (true);
_pnlSettings.SetActive (false);
@@ -99,13 +113,13 @@ public class MainGui : MonoBehaviour {
PlayerPrefs.SetInt ("developerMode", Convert.ToInt32(_pnlSettings.transform.Find ("developerEnabled").gameObject.GetComponent <Toggle>().isOn));
}
- void btnSettingsDiscardHandler() {
+ private void btnSettingsDiscardHandler() {
/* handler for btnSettingsDiscard */
_pnlMenu.SetActive (true);
_pnlSettings.SetActive (false);
}
- void UpdateSidebarPosandBtn() {
+ private void UpdateSidebarPosandBtn() {
/* update state of sidebar based on the expanded var */
if (_sidebarExpanded) {
_pnlSidebarTransform.localPosition = new Vector3 (_sidebarStates [1], 0f, 0f);
@@ -116,7 +130,15 @@ public class MainGui : MonoBehaviour {
}
}
- bool IntToBool(int input) {
+ private void btnGoMenuHandler() {
+ /* Handler for btnGoMenu */
+ }
+
+ private void btnGoRetryHandler() {
+ /* Handler for btnGoRetry */
+ }
+
+ private bool IntToBool(int input) {
/* Converts int to boolean */
if (input >= 1) {
return true;
@@ -125,4 +147,33 @@ public class MainGui : MonoBehaviour {
}
}
+ public void GameOverScreen(int score) {
+ /* Show game over screen */
+ bool newHighscore = false;
+ int highScore = 0;
+
+ if (PlayerPrefs.HasKey("highscore")) {
+ highScore = PlayerPrefs.GetInt("highscore");
+ if (score > highScore) {
+ newHighscore = true;
+ }
+ }
+
+ if (_sidebarExpanded) { toggleSidebarHandler(); }
+
+ /* Pause game */
+ Time.timeScale = 0.0F;
+ /* Activate panel */
+ _pnlGameOver.SetActive(true);
+ /* Set text, score */
+ _txtGoScore.text = "Score: " + score.ToString();
+ /* set text, highscore */
+ _txtGoHighScore.text = "Highscore: " + highScore.ToString();
+ /* set newHicgscore */
+ _txtGoNewHighScore.gameObject.SetActive(newHighscore);
+ /* Disable other onScreenButtons */
+ _btnToggleSidebar.interactable = false;
+ _btnPauseGame.interactable = false;
+ }
+
}
diff --git a/td/Assets/Scripts/player.cs b/td/Assets/Scripts/player.cs
index c421720..a8106d3 100644
--- a/td/Assets/Scripts/player.cs
+++ b/td/Assets/Scripts/player.cs
@@ -6,7 +6,8 @@ public class Player : MonoBehaviour {
public int InitialHealth;
public int StartingMoney;
-
+ public MainGui MainGui;
+
private GameObject[] _towers;
private int _playerMoney;
private int _playerScore;
@@ -16,6 +17,7 @@ public class Player : MonoBehaviour {
/* This method initializes the player class */
_playerMoney = StartingMoney;
_playerHealth = InitialHealth;
+ InvokeRepeating ("GameStateWatcher", 0f, 0.5f);
}
#region stats
@@ -43,9 +45,9 @@ public class Player : MonoBehaviour {
return _playerHealth;
}
- public float HealthAsPercentage()
+ public int HealthAsPercentage()
{
- return InitialHealth / _playerHealth;
+ return Mathf.RoundToInt((InitialHealth * _playerHealth) / 100.0f); // Basic percentage calc...
}
public void DecreaseHealth(int hp) {
@@ -59,4 +61,10 @@ public class Player : MonoBehaviour {
script.Player = this;
}
+ public void GameStateWatcher() {
+ if (_playerHealth <= 0) {
+ MainGui.GameOverScreen(_playerScore);
+ }
+ }
+
}