_TheStrongestSnail/TheStrongestSnail/Assets/Scripts/Battle_Royale/Allother.cs
2024-11-25 14:37:44 +08:00

209 lines
6.1 KiB
C#

using DG.Tweening;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;
using static UnityEngine.UIElements.UxmlAttributeDescription;
public class Allother : MonoBehaviour
{
public List<otherWoniu> otherWonius;
// public List<HouseBtn> House;//蜗牛进入的父物体
public GameObject OtherWoniuPre;//其他蜗牛预制体
public AllHouseContro allHouseContro;
public List<GameObject> JiaWoniu;//场下留下的假蜗牛
private List<GameObject> hiddenSnails = new List<GameObject>(); // 用来记录未显示的蜗牛
public Transform ReTurnPos;//返回点
// 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 HideJiaWoniu()
{
// 初始化时将所有蜗牛隐藏,并添加到未显示列表
foreach (GameObject go in JiaWoniu)
{
go.SetActive(false);
hiddenSnails.Add(go);
}
}
public void Reflash() // 随机显示假蜗牛
{
if (hiddenSnails.Count == 0)
{
//Debug.Log("所有假蜗牛都已显示!");
return;
}
// 每次随机显示一定数量(例如 1 到 3 个)
int numToShow = Random.Range(1, Mathf.Min(4, hiddenSnails.Count + 1));
for (int i = 0; i < numToShow; i++)
{
// 随机选择一个未显示的蜗牛
int randomIndex = Random.Range(0, hiddenSnails.Count);
GameObject snail = hiddenSnails[randomIndex];
// 设置位置
snail.GetComponent<RectTransform>().anchoredPosition = new Vector2(
Random.Range(-350, 550),
Random.Range(0, 40)
);
// 显示蜗牛
snail.SetActive(true);
// 从未显示列表中移除
hiddenSnails.RemoveAt(randomIndex);
}
}
//笼子里的蜗牛全部清空
public void ClearAllWoniu()
{
foreach (HouseBtn house in allHouseContro.HouseBtnList)
{
if (house.otherWonius != null)
{
house.otherWonius.Clear();
}
}
for (int i=0;i<otherWonius.Count;i++)
{
if (otherWonius[i] != null)
{
DOTween.Kill(otherWonius[i].gameObject);
Destroy(otherWonius[i].gameObject);
}
}
otherWonius.Clear();
}
//清空指定房间蜗牛
public void KilltheWoniu(int killNo)
{
for (int i=0;i<allHouseContro.HouseBtnList.Count;i++)
{
if (allHouseContro.HouseBtnList[i].roomNo==killNo)
{
for (int j = 0; j< allHouseContro.HouseBtnList[i].otherWonius.Count; j++)
{
if (allHouseContro.HouseBtnList[i].otherWonius[j].gameObject!=null)
{
}
Destroy(allHouseContro.HouseBtnList[i].otherWonius[j].gameObject);
}
allHouseContro.HouseBtnList[i].otherWonius.Clear();
}
}
}
//对应房间蜗牛回起始点
public IEnumerator ReturnstartPos(int roomNo)
{
allHouseContro.ControAllDoorOpen();
for (int i = 0; i < allHouseContro.HouseBtnList.Count; i++)
{
if (allHouseContro.HouseBtnList[i].roomNo == roomNo)
{
for (int j = 0; j < allHouseContro.HouseBtnList[i].otherWonius.Count; j++)
{
allHouseContro.HouseBtnList[i].otherWonius[j].GetComponent<OtherMove>().EndPos = ReTurnPos;
allHouseContro.HouseBtnList[i].otherWonius[j].GetComponent<OtherMove>().ReTurnStartPosMove();
yield return new WaitForSeconds(0.3f);
}
allHouseContro.HouseBtnList[i].otherWonius.Clear();
}
}
}
//public void CreateWoniu()
//{
// foreach (HouseBtn house in allHouseContro.HouseBtnList)
// {
// for (int i = 0; i < house.roomUserNo-house.otherWonius.Count; 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;
// // 添加到蜗牛列表并设置其行为
// house.otherWonius.Add(ot.GetComponent<otherWoniu>());
// ot.GetComponent<otherWoniu>().OtherWoniuMove(house); // 移动到目标房间
// Debug.Log("生成蜗牛,随机位置:" + randomPosition);
// }
// }
//}
public void startIEmove()
{
StartCoroutine(IECreateWoniu());
Reflash();
}
public IEnumerator IECreateWoniu()
{
foreach (HouseBtn house in allHouseContro.HouseBtnList)
{
for (int i = 0; i < house.roomUserNo-house.otherWonius.Count; i++)
{
GameObject ot = Instantiate(OtherWoniuPre, transform);
ot.GetComponent<RectTransform>().anchoredPosition = new Vector2(Random.Range(-250, 250), Random.Range(-80, 0));
house.otherWonius.Add(ot.GetComponent<otherWoniu>());
otherWonius.Add(ot.GetComponent<otherWoniu>());
ot.GetComponent<otherWoniu>().OtherWoniuMove(house);//进房间
//Debug.Log("生成蜗牛");
yield return new WaitForSeconds(0.1f);
}
}
}
}