From fae01da58dbea66af402807d7409fa53602f71fa Mon Sep 17 00:00:00 2001 From: Jakob Stendahl Date: Sat, 14 Oct 2017 22:07:19 +0200 Subject: Finished wavesystem. Should improve graph for how many enemies should spawn each time --- td/.idea/.idea.td/.idea/workspace.xml | 594 +++++++++++++++++++++++---------- td/Assets/Prefabs/Enemies/Enemy.prefab | Bin 7968 -> 7968 bytes td/Assets/Scenes/Level 1.unity | Bin 122396 -> 123228 bytes td/Assets/Scripts/Enemy.cs | 2 + td/Assets/Scripts/EnemySpawner.cs | 49 --- td/Assets/Scripts/EnemySpawner.cs.meta | 12 - td/Assets/Scripts/Projectile.cs | 1 + td/Assets/Scripts/tower.cs | 1 - td/Assets/Scripts/waveSpawner.cs | 92 ++++- 9 files changed, 504 insertions(+), 247 deletions(-) delete mode 100644 td/Assets/Scripts/EnemySpawner.cs delete mode 100644 td/Assets/Scripts/EnemySpawner.cs.meta (limited to 'td') diff --git a/td/.idea/.idea.td/.idea/workspace.xml b/td/.idea/.idea.td/.idea/workspace.xml index 2208f9a..c304f2f 100644 --- a/td/.idea/.idea.td/.idea/workspace.xml +++ b/td/.idea/.idea.td/.idea/workspace.xml @@ -2,13 +2,8 @@ - - - - - + + + toggle + + @@ -128,13 +196,15 @@ @@ -164,6 +234,7 @@ + @@ -176,11 +247,42 @@ + + + + + + + + + + + + + + + + + + - @@ -271,7 +373,7 @@ false - + @@ -297,12 +399,13 @@ - + + - @@ -320,27 +423,37 @@ - + - + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + @@ -404,21 +596,26 @@ - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + @@ -427,6 +624,7 @@ + @@ -442,21 +640,26 @@ - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + @@ -480,21 +683,23 @@ - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + @@ -503,6 +708,7 @@ + @@ -538,6 +744,7 @@ + @@ -569,13 +776,6 @@ - - - - - - - @@ -583,103 +783,149 @@ - + - - + + - + - - + + - + - - + + - + - - + + - + - - + + - + - - + + - + - - + + + + + + + + + + + + + + + + + + + + + + + + - - + + - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + - + - - + + + + + + + + + + - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/td/Assets/Prefabs/Enemies/Enemy.prefab b/td/Assets/Prefabs/Enemies/Enemy.prefab index ed40756..38900d3 100644 Binary files a/td/Assets/Prefabs/Enemies/Enemy.prefab and b/td/Assets/Prefabs/Enemies/Enemy.prefab differ diff --git a/td/Assets/Scenes/Level 1.unity b/td/Assets/Scenes/Level 1.unity index e93598e..23478b5 100644 Binary files a/td/Assets/Scenes/Level 1.unity and b/td/Assets/Scenes/Level 1.unity differ diff --git a/td/Assets/Scripts/Enemy.cs b/td/Assets/Scripts/Enemy.cs index 20436db..4d15b64 100644 --- a/td/Assets/Scripts/Enemy.cs +++ b/td/Assets/Scripts/Enemy.cs @@ -10,6 +10,7 @@ public class Enemy : MonoBehaviour { public int InitialHp; // HealthPoints public int Damage; public List Waypoints; // Pathway waypoints, should be set by the spawner + [Header("Scripting vars")] public Player Player; // Reference to the player object, should be set when instantiating @@ -29,6 +30,7 @@ public class Enemy : MonoBehaviour { // Selfdestruct if object reached the end if (_waypointNum + 1 >= Waypoints.Count) { + WaveSpawner.EnemiesAlive--; Player.DecreaseHealth (Damage); Destroy (gameObject); return; diff --git a/td/Assets/Scripts/EnemySpawner.cs b/td/Assets/Scripts/EnemySpawner.cs deleted file mode 100644 index a1a6b1f..0000000 --- a/td/Assets/Scripts/EnemySpawner.cs +++ /dev/null @@ -1,49 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class EnemySpawner : MonoBehaviour { - /* This is a class that spawns an enemy with a random interval - * it is not very good, but is what it needs to be for testing purposes */ - // TODO Add wave system with increasing difficulty - public Enemy EnemyPrefab; - public Transform PathWay; - [Header("Scripting vars")] - public Player Player; // Reference to the player object, should be set in designer - - private Transform _parentObject; - - private List _waypoints = new List(); - private int _next = 1; - private int _n = 0; - - void Awake() { - foreach (Transform child in PathWay) { - _waypoints.Add (child.position); - } - } - - void Start() { - _parentObject = transform.Find ("enemies").gameObject.GetComponent (); - } - - void Update () { - if (Player.GameIsPaused()) { return; } // This ensures that the game stays paused - _n++; - - if (_n == _next) { - _n = 0; - _next = (int)Random.Range (50, 400); - - Enemy newEnemy = Instantiate (EnemyPrefab, new Vector3(0, 0, 0), Quaternion.identity, _parentObject); - Enemy script = newEnemy.GetComponent (); - Transform transform = newEnemy.GetComponent (); - - script.Waypoints = _waypoints; - script.Speed = Random.Range (0.3f, 1.2f); - script.Player = Player; - transform.position = new Vector3 (0.93f, 0.483f, 0f); - } - - } -} diff --git a/td/Assets/Scripts/EnemySpawner.cs.meta b/td/Assets/Scripts/EnemySpawner.cs.meta deleted file mode 100644 index 3fe504e..0000000 --- a/td/Assets/Scripts/EnemySpawner.cs.meta +++ /dev/null @@ -1,12 +0,0 @@ -fileFormatVersion: 2 -guid: 3e57c25f0405b40cab5c30b2c92671ae -timeCreated: 1506633566 -licenseType: Free -MonoImporter: - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/td/Assets/Scripts/Projectile.cs b/td/Assets/Scripts/Projectile.cs index 0cdef16..3b02763 100644 --- a/td/Assets/Scripts/Projectile.cs +++ b/td/Assets/Scripts/Projectile.cs @@ -34,6 +34,7 @@ public class Projectile : MonoBehaviour { } void HitTarget() { + WaveSpawner.EnemiesAlive--; Player.ScoreAdd (PointsPerHit); Destroy (_target.gameObject); Destroy (gameObject); diff --git a/td/Assets/Scripts/tower.cs b/td/Assets/Scripts/tower.cs index d99f937..004605b 100644 --- a/td/Assets/Scripts/tower.cs +++ b/td/Assets/Scripts/tower.cs @@ -89,7 +89,6 @@ public class Tower : MonoBehaviour { _fireCountdown -= Time.deltaTime; - } void Shoot() { diff --git a/td/Assets/Scripts/waveSpawner.cs b/td/Assets/Scripts/waveSpawner.cs index 73c0bc3..3a6d7d4 100644 --- a/td/Assets/Scripts/waveSpawner.cs +++ b/td/Assets/Scripts/waveSpawner.cs @@ -6,16 +6,38 @@ using UnityEngine.UI; public class WaveSpawner : MonoBehaviour { + [Header("Attributes")] + public float TimeBetweenWaves; + public float SpawnRate; + + [Header("Objects")] public Transform SpawnPoint; + public Transform PathWay; public Text WaveCountdownText; - public float TimeBetweenWaves = 5f; + + [Header("Every possible enemy")] + public EnemyType[] Enemies; + [Header("Scripting vars")] public Player Player; // Reference to the player object, should be set in designer + private Transform _parentObject; + private List _waypoints = new List(); + public static int EnemiesAlive = 0; private float _countdown = 2f; private int _waveIndex = 0; + void Awake() { + foreach (Transform child in PathWay) { + _waypoints.Add (child.position); + } + } + + void Start() { + _parentObject = transform.Find ("enemies").gameObject.GetComponent (); + } + void Update () { if (EnemiesAlive > 0) { return; @@ -32,24 +54,72 @@ public class WaveSpawner : MonoBehaviour { //waveCountdownText.text = string.Format("{0:00.00}", countdown); } + private int WaveEnemyCount(int waveNum) { + // 10.64 * e^0,57x + float pow = (float) Math.Pow( Math.E, 0.57f * waveNum); + return (int) Math.Floor(10.64f * pow); + } + + private float EnemyAmountThing(int currentEnemy, int maxTypes) { + // TODO Change the for loop into a faster method + float rest = 1; + + for (int i=1; i <= maxTypes; i++) { + if (i != maxTypes) { rest = rest / 2; } + if (i == currentEnemy + 1) { return rest; } + } + + return 0; + } + IEnumerator SpawnWave () { - int waveNum = 1; - int gdshj = Mathf.FloorToInt(10.64 * (Math.Pow(Math.E, (0.57 * waveNum))))); + int enemiesToSpawn = WaveEnemyCount(_waveIndex); + EnemiesAlive = enemiesToSpawn; + List wave = new List(); + + for (int i = 0; i < Enemies.Length; i++) { + EnemyType enemy = Enemies[i]; - EnemiesAlive = wave.Count; + float amount = enemiesToSpawn * EnemyAmountThing(i, Enemies.Length); + if (amount >= 1) { + wave.Add(new WaveElement {Prefab = enemy.Enemy, Amount = (int)Math.Floor(amount)} ); + } + + } - for (int i = 0; i < wave.Count; i++) - { - SpawnEnemy(wave.Enemy); - yield return new WaitForSeconds(1f / wave.Rate); + foreach (var enemyType in wave) { + for (int i = 0; i < enemyType.Amount; i++) { + SpawnEnemy(enemyType.Prefab); + yield return new WaitForSeconds(1f / SpawnRate); + } + yield return new WaitForSeconds(1f / 100f); } + SpawnRate = SpawnRate * 2; _waveIndex++; } - void SpawnEnemy (GameObject enemy) - { - Instantiate(enemy, SpawnPoint.position, SpawnPoint.rotation); + void SpawnEnemy (GameObject enemyPrefab) { + GameObject newEnemy = Instantiate (enemyPrefab, new Vector3(0, 0, 0), Quaternion.identity, _parentObject); + Enemy script = newEnemy.GetComponent (); + Transform transform = newEnemy.GetComponent (); + + script.Waypoints = _waypoints; + script.Player = Player; + transform.position = new Vector3 (0.93f, 0.483f, 0f); + } + + [System.Serializable] + public class EnemyType { + public string Name; + public GameObject Enemy; + [Range(0, 1)] + public float Percentage; } + public class WaveElement { + public GameObject Prefab { get; set; } + public int Amount { get; set; } + } + } \ No newline at end of file -- cgit v1.2.3