Exemplo n.º 1
0
    // random build a tile
    void RandowmBuildTile()
    {
        List <GridIndexPair> emptyGridsIndexPairs = new List <GridIndexPair>();

        for (int i = 0; i < maxRows; i++)
        {
            for (int j = 0; j < maxColumns; j++)
            {
                if (!gridState[i, j])
                {
                    GridIndexPair pair;
                    pair.Row    = i;
                    pair.Column = j;
                    emptyGridsIndexPairs.Add(pair);
                }
            }
        }
        if (emptyGridsIndexPairs.Count == 0)
        {
            return;
        }
        else
        {
            int           r         = Random.Range(0, emptyGridsIndexPairs.Count);
            GridIndexPair indexPair = emptyGridsIndexPairs[r];
            if (Random.Range(0.0f, 9.9f) > 8.0f)
            {
                gridState[indexPair.Row, indexPair.Column] = Instantiate(tiles[1], GetWorldPositionByGridIndex(indexPair), Quaternion.identity) as GameObject;
            }
            else
            {
                gridState[indexPair.Row, indexPair.Column] = Instantiate(tiles[0], GetWorldPositionByGridIndex(indexPair), Quaternion.identity) as GameObject;
            }
        }
    }
Exemplo n.º 2
0
    // return position by grid index
    Vector2 GetWorldPositionByGridIndex(GridIndexPair indexPair)
    {
        Vector2 worldPosition = Vector2.zero;

        worldPosition.x = firstPosition.x + indexPair.Column * (tileWidth + borderWidth);
        worldPosition.y = firstPosition.y - indexPair.Row * (tileWidth + borderWidth);
        return(worldPosition);
    }
Exemplo n.º 3
0
    // merge same tile
    void MergeTwoSameTile(GameObject tileA, GameObject tileB, GridIndexPair oldIndexPair, GridIndexPair targetIndexPair)
    {
        GameObject go = Instantiate(tiles [tileA.GetComponent <Tiles> ().nextIndex],
                                    GetWorldPositionByGridIndex(targetIndexPair),
                                    Quaternion.identity) as GameObject;

        Destroy(tileA.gameObject);
        Destroy(tileB.gameObject);
        gridState [targetIndexPair.Row, targetIndexPair.Column] = go;
        gridState [oldIndexPair.Row, oldIndexPair.Column]       = null;

        // set current turn
        go.GetComponent <Tiles> ().mergeInThisTurn = true;

        // check if merge a tile with value 2048
        if (go.GetComponent <Tiles>().value == 2048)
        {
            gameState = GameState.GameWin;
        }
    }
Exemplo n.º 4
0
 // move the tile to a new place
 void MoveToNewPlace(GameObject tileA, GridIndexPair oldIndex, GridIndexPair newIndex)
 {
     tileA.transform.position = GetWorldPositionByGridIndex(newIndex);
     gridState [newIndex.Row, newIndex.Column] = tileA;
     gridState [oldIndex.Row, oldIndex.Column] = null;
 }
Exemplo n.º 5
0
	// return position by grid index
	Vector2 GetWorldPositionByGridIndex(GridIndexPair indexPair){
		Vector2 worldPosition = Vector2.zero;
		worldPosition.x = firstPosition.x + indexPair.Column * (tileWidth + borderWidth);
		worldPosition.y = firstPosition.y - indexPair.Row * (tileWidth + borderWidth);
		return worldPosition;
	}
Exemplo n.º 6
0
	// move the tile to a new place
	void MoveToNewPlace(GameObject tileA, GridIndexPair oldIndex, GridIndexPair newIndex){
		tileA.transform.position = GetWorldPositionByGridIndex (newIndex);
		gridState [newIndex.Row, newIndex.Column] = tileA;
		gridState [oldIndex.Row, oldIndex.Column] = null;
	}
Exemplo n.º 7
0
	// merge same tile
	void MergeTwoSameTile(GameObject tileA, GameObject tileB,GridIndexPair oldIndexPair ,GridIndexPair targetIndexPair){
		GameObject go = Instantiate (tiles [tileA.GetComponent<Tiles> ().nextIndex],
		                            GetWorldPositionByGridIndex (targetIndexPair),
		                            Quaternion.identity) as GameObject;
		Destroy (tileA.gameObject);
		Destroy (tileB.gameObject);
		gridState [targetIndexPair.Row, targetIndexPair.Column] = go;
		gridState [oldIndexPair.Row, oldIndexPair.Column] = null;

		// set current turn
		go.GetComponent<Tiles> ().mergeInThisTurn = true;

		// check if merge a tile with value 2048
		if(go.GetComponent<Tiles>().value == 2048)
			gameState = GameState.GameWin;
	}