残るはクリア判定とピースの絵を追加でしょうか。
まずクリア判定を作ってみます。
クリアの条件は、全てのピースが上を向くです。
回転のメソッドを作った時に、方向が分かっているので
判定は簡単に作れそうです。
判定用の変数を用意して、ピース毎に上を向いているかチェックさせ
全てが上を向いていればクリアですね。
public class GameManager : MonoBehaviour {
public GameObject[] PieceBt; //ピースの取得
new Transform transform; //トランスフォーム取得用変数
private string[] PieceDire = new string[36]; //各ピースの方向
private bool Judge; //ピースの向きを判定する変数
bool型変数で判定用の変数を追加します。
ピースの向きを判定して、上を向いたらtrueになるようにします。
続いて、判定メソッドを用意します。
void JUDGE()
{
for(int i = 0; i < 36; i++)
{
if (PieceDire[i] == "LEFT" || PieceDire[i] == "UNDER" || PieceDire[i] == "RIGHT")
{
Judge = false;
return;
}
else
{
Judge = true;
}
}
}
判定方法はピースの数だけループを回して上を向いているか確認です。
ただ、ピースを動かすたびにピース全部のチェックをするのも大変なので、
上を向いてないピースの所でループから出るようにしました。
判定メソッドができたので、ゲームクリアの処理も追加してみます。
用意するのは、完成した絵とClearのテキストです。
ピースが全て上を向いたら元の絵を出して、Clearの文字がでるようにします。

Canvasを右クリック UI→Image でイメージを追加します。
追加したイメージはImageClearとしてサイズを600×600にします。
このImageに元になる画像を入れてクリア時に呼び出します。
同じ要領でテキストを追加してClearの文字を表示します。

追加したテキストはTextClearとします。
最後はCanvas内に空オブジェクトを作って、ClearParentとします。
このオブジェクトの中にImageClearとTextClearを入れます。
ClearParentはクリア時に呼び出すのでチェックを外しfalseにしておきます。
これでクリア判定とクリアオブジェクトが用意できたので、
スクリプトを修正します。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
public class GameManager : MonoBehaviour {
public GameObject[] PieceBt; //ピースの取得
public GameObject ClearParent; //クリアオブジェクトの取得
クリアオブジェクトのメンバー変数を追加します。
JUDGE(); //判定メソッド呼び出し
//判定がtrueならクリアオブジェクトを表示する
if (Judge == true)
{
ClearParent.SetActive(true);
}
先ほど作ったJUDGEメソッドとクリアオブジェクトの呼び出しを
PushPieceメソッドのswitch文の下に追加します。
完成したのがこちら
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
public class GameManager : MonoBehaviour {
public GameObject[] PieceBt; //ピースの取得
public GameObject ClearParent; //クリアオブジェクトの取得
new Transform transform; //トランスフォーム取得用変数
private string[] PieceDire = new string[36]; //各ピースの方向変数
private bool Judge; //ピースの向きを判定用変数
// Use this for initialization
void Start () {
//ピースの数だけループを回しランダムに初期位置を決める
for (int i = 0; i < 36; i++)
{
PieceDire[i] = "UP"; //初期段階で上を向いている
int a = UnityEngine.Random.Range(1, 10); //ランダム変数を取得
transform = PieceBt[i].GetComponent(); //移動用コンポーネントの取得
//変数により角度を変える
if (a == 2 || a == 6 || a == 10)
{
PieceDire[i] = "LEFT";
transform.DORotate(new Vector3(0f, 0f, 90), 0f);
}
if (a == 3 || a == 7)
{
PieceDire[i] = "UNDER";
transform.DORotate(new Vector3(0f, 0f, 180), 0f);
}
if (a == 4 || a == 8)
{
PieceDire[i] = "RIGHT";
transform.DORotate(new Vector3(0f, 0f, 270), 0f);
}
}
}
// Update is called once per frame
void Update()
{
}
public void PushPiece(int a)
{
transform = PieceBt[a].GetComponent(); //ピースa番目のTransform情報を取得
//取得したピースa番の向き判定と回転処理
switch (PieceDire[a])
{
case "UP":
transform.DORotate(new Vector3(0f, 0f, 90), 0.3f);
PieceDire[a] = "LEFT";
break;
case "LEFT":
transform.DORotate(new Vector3(0f, 0f, 180), 0.3f);
PieceDire[a] = "UNDER";
break;
case "UNDER":
transform.DORotate(new Vector3(0f, 0f, 270), 0.3f);
PieceDire[a] = "RIGHT";
break;
case "RIGHT":
transform.DORotate(new Vector3(0f, 0f, 360), 0.3f);
PieceDire[a] = "UP";
break;
}
JUDGE(); //判定メソッド呼び出し
//判定がtrueならクリアオブジェクトを表示する
if (Judge == true)
{
ClearParent.SetActive(true);
}
}
//ピースの向きを判定する
void JUDGE()
{
for(int i = 0; i < 36; i++)
{
if (PieceDire[i] == "LEFT" || PieceDire[i] == "UNDER" || PieceDire[i] == "RIGHT")
{
Judge = false;
return;
}
else
{
Judge = true;
}
}
}
}
この記事へのコメント