Cute_demon_attacks/meng_yao/Assets/script/A_Fight/SpawnMonster.cs
2025-01-06 16:13:39 +08:00

132 lines
3.0 KiB
C#

using System.Collections.Generic;
using UnityEngine;
using System.Threading.Tasks;
using UnityEngine.Rendering;
public class SpawnMonster : Base
{
public static SpawnMonster intance;
[Header("生成位置")]
public List<Transform> SpawnLocations;
[Header("临时索引")]
private int index=1;
public int Index
{
get => index;
set
{
index=value;
StartSpawning();
}
}
public List<GameObject> enemysList=new List<GameObject>();
private void Awake()
{
intance = this;
}
void Start()
{
Base.GlobalObj.GetComponent<gameGlobal>().OnGamePlay += this.StartSpawning;
//StartSpawning();
}
public void UpdateNodeList()
{
// 获取所有带有 SortingGroup 组件的敌人对象
List<GameObject> gameObjectsList = new List<GameObject>();
foreach (GameObject go in enemysList)
{
// 获取该物体上的 SortingGroup 组件
if (go != null)
{
SortingGroup sortingGroup = go.GetComponent<SortingGroup>();
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<SortingGroup>();
if (sortingGroup != null)
{
// 计算排序顺序
int order = gameObjectsList.Count - i + 2;
sortingGroup.sortingOrder = order;
// 获取子 Canvas 并设置其 sortingOrder
Canvas canvas = gameObjectsList[i].GetComponentInChildren<Canvas>();
if (canvas != null)
{
canvas.sortingOrder = order;
}
}
}
}
void Update()
{
UpdateNodeList();
}
public async void StartSpawning()
{
foreach (Wave value in MapLevelJsonRead.instance.waves)
{
if (value.wave == index)
{
foreach (var enemy in value.enemies)
{
await Task.Delay(enemy.startTime * 3);
GameObject go = GameObject.Instantiate(Monster_Infos.instance.GetMonster(enemy.id), SpawnLocations[value.spawnPoint - 1].position, Quaternion.identity);
go.GetComponent<enemy>().index = value.spawnPoint-1;
go.GetComponent<Role>().gold = enemy.Reward;
enemysList.Add(go);
}
}
}
}
// public void
private void OnDisable()
{
Base.GlobalObj.GetComponent<gameGlobal>().OnGamePlay -= this.StartSpawning;
}
}