diff options
author | chai <215380520@qq.com> | 2024-03-13 11:00:58 +0800 |
---|---|---|
committer | chai <215380520@qq.com> | 2024-03-13 11:00:58 +0800 |
commit | 6ce8b9e22fc13be34b442c7b6af48b42cd44275a (patch) | |
tree | b38119d2acf0a982cb67e381f146924b9bfc3b3f /GameMode.cs |
+init
Diffstat (limited to 'GameMode.cs')
-rw-r--r-- | GameMode.cs | 124 |
1 files changed, 124 insertions, 0 deletions
diff --git a/GameMode.cs b/GameMode.cs new file mode 100644 index 0000000..2d717aa --- /dev/null +++ b/GameMode.cs @@ -0,0 +1,124 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class GameMode : MonoBehaviour +{ + public List<EnemyWave> wavesToSpawn = new List<EnemyWave>(); + + public static GameMode instance; + + private int currentWave; + + public List<EnemyAI> enemies = new List<EnemyAI>(); + + public Transform spawnPointParent; + + private List<Transform> spawnPoints = new List<Transform>(); + + public LayerMask mask; + + public GameObject enemy_Default; + + public GameObject enemy_Fast; + + private IEnumerator Start() + { + Screen.fullScreen = false; + Screen.SetResolution(1800, 1000, false); + + Cursor.visible = false; + Cursor.lockState = CursorLockMode.Locked; + + Init(); + while (true) + { + yield return new WaitForSeconds(3f); + //SpawnWave(); + yield return WaitForWaveToEnd(); + } + } + + private IEnumerator WaitForWaveToEnd() + { + while (enemies.Count > 0) + { + yield return null; + } + } + + private void Update() + { + for (int num = enemies.Count - 1; num >= 0; num--) + { + if (enemies[num] == null) + { + enemies.RemoveAt(num); + } + else if (enemies[num].IsDead()) + { + enemies.RemoveAt(num); + } + } + } + + private void Init() + { + mask = LayerMask.GetMask("Map"); + for (int i = 0; i < spawnPointParent.childCount; i++) + { + spawnPoints.Add(spawnPointParent.GetChild(i)); + } + } + + private void SpawnWave() + { + for (int i = 0; i < wavesToSpawn[currentWave].groups.Count; i++) + { + for (int j = 0; (float)j < wavesToSpawn[currentWave].groups[i].number; j++) + { + SpawnEnemy(wavesToSpawn[currentWave].groups[i].enemyType); + } + } + currentWave++; + if (currentWave >= wavesToSpawn.Count) + { + currentWave = wavesToSpawn.Count - 1; + } + } + + private void SpawnEnemy(EnemyGroup.EnemyType enemyType) + { + GameObject gameObject = Object.Instantiate(GetEnemyFromType(enemyType), GetSpawnPos(), Quaternion.identity); + gameObject.GetComponent<EnemyAI>().target = Player.localPlayer.torso; + enemies.Add(gameObject.GetComponent<EnemyAI>()); + } + + private Vector3 GetSpawnPos() + { + bool flag = false; + int num = 100000; + while (true && num > 0) + { + num--; + Vector3 vector = spawnPoints[Random.Range(0, spawnPoints.Count)].position + Vector3.up * 0.3f; + RaycastHit hitInfo = default(RaycastHit); + Ray ray = new Ray(vector, vector - Player.localPlayer.transform.position); + Physics.Raycast(ray, out hitInfo, Vector3.Distance(vector, Player.localPlayer.transform.position), mask); + if (hitInfo.collider == null) + { + return vector; + } + } + return spawnPoints[Random.Range(0, spawnPoints.Count)].position + Vector3.up * 0.3f; + } + + private GameObject GetEnemyFromType(EnemyGroup.EnemyType enemyType) + { + if (enemyType == EnemyGroup.EnemyType.Fast) + { + return enemy_Fast; + } + return enemy_Default; + } +} |