GameMode.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class GameMode : MonoBehaviour
  5. {
  6. const float aBoxWidth = 30;
  7. const int aDevideCount= 2;
  8. const float bBoxWidth = 45;
  9. const int bDevideCount = 3;
  10. const float cBoxWidth = 60;
  11. const int cDevideCount = 4;
  12. public const float boxDownSpeed = 100;
  13. public const float createBoxInterval = 1;
  14. [SerializeField] RectTransform container;
  15. [SerializeField] GameObject boxPrefab;
  16. Queue<float> useStartYQueue = new();
  17. void Start()
  18. {
  19. boxPrefab.SetActive(false);
  20. }
  21. float timeCount = 0;
  22. void Update()
  23. {
  24. timeCount += Time.deltaTime;
  25. if (timeCount > createBoxInterval)
  26. {
  27. timeCount = 0;
  28. float minX = -container.rect.width / 2 + 100;
  29. float maxX = container.rect.width / 2 - 100;
  30. float startX = Random.Range(minX, maxX);
  31. while (true)
  32. {
  33. bool hasNear = false;
  34. foreach (var item in useStartYQueue)
  35. {
  36. if (Mathf.Abs(startX - item) < (cBoxWidth + 20))
  37. {
  38. hasNear = true;
  39. break;
  40. }
  41. }
  42. if (hasNear == false) break;
  43. startX = Random.Range(minX, maxX);
  44. Debug.Log("相近,重新计算随机x点");
  45. }
  46. useStartYQueue.Enqueue(startX);
  47. while(useStartYQueue.Count > 2) useStartYQueue.Dequeue();
  48. float boxWidth = bBoxWidth;
  49. int devideCount = bDevideCount;
  50. float randomValue = Random.Range(0f, 1f);
  51. if (randomValue < 0.333)
  52. {
  53. boxWidth = aBoxWidth;
  54. devideCount = aDevideCount;
  55. }
  56. else if (randomValue < 0.666)
  57. {
  58. boxWidth = cBoxWidth;
  59. devideCount = cDevideCount;
  60. }
  61. float startY = container.rect.height / 2 + boxWidth / 2;
  62. GameObject o = Instantiate(boxPrefab, container);
  63. o.name = boxPrefab.name;
  64. o.transform.localPosition = new Vector3(startX, startY);
  65. (o.transform as RectTransform).sizeDelta = new Vector2(boxWidth, boxWidth);
  66. o.SetActive(true);
  67. Box box = o.GetComponent<Box>();
  68. box.bombDivideCount = devideCount;
  69. }
  70. }
  71. }