Unity2Dのパズルを作成しており、横一列に揃って消えた回数をカウントしたいのですが
うまくカウントできず困っています。
Count.cs
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using UnityEngine.UI; 5 6public class Count : MonoBehaviour { 7 8 public Text RowCount; 9 public int deleteRowsCount = 0; 10 11 bool deleteFullRows() 12 { 13 for (int y = 0; y < Grid.h; ++y) 14 { 15 if (!Grid.isRowFull(y)) 16 { 17 return false; 18 } 19 } 20 return true; 21 } 22 // Use this for initialization 23 void Start () { 24 RowCount.text = "消えた列:" + deleteRowsCount; 25 } 26 public void countUp() { 27 if (deleteFullRows()) 28 { 29 ++deleteRowsCount; 30 RowCount.text = "消えた列:" + deleteRowsCount; 31 } 32 } 33} 34
Group.cs
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class Group : MonoBehaviour 6{ 7 private Vector3 screenPoint; 8 private Vector3 offset; 9 private Count count; 10 11 //座標が範囲内であるか,Gridに値があるかどうかを判定 12 bool isValidGridPos() 13 { 14 foreach (Transform child in transform) 15 { 16 Vector2 v = Grid.roundVec2(child.position); 17 18 if (!Grid.insideBorder(v)) 19 return false; 20 21 if (Grid.grid[(int)v.x, (int)v.y] != null && 22 Grid.grid[(int)v.x, (int)v.y].parent != transform) 23 return false; 24 } 25 return true; 26 } 27 28 void updateGrid() 29 { 30 for (int y = 0; y < Grid.h; ++y) 31 for (int x = 0; x < Grid.w; ++x) 32 if (Grid.grid[x, y] != null) 33 if (Grid.grid[x, y].parent == transform) 34 Grid.grid[x, y] = null; 35 36 foreach (Transform child in transform) 37 { 38 Vector2 v = Grid.roundVec2(child.position); 39 Grid.grid[(int)v.x, (int)v.y] = child; 40 } 41 } 42 43 //オブジェクトをクリックする 44 void OnMouseDown() 45 { 46 // マウスカーソルは、スクリーン座標なので、 47 // 対象のオブジェクトもスクリーン座標に変換してから計算する。 48 49 // このオブジェクトの位置(transform.position)をスクリーン座標に変換。 50 screenPoint = Camera.main.WorldToScreenPoint(transform.position); 51 // ワールド座標上の、マウスカーソルと、対象の位置の差分。 52 offset = transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z)); 53 54 55 } 56 57 //オブジェクトをドラッグする 58 void OnMouseDrag() 59 { 60 Vector3 currentScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z); 61 Vector3 currentPosition = Camera.main.ScreenToWorldPoint(currentScreenPoint) + this.offset; 62 transform.position = currentPosition; 63 } 64 65 //オブジェクトをドロップする 66 void OnMouseUp() 67 { 68 Vector3 currentScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z); 69 Vector3 currentPosition = Camera.main.ScreenToWorldPoint(currentScreenPoint) + this.offset; 70 71 //currentPositionが範囲内であるか,Gridに値があるかどうかを判定 72 if (isValidGridPos()) 73 { 74 transform.position = Grid.roundVec2(currentPosition); 75 //修正 76 count.countUp(); 77 //横列と縦列を消去 78 Grid.deleteFullRows(); 79 Grid.deleteFullLines(); 80 81 //指定範囲内ではブロックの当たり判定を無くす 82 BoxCollider2D collider = GetComponent<BoxCollider2D>(); 83 collider.enabled = false; 84 85 //次のブロックを呼び出す 86 FindObjectOfType<Spawner>().spawnNext(); 87 88 89 } 90 else 91 { 92 //Spawnerオブジェクトの座標を取る 93 transform.position = GameObject.Find("Spawner").transform.position; 94 } 95 96 } 97 98 99 // Use this for initialization 100 void Start() 101 { 102 count = FindObjectOfType<Count>(); 103 } 104 105 // Update is called once per frame 106 void Update() 107 { 108 if (isValidGridPos()) 109 { 110 // It's valid. Update grid. 111 updateGrid(); 112 } 113 114 115 116 117 } 118} 119
Grid.cs
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class Grid: MonoBehaviour { 6 public static int w = 8; 7 public static int h = 8; 8 public static Transform[,] grid = new Transform[w, h]; 9 10 //座標を整数にする 11 public static Vector2 roundVec2(Vector2 v) 12 { 13 return new Vector2(Mathf.Round(v.x), 14 Mathf.Round(v.y)); 15 } 16 17 //指定範囲内かどうかを判定 18 public static bool insideBorder(Vector2 pos) 19 { 20 return ((int)pos.x >= 0 && 21 (int)pos.x < w && 22 (int)pos.y >= 0 && 23 (int)pos.y < h); 24 } 25 26 //横列を消去 27 public static void deleteRow(int y) 28 { 29 for(int x = 0; x < w; ++x) 30 { 31 Destroy(grid[x, y].gameObject); 32 grid[x, y] = null; 33 } 34 } 35 36 //縦列を消去 37 public static void deleteLine(int x) 38 { 39 for (int y = 0; y < h; ++y) 40 { 41 Destroy(grid[x, y].gameObject); 42 grid[x, y] = null; 43 } 44 } 45 46 //横列が埋まっているかどうかを判定 47 public static bool isRowFull(int y) 48 { 49 for (int x = 0; x < w; ++x) 50 if (grid[x, y] == null) 51 return false; 52 return true; 53 } 54 55 //縦列が埋まっているかどうかを判定 56 public static bool isLineFull(int x) 57 { 58 for (int y = 0; y < h; ++y) 59 if (grid[x, y] == null) 60 return false; 61 return true; 62 } 63 64 65 public static void deleteFullRows() 66 { 67 for(int y = 0; y < h; ++y){ 68 if (isRowFull(y)) 69 { 70 deleteRow(y); 71 } 72 } 73 } 74 75 public static void deleteFullLines() 76 { 77 for (int x = 0; x < w; ++x){ 78 if (isLineFull(x)) 79 { 80 deleteLine(x); 81 } 82 } 83 } 84} 85
よろしくお願いいたします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/09/09 10:39
2017/09/09 10:47
2017/09/09 10:53
2017/09/11 08:31 編集
2017/09/11 13:14
2017/09/12 05:54
2017/09/12 06:10
2017/09/12 06:20