GridManager.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. namespace Game2048
  6. {
  7. public class GridManager : MonoBehaviour
  8. {
  9. public static GridManager Instance;
  10. public GameObject NodeGrids; // 父对象
  11. public List<GameObject> GridGO = new List<GameObject>();
  12. public int[,] GridValue = new int[4, 4];
  13. // ==================================================
  14. private void Awake()
  15. {
  16. Instance = this;
  17. }
  18. private void Start()
  19. {
  20. Init();
  21. }
  22. private void Update()
  23. {
  24. if (Input.GetKeyDown(KeyCode.W))
  25. {
  26. SwipeUp();
  27. }
  28. }
  29. // ==================================================
  30. private void Init()
  31. {
  32. GetGridGO();
  33. GetGridValue();
  34. PrintGridValue();
  35. return;
  36. }
  37. /// <summary>
  38. /// 获取网格对象
  39. /// </summary>
  40. public void GetGridGO()
  41. {
  42. GridGO.Clear();
  43. for (int i = 0; i < NodeGrids.transform.childCount; i++)
  44. {
  45. GridGO.Add(NodeGrids.transform.GetChild(i).gameObject);
  46. }
  47. return;
  48. }
  49. /// <summary>
  50. /// 获取网格数值
  51. /// </summary>
  52. public void GetGridValue()
  53. {
  54. for (int i = 0; i < 4; i++)
  55. {
  56. for (int j = 0; j < 4; j++)
  57. {
  58. // 0*4+0 0*4+1 0*4+2 0*4+3
  59. // 1*4+0 1*4+1 1*4+2 1*4+3
  60. // 2*4+0
  61. GridValue[i, j] = int.Parse(GridGO[i * 4 + j].transform.GetChild(0).GetComponent<Text>().text);
  62. }
  63. }
  64. return;
  65. }
  66. /// <summary>
  67. /// DEBUG
  68. /// 打印数值
  69. /// </summary>
  70. public void PrintGridValue()
  71. {
  72. string debugMessage = null;
  73. for (int i = 0; i < GridValue.GetLength(1); i++)
  74. {
  75. for (int j = 0; j < GridValue.GetLength(0); j++)
  76. {
  77. debugMessage += GridValue[i, j].ToString() + "\t";
  78. }
  79. debugMessage += "\n";
  80. }
  81. Debug.Log("\n" + debugMessage);
  82. return;
  83. }
  84. // ==================================================
  85. /// <summary>
  86. /// 向上滑动
  87. /// </summary>
  88. public void SwipeUp()
  89. {
  90. // 0002
  91. // 0020
  92. // 0200
  93. // 2000
  94. for (int x = 0; x < GridValue.GetLength(0); x++) // 列循环
  95. {
  96. int[] tempColumn = new int[4];
  97. for (int i = 0; i < 4; i++) // 获取第一列数值
  98. {
  99. tempColumn[i] = GridValue[i, x];
  100. }
  101. tempColumn = PutZero2End(tempColumn);
  102. for (int i = 0; i < 4 - 1; i++) // 加
  103. {
  104. if (tempColumn[i] == tempColumn[i + 1])
  105. {
  106. tempColumn[i] *= 2;
  107. tempColumn[i + 1] = 0;
  108. }
  109. }
  110. tempColumn = PutZero2End(tempColumn);
  111. for (int i = 0; i < 4; i++) // 交还数据
  112. {
  113. GridValue[i, x] = tempColumn[i];
  114. }
  115. }
  116. UpdateAllGridsView();
  117. return;
  118. }
  119. /// <summary>
  120. /// 0排序至数组末尾
  121. /// </summary>
  122. /// <param name="value"></param>
  123. /// <returns></returns>
  124. private int[] PutZero2End(int[] value)
  125. {
  126. for (int i = 0; i < value.Length; i++)
  127. {
  128. for (int j = 0; j < value.Length - 1; j++)
  129. {
  130. if (value[j] == 0 && value[j + 1] != 0)
  131. {
  132. int tempValue = value[j];
  133. value[j] = value[j + 1];
  134. value[j + 1] = tempValue;
  135. }
  136. }
  137. }
  138. return value;
  139. }
  140. // ==================================================
  141. // View
  142. /// <summary>
  143. /// 更新网格视图
  144. /// </summary>
  145. /// <param name="value"></param>
  146. public void UpdateAllGridsView()
  147. {
  148. for (int i = 0; i < 4; i++)
  149. {
  150. for (int j = 0; j < 4; j++)
  151. {
  152. GridGO[i * 4 + j].transform.GetChild(0).GetComponent<Text>().text = GridValue[i, j].ToString();
  153. }
  154. }
  155. return;
  156. }
  157. }
  158. }