Cute_demon_attacks/meng_yao/Assets/script/A_Fight/SpawnMonster.cs
wulongxiao 804e570781 修改
2025-01-09 14:37:27 +08:00

196 lines
4.9 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Collections.Generic;
using UnityEngine;
using System.Threading.Tasks;
using Unity.VisualScripting;
using UnityEngine.Rendering;
[System.Serializable]
public class monster
{
public string id;
public GameObject prefab;
}
public class SpawnMonster : Base
{
public static SpawnMonster intance;
public List<monster> monsters = new List<monster>();
[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>();
public List<GameObject> MengYaoList = new List<GameObject>();
public List<GameObject> SortList = 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>();
SortList.AddRange(enemysList);
SortList.AddRange(MengYaoList);
foreach (GameObject go in SortList)
{
// 获取该物体上的 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 FixedUpdate()
{
//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);
}*/
for (int i = 0; i < value.enemies.Count;)
{
if(Time.timeScale == 0)
{
await Task.Delay(1000);
}
else
{
var enemy = value.enemies[i];
await Task.Delay(enemy.startTime * 3);
Debug.Log("生成怪物");
GameObject go = GameObject.Instantiate(
GetMonster(enemy.id),
SpawnLocations[value.spawnPoint - 1].position,
Quaternion.identity
);
Rigidbody rb = go.GetComponent<Rigidbody>();
if (rb != null) rb.isKinematic = true;
go.GetComponent<enemy>().index = value.spawnPoint - 1;
go.GetComponent<Role>().gold = enemy.Reward;
enemysList.Add(go);
i++;
}
}
}
}
}
/// <summary>
/// 根据id得到预制体如果找不到对应id默认返回第一个怪物
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public GameObject GetMonster(string id)
{
foreach (monster value in monsters)
{
if (value.id == id)
{
return value.prefab;
}
}
return monsters[0].prefab;
}
private void OnDisable()
{
Base.GlobalObj.GetComponent<gameGlobal>().OnGamePlay -= this.StartSpawning;
}
}