using System.Collections; using System.Collections.Generic; using UnityEngine; public class GameMode : MonoBehaviour { const float aBoxWidth = 30; const int aDevideCount= 2; const float bBoxWidth = 45; const int bDevideCount = 3; const float cBoxWidth = 60; const int cDevideCount = 4; public const float boxDownSpeed = 100; public const float createBoxInterval = 1; [SerializeField] RectTransform container; [SerializeField] GameObject boxPrefab; Queue useStartYQueue = new(); void Start() { boxPrefab.SetActive(false); } float timeCount = 0; void Update() { timeCount += Time.deltaTime; if (timeCount > createBoxInterval) { timeCount = 0; float minX = -container.rect.width / 2 + 100; float maxX = container.rect.width / 2 - 100; float startX = Random.Range(minX, maxX); while (true) { bool hasNear = false; foreach (var item in useStartYQueue) { if (Mathf.Abs(startX - item) < (cBoxWidth + 20)) { hasNear = true; break; } } if (hasNear == false) break; startX = Random.Range(minX, maxX); Debug.Log("相近,重新计算随机x点"); } useStartYQueue.Enqueue(startX); while(useStartYQueue.Count > 2) useStartYQueue.Dequeue(); float boxWidth = bBoxWidth; int devideCount = bDevideCount; float randomValue = Random.Range(0f, 1f); if (randomValue < 0.333) { boxWidth = aBoxWidth; devideCount = aDevideCount; } else if (randomValue < 0.666) { boxWidth = cBoxWidth; devideCount = cDevideCount; } float startY = container.rect.height / 2 + boxWidth / 2; GameObject o = Instantiate(boxPrefab, container); o.name = boxPrefab.name; o.transform.localPosition = new Vector3(startX, startY); (o.transform as RectTransform).sizeDelta = new Vector2(boxWidth, boxWidth); o.SetActive(true); Box box = o.GetComponent(); box.bombDivideCount = devideCount; } } }