diff --git a/meng_yao/Assets/script/A_Fight/MY_Infos.cs b/meng_yao/Assets/script/A_Fight/MY_Infos.cs new file mode 100644 index 00000000..ed2957b4 --- /dev/null +++ b/meng_yao/Assets/script/A_Fight/MY_Infos.cs @@ -0,0 +1,18 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class MY_Infos : MonoBehaviour +{ + // Start is called before the first frame update + void Start() + { + + } + + // Update is called once per frame + void Update() + { + + } +} diff --git a/meng_yao/Assets/script/A_Fight/MY_Infos.cs.meta b/meng_yao/Assets/script/A_Fight/MY_Infos.cs.meta new file mode 100644 index 00000000..e520eda3 --- /dev/null +++ b/meng_yao/Assets/script/A_Fight/MY_Infos.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: f6c570b2c3c14994fa8337757d1d492e +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/meng_yao/Assets/script/A_Fight/SpawnMonster.cs b/meng_yao/Assets/script/A_Fight/SpawnMonster.cs new file mode 100644 index 00000000..877326d3 --- /dev/null +++ b/meng_yao/Assets/script/A_Fight/SpawnMonster.cs @@ -0,0 +1,211 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using UnityEngine.Rendering; + + +public class SpawnMonster : Base +{ + public static SpawnMonster intance; + + public ObJPool EnemyPool = new ObJPool(); + public List Enemys = new List(); + + [Header("生成预制体")] + public GameObject Prefab; + + [Header("生成位置")] + public Transform SpawnLocation; + + [Header("一波生成间隔")] + public float GenerationInterval; + + [Header("一次生成间隔")] + public float GenerationIntervalOfOne; + + [Header("生成总数量")] + public float GenerateQuantityMax; + + [Header("每一波生成数量")] + public float GenerateQuantity; + + [HideInInspector] + public int deadNumber = 0; + public int DeadNumber + { + get => deadNumber; + set + { + deadNumber = value; + if (deadNumber >= GenerateQuantityMax) + { + // gameGlobal.GameVictory(); + } + } + } + + + // 状态变量 + private bool IsStartGet = false; // 是否开始生成 + private bool IsWave = true; // 当前是否在生成波 + private float timerOfOne = 0f; // 记录每个敌人生成的时间 + private float timerOfOneWave = 0f; // 记录波与波之间的时间 + private int generatedCount = 0; // 总已生成敌人数量 + private int waveGeneratedCount = 0; // 当前波已生成敌人数量 + private int currentWave = 0; // 当前波数 + + + private void Awake() + { + intance = this; + } + + void Start() + { + Base.GlobalObj.GetComponent().OnGamePlay += this.StartSpawning; + } + public void UpdateNodeList() + { + // 获取所有带有 SortingGroup 组件的敌人对象 + List gameObjectsList = new List(); + + foreach (GameObject go in Enemys) + { + // 获取该物体上的 SortingGroup 组件 + if (go != null) + { + SortingGroup sortingGroup = go.GetComponent(); + if (sortingGroup != null && go.activeSelf) + { + // 添加到列表中 + gameObjectsList.Add(go); + } + } + + } + + // 按照 y 轴排序 + gameObjectsList.Sort((a, b) => a.transform.position.y.CompareTo(b.transform.position.y)); + + // 设置 SortingGroup 和子 Canvas 的 sortingOrder + for (int i = 0; i < gameObjectsList.Count; i++) + { + SortingGroup sortingGroup = gameObjectsList[i].GetComponent(); + if (sortingGroup != null) + { + // 计算排序顺序 + int order = gameObjectsList.Count - i + 2; + sortingGroup.sortingOrder = order; + + // 获取子 Canvas 并设置其 sortingOrder + Canvas canvas = gameObjectsList[i].GetComponentInChildren(); + if (canvas != null) + { + canvas.sortingOrder = order; + } + } + } + } + + + + void Update() + { + if (IsStartGet) + { + if (IsWave) + { + timerOfOne += Time.deltaTime; + if (timerOfOne >= GenerationIntervalOfOne && waveGeneratedCount < GenerateQuantity) + { + GenerateEnemy(); // 生成敌人 + timerOfOne = 0f; // 重置计时器 + waveGeneratedCount++; // 增加当前波的生成数量 + + // 如果当前波的敌人数量已达上限,则标记波结束 + if (waveGeneratedCount >= GenerateQuantity) + { + IsWave = false; // 当前波生成完毕,切换到等待下一波 + } + } + } + else + { + timerOfOneWave += Time.deltaTime; + if (timerOfOneWave >= GenerationInterval) + { + // 当前波生成完毕,等待下一波 + currentWave++; + if (generatedCount < GenerateQuantityMax) + { + waveGeneratedCount = 0; + timerOfOneWave = 0f; // 重置波间隔计时器 + IsWave = true; // 启动新的一波 + } + else + { + IsStartGet = false; // 所有敌人生成完毕,停止生成 + } + } + } + } + } + + private void FixedUpdate() + { + UpdateNodeList(); + } + public void StartSpawning() + { + if (GenerateQuantity >= GenerateQuantityMax) + { + GenerateQuantity = GenerateQuantityMax; + } + // 初始化状态,开始生成 + IsStartGet = true; + waveGeneratedCount = 0; + currentWave = 0; + generatedCount = 0; + timerOfOne = 0f; + timerOfOneWave = 0f; + } + + void GenerateEnemy() + { + + GameObject enemy = EnemyPool.Get(); // 获取一个敌人对象 + + // 如果池子中没有可用的对象,创建新的敌人 + if (enemy == null) + { + if (Prefab == null) + { + Debug.LogError("Prefab is not assigned."); + return; + } + + enemy = Instantiate(Prefab); + //EnemyPool.SurvivalPool.Add(enemy); + Enemys.Add(enemy); + } + + //enemy.GetComponent().ResetAllStatus(); + // 设置敌人的位置和其他属性 + enemy.SetActive(true); // 激活敌人对象 + enemy.transform.position = SpawnLocation.position; + + // 更新生成计数 + generatedCount++; + } + + + + + private void OnDisable() + { + Base.GlobalObj.GetComponent().OnGamePlay -= this.StartSpawning; + } +} + + + diff --git a/meng_yao/Assets/script/A_Fight/SpawnMonster.cs.meta b/meng_yao/Assets/script/A_Fight/SpawnMonster.cs.meta new file mode 100644 index 00000000..6e1d61e2 --- /dev/null +++ b/meng_yao/Assets/script/A_Fight/SpawnMonster.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 97664c5246bb0d242997921b3560d036 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: