質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

Q&A

解決済

1回答

2945閲覧

Unity 2Dでパズルを縦と横同時に消したい

sumikko6210

総合スコア138

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

0グッド

0クリップ

投稿2017/09/22 08:34

イメージ説明

###前提・実現したいこと
Unity 2Dでドラッグ&ドロップではめていくパズルを作っており、縦と横の列が同時に揃ったとき、十字に消したいです。

###発生している問題・エラーメッセージ
縦と横の列が同時に揃ったときに横の列だけが消えてしまっている状況です。

###該当のソースコード

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 10 private Character owner; //持ち主キャラ 11 private int spawnerID = -1; //スポーン元のスポーナー番号 12 13 public bool delete = false; 14 15 //座標が範囲内であるか,Gridに値があるかどうかを判定 16 public bool isValidGridPos() 17 { 18 foreach (Transform child in transform) 19 { 20 Vector2 v = Grid.roundVec2(child.position); 21 22 if (!Grid.insideBorder(v)) 23 return false; 24 25 if (Grid.grid[(int)v.x, (int)v.y] != null && 26 Grid.grid[(int)v.x, (int)v.y].parent != transform) 27 return false; 28 } 29 return true; 30 } 31 32 void updateGrid() 33 { 34 for (int y = 0; y < Grid.h; ++y) 35 for (int x = 0; x < Grid.w; ++x) 36 if (Grid.grid[x, y] != null) 37 if (Grid.grid[x, y].parent == transform) 38 Grid.grid[x, y] = null; 39 40 foreach (Transform child in transform) 41 { 42 Vector2 v = Grid.roundVec2(child.position); 43 Grid.grid[(int)v.x, (int)v.y] = child; 44 } 45 } 46 47 //オブジェクトをクリックする 48 void OnMouseDown() 49 { 50 // マウスカーソルは、スクリーン座標なので、 51 // 対象のオブジェクトもスクリーン座標に変換してから計算する。 52 53 // このオブジェクトの位置(transform.position)をスクリーン座標に変換。 54 screenPoint = Camera.main.WorldToScreenPoint(transform.position); 55 // ワールド座標上の、マウスカーソルと、対象の位置の差分。 56 offset = transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z)); 57 58 //スケールを大きく 59 transform.localScale = new Vector3(1.0f, 1.0f, 1.0f); 60 61 } 62 63 //オブジェクトをドラッグする 64 void OnMouseDrag() 65 { 66 Vector3 currentScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z); 67 Vector3 currentPosition = Camera.main.ScreenToWorldPoint(currentScreenPoint) + this.offset; 68 transform.position = currentPosition; 69 } 70 71 //オブジェクトをドロップする 72 void OnMouseUp() 73 { 74 Vector3 currentScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z); 75 Vector3 currentPosition = Camera.main.ScreenToWorldPoint(currentScreenPoint) + this.offset; 76 77 // 消えた列数リセット ///////////////////////////////////////////////// 78 Count count = GameObject.Find("Text").GetComponent<Count>(); 79 count.ResetCount(); 80 81 //currentPositionが範囲内であるか,Gridに値があるかどうかを判定 82 if (isValidGridPos()) 83 { 84 // It's valid. Update grid. 85 updateGrid(); 86 87 transform.position = Grid.roundVec2(currentPosition); 88 89 //置いたパズルが常に下にくるようにする 90 Vector3 pos = transform.position; 91 transform.position = new Vector3(pos.x, pos.y, pos.z+1); 92 93 //攻撃情報 94 //ピースの数(子オブジェクトの数)を判定 95 int ObjCount = this.transform.childCount; 96 97 BattleManager bm = GameObject.FindGameObjectWithTag("BattleManager").GetComponent<BattleManager>(); 98 bm.SetAttackInfo(ObjCount, owner, 0, 0, 0, BattleManager.TYPE.SPRING, 0, 0, 0, 0, 0); 99 bm.BlockSet(); 100 101 for (int y = 0; y < Grid.h; ++y) 102 for (int x = 0; x < Grid.w; ++x) 103 104 if (Grid.isRowFull(y) || Grid.isLineFull(x)) 105 { 106 delete = true; 107 } 108 109 if (delete) 110 { 111 Grid.deleteFullRows(); 112 Grid.deleteFullLines(); 113 } 114 115 //消えた列数をテキストに反映 116 count.SetText(); 117 118 //指定範囲内ではブロックの当たり判定を無くす 119 BoxCollider2D collider = GetComponent<BoxCollider2D>(); 120 collider.enabled = false; 121 122 //次のブロックを呼び出す 123 FindObjectOfType<SpawnerManager>().requestSpawn(spawnerID); 124 } 125 else 126 { 127 //Spawnerオブジェクトの座標を取る 128 foreach(GameObject go in GameObject.FindGameObjectsWithTag("Spawner")) 129 { 130 if (spawnerID == go.GetComponent<Spawner>().SpawnerID) 131 { 132 transform.position = go.transform.position; 133 transform.localScale = new Vector3(0.3f, 0.3f, 1.0f); 134 } 135 } 136 137 } 138 139 } 140 141 142 // Use this for initialization 143 void Start() 144 { 145 146 } 147 148 // Update is called once per frame 149 void Update() 150 { 151 152 } 153 154 //スポーなーIDを設定 155 public void SetSpawnerID(int id) 156 { 157 spawnerID = id; 158 } 159 160 //持ち主を設定 161 public void SetOwner(Character character) 162 { 163 owner = character; 164 } 165} 166 167

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 deleteReset() 28 { 29 for (int y = 0; y < h; ++y) 30 for (int x = 0; x < w; ++x) 31 32 if (grid[x, y] != null) 33 { 34 Destroy(grid[x, y].gameObject); 35 grid[x, y] = null; 36 } 37 } 38 39 //横列を消去 40 public static void deleteRow(int y) 41 { 42 for(int x = 0; x < w; ++x) 43 { 44 Destroy(grid[x, y].gameObject); 45 grid[x, y] = null; 46 } 47 } 48 49 //縦列を消去 50 public static void deleteLine(int x) 51 { 52 for (int y = 0; y < h; ++y) 53 { 54 Destroy(grid[x, y].gameObject); 55 grid[x, y] = null; 56 } 57 } 58 59 //横列が埋まっているかどうかを判定 60 public static bool isRowFull(int y) 61 { 62 for (int x = 0; x < w; ++x) 63 if (grid[x, y] == null) 64 return false; 65 return true; 66 } 67 68 //縦列が埋まっているかどうかを判定 69 public static bool isLineFull(int x) 70 { 71 for (int y = 0; y < h; ++y) 72 if (grid[x, y] == null) 73 return false; 74 return true; 75 } 76 77 78 public static void deleteFullRows() 79 { 80 for(int y = 0; y < h; ++y){ 81 if (isRowFull(y)) 82 { 83 deleteRow(y); 84 Character test = GameObject.FindGameObjectWithTag("Party").GetComponent<Party>().GetMember(Party.PARTY_MEMBER.LEADER); 85 BattleManager bm = GameObject.FindGameObjectWithTag("BattleManager").GetComponent<BattleManager>(); 86 bm.SetAttackInfo(0, test, 1, 0, 0, BattleManager.TYPE.SPRING, 0, 0, 0, 0, 0); 87 bm.DeleteLine(); 88 89 // 消えた列数カウントアップ /////////////////////////////////// 90 Count count = GameObject.Find("Text").GetComponent<Count>(); 91 count.CountUp(); 92 } 93 } 94 } 95 96 public static void deleteFullLines() 97 { 98 for (int x = 0; x < w; ++x){ 99 if (isLineFull(x)) 100 { 101 deleteLine(x); 102 Character test = GameObject.FindGameObjectWithTag("Party").GetComponent<Party>().GetMember(Party.PARTY_MEMBER.LEADER); 103 BattleManager bm = GameObject.FindGameObjectWithTag("BattleManager").GetComponent<BattleManager>(); 104 bm.SetAttackInfo(0, test, 1, 0, 0, BattleManager.TYPE.SPRING, 0, 0, 0, 0, 0); 105 bm.DeleteLine(); 106 107 // 消えた列数カウントアップ /////////////////////////////////// 108 Count count = GameObject.Find("Text").GetComponent<Count>(); 109 count.CountUp(); 110 111 } 112 } 113 } 114 115 116 117 118} 119

###試したこと
まず列が埋まっているとこを探し出してから、その列を一気に消す処理に変更したいのですが、うまくできないです。

ご回答よろしくお願いいたします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

  1. gridと同じサイズ(8x8)のbool型の配列を用意し、全てfalseにする
  2. 横一列そろったとき、すぐに消さずに、先ほど用意した配列の該当箇所をtrueにする
  3. 縦一列そろったときも同様に処理する
  4. 全ての判定が終わったら、trueにした箇所を消す
  5. 配列を全てfalseに戻す

投稿2017/09/24 09:49

fiveHundred

総合スコア9801

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問