MirzkisD1Ex0 2 жил өмнө
parent
commit
0a4faeb35e

Файлын зөрүү хэтэрхий том тул дарагдсан байна
+ 39 - 699
2048/Assets/Main/Scenes/DevScene.unity


+ 1 - 1
2048/Assets/Main/Scenes/DevScene.unity.meta

@@ -1,5 +1,5 @@
 fileFormatVersion: 2
-guid: 2cda990e2423bbf4892e6590ba056729
+guid: 5fe0a60e403e5f04f912ceca534c5fb3
 DefaultImporter:
   externalObjects: {}
   userData: 

+ 0 - 10
2048/Assets/Main/Scripts/GameManager.cs

@@ -6,16 +6,6 @@ namespace Game2048
 {
   public class GameManager : MonoBehaviour
   {
-    // Start is called before the first frame update
-    void Start()
-    {
 
-    }
-
-    // Update is called once per frame
-    void Update()
-    {
-
-    }
   }
 }

+ 146 - 3
2048/Assets/Main/Scripts/GridManager.cs

@@ -9,9 +9,10 @@ namespace Game2048
   {
     public static GridManager Instance;
 
+    public GameObject NodeGrids; // 父对象
     public List<GameObject> GridGO = new List<GameObject>();
 
-    public int[,] GridArray = new int[4, 4];
+    public int[,] GridValue = new int[4, 4];
 
     // ==================================================
 
@@ -22,18 +23,160 @@ namespace Game2048
 
     private void Start()
     {
+      Init();
+    }
 
+    private void Update()
+    {
+      if (Input.GetKeyDown(KeyCode.W))
+      {
+        SwipeUp();
+      }
     }
 
     // ==================================================
 
+    private void Init()
+    {
+      GetGridGO();
+      GetGridValue();
+      PrintGridValue();
+      return;
+    }
+
+    /// <summary>
+    /// 获取网格对象
+    /// </summary>
+    public void GetGridGO()
+    {
+      GridGO.Clear();
+      for (int i = 0; i < NodeGrids.transform.childCount; i++)
+      {
+        GridGO.Add(NodeGrids.transform.GetChild(i).gameObject);
+      }
+      return;
+    }
+
     /// <summary>
-    /// 
+    /// 获取网格数值
+    /// </summary>
+    public void GetGridValue()
+    {
+      for (int i = 0; i < 4; i++)
+      {
+        for (int j = 0; j < 4; j++)
+        {
+          // 0*4+0 0*4+1 0*4+2 0*4+3
+          // 1*4+0 1*4+1 1*4+2 1*4+3
+          // 2*4+0
+          GridValue[i, j] = int.Parse(GridGO[i * 4 + j].transform.GetChild(0).GetComponent<Text>().text);
+        }
+      }
+      return;
+    }
+
+    /// <summary>
+    /// DEBUG
+    /// 打印数值
+    /// </summary>
+    public void PrintGridValue()
+    {
+      string debugMessage = null;
+      for (int i = 0; i < GridValue.GetLength(1); i++)
+      {
+        for (int j = 0; j < GridValue.GetLength(0); j++)
+        {
+          debugMessage += GridValue[i, j].ToString() + "\t";
+        }
+        debugMessage += "\n";
+      }
+      Debug.Log("\n" + debugMessage);
+      return;
+    }
+
+    // ==================================================
+
+
+    /// <summary>
+    /// 向上滑动
+    /// </summary>
+    public void SwipeUp()
+    {
+      // 0002
+      // 0020
+      // 0200
+      // 2000
+
+      for (int x = 0; x < GridValue.GetLength(0); x++) // 列循环
+      {
+        int[] tempColumn = new int[4];
+
+        for (int i = 0; i < 4; i++) // 获取第一列数值
+        {
+          tempColumn[i] = GridValue[i, x];
+        }
+
+        tempColumn = PutZero2End(tempColumn);
+
+        for (int i = 0; i < 4 - 1; i++) // 加
+        {
+          if (tempColumn[i] == tempColumn[i + 1])
+          {
+            tempColumn[i] *= 2;
+            tempColumn[i + 1] = 0;
+          }
+        }
+
+        tempColumn = PutZero2End(tempColumn);
+
+        for (int i = 0; i < 4; i++) // 交还数据
+        {
+          GridValue[i, x] = tempColumn[i];
+        }
+      }
+
+      UpdateAllGridsView();
+      return;
+    }
+
+    /// <summary>
+    /// 0排序至数组末尾
     /// </summary>
     /// <param name="value"></param>
-    public void UpdateAllGridsText(int[,] value)
+    /// <returns></returns>
+    private int[] PutZero2End(int[] value)
     {
+      for (int i = 0; i < value.Length; i++)
+      {
+        for (int j = 0; j < value.Length - 1; j++)
+        {
+          if (value[j] == 0 && value[j + 1] != 0)
+          {
+            int tempValue = value[j];
+            value[j] = value[j + 1];
+            value[j + 1] = tempValue;
+          }
+        }
+      }
+      return value;
+    }
 
+    // ==================================================
+    // View
+
+    /// <summary>
+    /// 更新网格视图
+    /// </summary>
+    /// <param name="value"></param>
+    public void UpdateAllGridsView()
+    {
+      for (int i = 0; i < 4; i++)
+      {
+        for (int j = 0; j < 4; j++)
+        {
+          GridGO[i * 4 + j].transform.GetChild(0).GetComponent<Text>().text = GridValue[i, j].ToString();
+        }
+      }
       return;
     }
   }

+ 1 - 1
2048/ProjectSettings/EditorBuildSettings.asset

@@ -6,6 +6,6 @@ EditorBuildSettings:
   serializedVersion: 2
   m_Scenes:
   - enabled: 1
-    path: Assets/Main/Scenes/DevScene.unity
+    path: Assets/Scenes/SampleScene.unity
     guid: 2cda990e2423bbf4892e6590ba056729
   m_configObjects: {}

+ 1 - 1
2048/ProjectSettings/ProjectSettings.asset

@@ -4,7 +4,7 @@
 PlayerSettings:
   m_ObjectHideFlags: 0
   serializedVersion: 26
-  productGUID: 867acb51ba7b9be4499922a0c9193e20
+  productGUID: 188f9a4a5c06d6444955c059fcb612ce
   AndroidProfiler: 0
   AndroidFilterTouchesWhenObscured: 0
   AndroidEnableSustainedPerformanceMode: 0

Энэ ялгаанд хэт олон файл өөрчлөгдсөн тул зарим файлыг харуулаагүй болно