_TheStrongestSnail/TheStrongestSnail/Assets/Scripts/Battle_Royale/Allother.cs
2024-11-21 12:03:01 +08:00

97 lines
2.7 KiB
C#

using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;
public class Allother : MonoBehaviour
{
public List<otherWoniu> otherWonius=new List<otherWoniu>();
// public List<HouseBtn> House;//蜗牛进入的父物体
public GameObject OtherWoniuPre;//其他蜗牛预制体
public AllHouseContro allHouseContro;
// Start is called before the first frame update
void Start()
{
//ControWoniuToMove();
//StartCoroutine(WoniuToMove());
}
//public IEnumerator WoniuToMove()
//{
// CreateWoniu();//创建蜗牛
//}
//public async void ControWoniuToMove()
// {
// foreach (otherWoniu item in otherWonius)
// {
// await Task.Delay(1000);
// item.OtherWoniuMove();
// }
// }
//笼子里的蜗牛全部清空
public void ClearAllWoniu()
{
if (otherWonius != null)
{
for (int i = 0; i < otherWonius.Count; i++)
{
Destroy(otherWonius[i].gameObject);
}
}
otherWonius.Clear();
}
public void CreateWoniu()
{
foreach (HouseBtn house in allHouseContro.HouseBtnList)
{
for (int i = 0; i < house.roomUserNo; i++)
{
// 在指定范围内随机生成位置
float randomX = Random.Range(-250f, 250f); // X轴范围
float randomY = Random.Range(-80f, 80f); // Y轴范围
Vector2 randomPosition = new Vector2(randomX, randomY);
// 实例化蜗牛
GameObject ot = Instantiate(OtherWoniuPre, transform);
ot.GetComponent<RectTransform>().anchoredPosition = randomPosition;
// 添加到蜗牛列表并设置其行为
otherWonius.Add(ot.GetComponent<otherWoniu>());
ot.GetComponent<otherWoniu>().OtherWoniuMove(house); // 移动到目标房间
Debug.Log("生成蜗牛,随机位置:" + randomPosition);
}
}
}
public void startIEmove()
{
StartCoroutine(IECreateWoniu());
}
public IEnumerator IECreateWoniu()
{
foreach (HouseBtn house in allHouseContro.HouseBtnList)
{
for (int i = 0; i < house.roomUserNo; i++)
{
GameObject ot = Instantiate(OtherWoniuPre, transform);
ot.GetComponent<RectTransform>().anchoredPosition = new Vector2(Random.Range(-250, 250), Random.Range(-80, 80));
otherWonius.Add(ot.GetComponent<otherWoniu>());
ot.GetComponent<otherWoniu>().OtherWoniuMove(house);//进房间
Debug.Log("生成蜗牛");
yield return new WaitForSeconds(0.1f);
}
}
}
}