检测颜色是否匹配

时间:2017-09-19 20:12:14

标签: c# unity3d unity2d

我正在尝试检测Unity2D中的颜色是否匹配(2x2),但它并不完全有效。我有16个瓷砖,有4种不同的颜色。 我正在检测颜色是否与脚本相匹配,脚本附着在每个碰撞器上,该碰撞器与周围的4个瓦片碰撞。在每个colider前面是一个按钮,它将围绕该按钮(对撞机)的4个瓷砖旋转到右边。对撞机的脚本有一个名为childTiles的列表。该列表包含该对撞机周围的4个瓦片,用于检测一个列表中是否有4个相同颜色的瓦片。看起来像这样。 (GIF)https://gyazo.com/a2a5c8a5051e937338807fb5ecfaa4ec

此脚本附加到每个对撞机。总共有9个碰撞器(每个碰撞器都在一个按钮后面)。

public GameObject GameManager;

public List<GameObject> ChildTiles = new List<GameObject>();
public GameObject[] ParentPositions;
//public GameObject Tiles;
public bool canTurn = true;
private TileController parentPositions;
private float _rotationDegrees;
private GameManager gameManager;

public Color Green;
public Color Red;
public Color Blue;
public Color Yellow;
public bool greenComplete = false;
public bool redComplete = false;
public bool blueComplete = false;
public bool yellowComplete = false;

public GameObject Topleft; //colliders (only these colliders are necessary out of the 9)
public GameObject Topright;
public GameObject Bottomleft;
public GameObject Bottomright;

public GameObject tileGreen1;
public GameObject tileRed1;
public GameObject tileBlue1;
public GameObject tileYellow1;

void Awake()
{
    if (gameObject.name == "Top-left" || gameObject.name == "Top-right" || gameObject.name == "Bottom-left" || gameObject.name == "Bottom-right")
    {
        Green = tileGreen1.GetComponent<SpriteRenderer> ().color;
        Red = tileRed1.GetComponent<SpriteRenderer> ().color;
        Blue = tileBlue1.GetComponent<SpriteRenderer> ().color;
        Yellow = tileYellow1.GetComponent<SpriteRenderer> ().color;
    }
    GameManager = GameObject.Find ("GameManager");
    //Tiles = GameObject.Find ("Tiles");
    ParentPositions = GameObject.FindGameObjectsWithTag ("Position");
}

void Update()
{
    greenComplete = parentPositions.greenComplete;
    redComplete = parentPositions.redComplete;
    blueComplete = parentPositions.blueComplete;
    yellowComplete = parentPositions.yellowComplete;
}

void Start()
{

    gameManager = GameManager.GetComponent<GameManager> ();
    _rotationDegrees = 0f;
    foreach (GameObject go in ParentPositions) 
    {
        parentPositions = go.GetComponent<TileController> ();
    }
}


public void SquareClicked()
{
    if (parentPositions.canTurn == true && gameManager.turns > 0f)
        {
        gameManager.turns -= 1;
        gameManager.turnsText.text = "Turns: " + gameManager.turns;
        foreach(GameObject go in ParentPositions)
        {
            parentPositions = go.GetComponent<TileController> ();
            parentPositions.canTurn = false;
        }

        foreach(GameObject tile in ChildTiles)
        {
            tile.transform.parent = gameObject.transform;
        }
        StartCoroutine(Rotate()); //this is where you do the rotation of the parent object.

        if (gameManager.turns == 0f) 
        {
            gameManager.turnsText.text = "No turns left";
        }
    }
}

IEnumerator Rotate()
{
    LeanTween.rotateZ (gameObject, _rotationDegrees - 90f, 0.5f);
    _rotationDegrees -= 90f;
    if (_rotationDegrees == -360f) 
    {
        _rotationDegrees = 0f;
    }
    yield return new WaitForSeconds (0.5f);
    parentPositions.canTurn = true;

    yield return new WaitForSeconds (0.5f);
    if (parentPositions.greenComplete == false) 
    {
        parentPositions.greenComplete = parentPositions.ChildTiles.All (t => t.GetComponent<SpriteRenderer> ().color == Green);
    }
    if (parentPositions.redComplete == false) 
    {
        parentPositions.redComplete = parentPositions.ChildTiles.All (t => t.GetComponent<SpriteRenderer> ().color == Red);
    }
    if (parentPositions.blueComplete == false) 
    {
        parentPositions.blueComplete = parentPositions.ChildTiles.All (t => t.GetComponent<SpriteRenderer> ().color == Blue);
    }
    if (parentPositions.yellowComplete == false) 
    {
        parentPositions.yellowComplete = parentPositions.ChildTiles.All (t => t.GetComponent<SpriteRenderer> ().color == Yellow);
    }

    Debug.Log ("_greenComplete " + parentPositions.greenComplete);
    Debug.Log ("_redComplete " + parentPositions.redComplete);
    Debug.Log ("_blueComplete " + parentPositions.blueComplete);
    Debug.Log ("_yellowComplete " + parentPositions.yellowComplete);

    if (parentPositions.greenComplete && parentPositions.redComplete && parentPositions.blueComplete && parentPositions.yellowComplete) 
    {
        Debug.Log ("Level Complete");
    }

}

void OnTriggerEnter2D(Collider2D col)
{
    if (col.gameObject.tag == "Tile") 
    {
        ChildTiles.Add (col.gameObject);
    }
}

void OnTriggerExit2D(Collider2D col)
{
    if (col.gameObject.tag == "Tile") 
    {
        ChildTiles.Remove (col.gameObject);
    }
}

正如您在GIF中看到的那样,存在一个问题。问题是当有4种颜色匹配时,它不会返回True。如果你点击一个周围有4个相同颜色的图块的按钮,它只返回True。只有这样才返回True。因此,您匹配4种颜色,它不会返回True,您单击其周围有4种颜色的按钮,它将返回True。如果匹配4种颜色,如何使其立即返回True?如您所见,我只使用左上角,右上角,左下角和右下角对撞机,因为这些是唯一可能同时具有4个相同颜色的瓷砖的碰撞器。

0 个答案:

没有答案