Search Unity

No matches on Start Help

Discussion in 'Scripting' started by JordanCrosby, Jul 28, 2017.

  1. JordanCrosby

    JordanCrosby

    Joined:
    Jul 2, 2015
    Posts:
    4
    Hello,

    Im currently looking for some help with my match 3 code.. I would like it so when my pieces spawn they don't spawn automatic matches.




    Code (CSharp):
    1.  
    2.  
    3. public enum PieceType
    4.     {
    5.         EMPTY,  // Empty shell used when pieces game starts
    6.         NORMAL,  // the basic piece
    7.         BREAKABLE,  // An obstacle piece that can be destroyed
    8.         UNBREAKABLE, // Pieces that cannot be broken
    9.         ROW_CLEAR,     // Clears Row
    10.         COLUMN_CLEAR,  // Clears Column
    11.         COLORMATCH,       // Clears all of a color
    12.         COUNT,
    13.     };
    14.  
    15.     [System.Serializable]
    16.     public struct PiecePrefab
    17.     {
    18.         public PieceType type;
    19.         public GameObject prefab;
    20.     };
    21.  
    22.     [System.Serializable]
    23.     public struct PiecePosition
    24.     {
    25.         public PieceType type;
    26.         public int x;
    27.         public int y;
    28.     };
    29.  
    30.     // Grid Size
    31.     public int xDim; // The horizontal size of the grid
    32.     public int yDim; // The vertical size of the grid
    33.  
    34.     // Time to fill / refill the grid
    35.     public float fillTime; // Higher Fill time makes board fill slower
    36.  
    37.     //Used to call Level Class
    38.     public Level level;   // Reference to Level
    39.  
    40.     // Array of piece types that can be defined in the inspector
    41.     public PiecePrefab[] piecePrefabs;
    42.     // GameObject to set a background for our pieces
    43.     public GameObject backgroundPrefab;
    44.  
    45.     // Used to set our initial pieces in the inspector, for each level
    46.     public PiecePosition[] initialPieces;
    47.  
    48.     // Dictionary for attaching a piece to its prefab
    49.     private Dictionary<PieceType, GameObject> piecePrefabDict;
    50.  
    51.     // 2D array to contain all our pieces
    52.     private GamePiece[,] pieces;
    53.  
    54.     // Used to move pieces in diag (for filling a grid with obstacles)
    55.     private bool inverse = false;
    56.  
    57.     // Used to know wich pieces will be swapped
    58.     private GamePiece pressedPiece;
    59.     private GamePiece enteredPiece;
    60.  
    61.     // Set the game over state
    62.     private bool gameOver = false;
    63.  
    64.     private bool isFilling = false;
    65.  
    66.     public bool IsFilling
    67.     {
    68.         get { return isFilling; }
    69.     }
    70.  
    71.  
    72.     // Use this for initialization
    73.     void Awake () {
    74.  
    75.         piecePrefabDict = new Dictionary<PieceType, GameObject>();
    76.  
    77.         // First we need to check if we have keys in our dictionary. If not, add them
    78.         for ( int i = 0; i < piecePrefabs.Length; i++)
    79.         {
    80.             if ( !piecePrefabDict.ContainsKey (piecePrefabs .type))
    81.             {
    82.                 piecePrefabDict.Add(piecePrefabs.type, piecePrefabs.prefab);
    83.             }
    84.      
    85.         }
    86.  
    87.         //Building the Grid the blank piece background
    88.         for (int x = 0; x < xDim; x++)
    89.         {
    90.             for (int y = 0; y < yDim; y++)
    91.             {
    92.                 GameObject background = (GameObject)Instantiate(backgroundPrefab, GetWorldPosition(x, y), Quaternion.identity);
    93.                 background.transform.parent = transform;
    94.             }
    95.         }
    96.  
    97.         // Now we need to add our initial and empty pieces on the grid
    98.         pieces = new GamePiece [xDim, yDim];
    99.  
    100.         // Filling the Board
    101.         for ( int i = 0; i < initialPieces.Length; i++)
    102.         {
    103.             if ( initialPieces .x >= 0 && initialPieces .x < xDim
    104.                 && initialPieces .y >= 0 && initialPieces .y < yDim)
    105.             {
    106.                 SpawnNewPiece(initialPieces.x, initialPieces.y, initialPieces.type);
    107.             }
    108.         }
    109.  
    110.         for ( int x = 0; x< xDim; x++)
    111.         {
    112.             for ( int y = 0; y < yDim; y++)
    113.             {
    114.                 if (pieces[x, y] == null)
    115.                 {
    116.                     SpawnNewPiece(x, y, PieceType.EMPTY);
    117.                 }
    118.          
    119.             }
    120.         }
    121.  
    122.         // Now the grid is filled with empty pieces, we need to replace them by our non-empty pieces with animation
    123.         StartCoroutine(Fill());
    124.  
    125.     }
    126.  
    127.     // Update is called once per frame
    128.     void Update () {
    129.  
    130.     }
    131.  
    132.     //
    133.     // Fill the grid
    134.     //
    135.     public IEnumerator Fill()
    136.     {
    137.         bool needsRefill = true;
    138.         isFilling = true;
    139.  
    140.         while (needsRefill)
    141.         {
    142.             yield return new WaitForSeconds(fillTime);
    143.  
    144.             while (FillStep())
    145.             {
    146.                 inverse = !inverse;
    147.                 yield return new WaitForSeconds(fillTime);
    148.             }
    149.  
    150.             needsRefill = ClearAllValidMatches();
    151.         }
    152.  
    153.         //   Old code    //isFilling = false;
    154.  
    155.         //-------------------------------------------------
    156.         CheckAllPossibleMatches();
    157.         //-------------------------------------------------
    158.     }
    159.  
    160.     // Move a piece to fill the grid
    161.     public bool FillStep()
    162.     {
    163.         bool movedPiece = false;
    164.  
    165.         for (int y = yDim - 2; y >= 0; y--)
    166.         {
    167.             for (int loopX = 0; loopX < xDim; loopX++)
    168.             {
    169.                 int x = loopX;
    170.  
    171.                 if (inverse)
    172.                 {
    173.                     x = xDim - 1 - loopX;
    174.                 }
    175.  
    176.                 GamePiece piece = pieces[x, y];
    177.  
    178.                 if (piece.IsMovable())
    179.                 {
    180.                     GamePiece pieceBelow = pieces[x, y + 1];
    181.  
    182.                     if (pieceBelow.Type == PieceType.EMPTY)
    183.                     {
    184.                         Destroy(pieceBelow.gameObject);
    185.                         piece.MovableComponent.Move(x, y + 1, fillTime);
    186.                         pieces[x, y + 1] = piece;
    187.                         SpawnNewPiece(x, y, PieceType.EMPTY);
    188.                         movedPiece = true;
    189.                     }
    190.                     else
    191.                     {
    192.                         for (int diag = -1; diag <= 1; diag++)
    193.                         {
    194.                             if (diag != 0)
    195.                             {
    196.                                 int diagX = x + diag;
    197.  
    198.                                 if (inverse)
    199.                                 {
    200.                                     diagX = x - diag;
    201.                                 }
    202.  
    203.                                 if (diagX >= 0 && diagX < xDim)
    204.                                 {
    205.                                     GamePiece diagonalPiece = pieces[diagX, y + 1];
    206.  
    207.                                     if (diagonalPiece.Type == PieceType.EMPTY)
    208.                                     {
    209.                                         bool hasPieceAbove = true;
    210.  
    211.                                         for (int aboveY = y; aboveY >= 0; aboveY--)
    212.                                         {
    213.                                             GamePiece pieceAbove = pieces[diagX, aboveY];
    214.  
    215.                                             if (pieceAbove.IsMovable())
    216.                                             {
    217.                                                 break;
    218.                                             }
    219.                                             else if (!pieceAbove.IsMovable() && pieceAbove.Type != PieceType.EMPTY)
    220.                                             {
    221.                                                 hasPieceAbove = false;
    222.                                                 break;
    223.                                             }
    224.                                         }
    225.  
    226.                                         if (!hasPieceAbove)
    227.                                         {
    228.                                             Destroy(diagonalPiece.gameObject);
    229.                                             piece.MovableComponent.Move(diagX, y + 1, fillTime);
    230.                                             pieces[diagX, y + 1] = piece;
    231.                                             SpawnNewPiece(x, y, PieceType.EMPTY);
    232.                                             movedPiece = true;
    233.                                             break;
    234.                                         }
    235.                                     }
    236.                                 }
    237.                             }
    238.                         }
    239.                     }
    240.  
    241.                 }
    242.             }
    243.  
    244.         }
    245.  
    246.         // Filling Top row
    247.         for (int x = 0; x < xDim; x++)
    248.         {
    249.             GamePiece pieceBelow = pieces[x, 0];
    250.  
    251.             if (pieceBelow.Type == PieceType.EMPTY)
    252.             {
    253.                 Destroy(pieceBelow.gameObject);
    254.  
    255.                 // This is where we fill the board
    256.                 GameObject newPiece = (GameObject)Instantiate(piecePrefabDict[PieceType.NORMAL], GetWorldPosition(x, -1), Quaternion.identity);
    257.                 newPiece.transform.parent = transform;
    258.  
    259.                 pieces[x, 0] = newPiece.GetComponent<GamePiece>();
    260.                 pieces[x, 0].Init(x, -1, this, PieceType.NORMAL);
    261.                 pieces[x, 0].MovableComponent.Move(x, 0, fillTime);
    262.  
    263.                 // This is where we set color that spawns
    264.                 pieces[x, 0].ColorComponent.SetColor((ColorPiece.ColorType)Random.Range(0, pieces[x, 0].ColorComponent.NumColors));
    265.  
    266.  
    267.                 movedPiece = true;
    268.             }
    269.         }
    270.  
    271.         return movedPiece;
    272.     }
    273.  
    274.     // Used to get the offset of our pieces
    275.     public Vector2 GetWorldPosition(int x, int y)
    276.     {
    277.         return new Vector2(transform.position.x - xDim / 2.0f + x, transform.position.y + yDim / 2.0f - y);
    278.     }
    279.  
    280.     //Spawn new Piece
    281.     public GamePiece SpawnNewPiece (int x, int y, PieceType type)
    282.     {
    283.         GameObject newPiece = (GameObject)Instantiate(piecePrefabDict[type], GetWorldPosition (x, y), Quaternion.identity);
    284.         newPiece.transform.parent = transform;
    285.  
    286.         pieces [x, y] = newPiece.GetComponent<GamePiece>();
    287.         pieces [x, y].Init(x, y, this, type);
    288.  
    289.         return pieces[x, y];
    290.     }
    291.  
    292.     // Used to know if piece1 is adjacent to piece2
    293.     public bool IsAdjacent ( GamePiece piece1, GamePiece piece2)
    294.     {
    295.         return (piece1.X == piece2.X && (int)Mathf.Abs(piece1.Y - piece2.Y) == 1)
    296.             || (piece1.Y == piece2.Y && (int)Mathf.Abs(piece1.X - piece2.X) == 1);
    297.     }
    298.  
    299.  
    300.    
    301.  
    302.     // Used to know if pieces can match
    303.     public List <GamePiece> GetMatch ( GamePiece piece, int newX, int newY)
    304.     {
    305.         if (piece.IsColored())
    306.         {
    307.             ColorPiece.ColorType color = piece.ColorComponent.Color;
    308.             List <GamePiece> horizontalPieces = new List<GamePiece>();
    309.             List <GamePiece> verticalPieces = new List<GamePiece>();
    310.             List <GamePiece> matchingPieces = new List<GamePiece>();
    311.  
    312.             // First check Horizontal
    313.             horizontalPieces.Add(piece);
    314.  
    315.             for ( int dir = 0; dir<= 1; dir ++)
    316.             {
    317.                 for ( int xOffset = 1; xOffset < xDim; xOffset++)
    318.                 {
    319.                     int x;
    320.  
    321.                     if ( dir == 0)
    322.                     {
    323.                         // Left
    324.                         x = newX - xOffset;
    325.                     }
    326.                     else
    327.                     {
    328.                         // Right
    329.                         x = newX + xOffset;
    330.                     }
    331.                     if ( x < 0 || x >= xDim)
    332.                     {
    333.                         break;
    334.                     }
    335.  
    336.                     if ( pieces [ x, newY].IsColored() && pieces[x, newY].ColorComponent.Color == color)
    337.                     {
    338.                         horizontalPieces.Add(pieces[x, newY]);
    339.                     }
    340.                     else
    341.                     {
    342.                         break;
    343.                     }
    344.                 }
    345.             }
    346.  
    347.             if ( horizontalPieces.Count >= 3)
    348.             {
    349.                 for ( int i = 0; i < horizontalPieces.Count; i++)
    350.                 {
    351.                     matchingPieces.Add(horizontalPieces);
    352.                 }
    353.             }
    354.  
    355.             // Traverse vertically if we found a match ( for L and T shape mathces)
    356.             if ( horizontalPieces.Count >= 3)
    357.             {
    358.                 for ( int i = 0; i < horizontalPieces.Count; i++)
    359.                 {
    360.                     for ( int dir = 0; dir <= 1; dir++)
    361.                     {
    362.                         for( int yOffset = 1; yOffset< yDim; yOffset ++)
    363.                         {
    364.                             int y;
    365.  
    366.                             if ( dir == 0)
    367.                             {
    368.                                 // Up
    369.                                 y = newY - yOffset;
    370.                             }
    371.                             else
    372.                             {
    373.                                 // Down
    374.                                 y = newY + yOffset;
    375.                             }
    376.  
    377.                             if ( y < 0 || y >= yDim)
    378.                             {
    379.                                 break;
    380.                             }
    381.  
    382.                             if (pieces[horizontalPieces.X, y].IsColored() && pieces[horizontalPieces.X, y].ColorComponent.Color == color)
    383.                             {
    384.                                 verticalPieces.Add(pieces[horizontalPieces.X, y]);
    385.                             }
    386.                             else
    387.                             {
    388.                                 break;
    389.                             }
    390.                         }
    391.                     }
    392.  
    393.                     if ( verticalPieces.Count < 2)
    394.                     {
    395.                         verticalPieces.Clear();
    396.                     }
    397.                     else
    398.                     {
    399.                         for ( int j = 0; j < verticalPieces.Count; j++)
    400.                         {
    401.                             matchingPieces.Add(verticalPieces[j]);
    402.                         }
    403.  
    404.                         break;
    405.                     }
    406.                 }
    407.             }
    408.  
    409.             if ( matchingPieces.Count >= 3)
    410.             {
    411.                 return matchingPieces;
    412.             }
    413.  
    414.             // Then check Vertical
    415.             horizontalPieces.Clear();
    416.             verticalPieces.Clear();
    417.  
    418.             verticalPieces.Add(piece);
    419.  
    420.             for (int dir = 0; dir <= 1; dir++)
    421.             {
    422.                 for (int yOffset = 1; yOffset < xDim; yOffset++)
    423.                 {
    424.                     int y;
    425.  
    426.                     if (dir == 0)
    427.                     {
    428.                         // Left
    429.                         y = newY - yOffset;
    430.                     }
    431.                     else
    432.                     {
    433.                         // Right
    434.                         y = newY + yOffset;
    435.                     }
    436.                     if (y < 0 || y >= yDim)
    437.                     {
    438.                         break;
    439.                     }
    440.  
    441.                     if (pieces[newX, y].IsColored() && pieces[newX, y].ColorComponent.Color == color)
    442.                     {
    443.                         verticalPieces.Add(pieces[newX, y]);
    444.                     }
    445.                     else
    446.                     {
    447.                         break;
    448.                     }
    449.                 }
    450.             }
    451.  
    452.             if (verticalPieces.Count >= 3)
    453.             {
    454.                 for (int i = 0; i < verticalPieces.Count; i++)
    455.                 {
    456.                     matchingPieces.Add(verticalPieces);
    457.                 }
    458.             }
    459.  
    460.             // Traverse horizontaly if we found a match ( for L and T shape mathces)
    461.             if (verticalPieces.Count >= 3)
    462.             {
    463.                 for (int i = 0; i < verticalPieces.Count; i++)
    464.                 {
    465.                     for (int dir = 0; dir <= 1; dir++)
    466.                     {
    467.                         for (int xOffset = 1; xOffset < xDim; xOffset++)
    468.                         {
    469.                             int x;
    470.  
    471.                             if (dir == 0)
    472.                             {
    473.                                 // Left
    474.                                 x = newX - xOffset;
    475.                             }
    476.                             else
    477.                             {
    478.                                 // Right
    479.                                 x = newX + xOffset;
    480.                             }
    481.  
    482.                             if (x < 0 || x >= xDim)
    483.                             {
    484.                                 break;
    485.                             }
    486.  
    487.                             if (pieces[x, verticalPieces.Y].IsColored() && pieces[x, verticalPieces.Y].ColorComponent.Color == color)
    488.                             {
    489.                                 horizontalPieces.Add(pieces[x, verticalPieces.Y]);
    490.                             }
    491.                             else
    492.                             {
    493.                                 break;
    494.                             }
    495.                         }
    496.                     }
    497.  
    498.                     if (horizontalPieces.Count < 2)
    499.                     {
    500.                         horizontalPieces.Clear();
    501.                     }
    502.                     else
    503.                     {
    504.                         for (int j = 0; j < horizontalPieces.Count; j++)
    505.                         {
    506.                             matchingPieces.Add(horizontalPieces[j]);
    507.                         }
    508.  
    509.                         break;
    510.                     }
    511.                 }
    512.             }
    513.  
    514.             if (matchingPieces.Count >= 3)
    515.             {
    516.                 return matchingPieces;
    517.             }
    518.         }
    519.  
    520.         return null;
    521.     }
    522.  
    523.     // Clears all Matches
    524.     public bool ClearAllValidMatches()
    525.     {
    526.         bool needsRefill = false;
    527.  
    528.         for ( int y = 0; y < yDim; y++)
    529.         {
    530.             for ( int x = 0; x < xDim; x++)
    531.             {
    532.                 if ( pieces [x, y].IsClearable())
    533.                 {
    534.                     List<GamePiece> match = GetMatch(pieces[x, y], x, y);
    535.  
    536.                     if ( match != null)
    537.                     {
    538.  
    539.                         //Special Piece Spawn
    540.                         PieceType specialPieceType = PieceType.COUNT;
    541.                         GamePiece randomPiece = match[Random.Range(0, match.Count)];
    542.  
    543.                         int specialPieceX = randomPiece.X;
    544.                         int specialPieceY = randomPiece.Y;
    545.  
    546.                         if ( match.Count == 4)
    547.                         {
    548.                             if (pressedPiece == null || enteredPiece == null)
    549.                             {
    550.                                 specialPieceType = (PieceType)Random.Range((int)PieceType.ROW_CLEAR, (int)PieceType.COLUMN_CLEAR);
    551.                             }
    552.                             else if (pressedPiece.Y == enteredPiece.Y)
    553.                             {
    554.                                 specialPieceType = PieceType.ROW_CLEAR;
    555.                             }
    556.                             else
    557.                             {
    558.                                 specialPieceType = PieceType.COLUMN_CLEAR;
    559.                             }
    560.                         }
    561.  
    562.                         else if ( match.Count >= 5)
    563.                         {
    564.                             specialPieceType = PieceType.COLORMATCH;
    565.                         }
    566.  
    567.                         for ( int i = 0; i < match.Count; i++)
    568.                         {
    569.                             if ( ClearPiece ( match .X, match .Y))
    570.                             {
    571.                                 needsRefill = true;
    572.  
    573.                                 if ( match[i] == pressedPiece || match [i] == enteredPiece)
    574.                                 {
    575.                                     specialPieceX = match[i].X;
    576.                                     specialPieceY = match[i].Y;
    577.                                 }
    578.                             }
    579.                         }
    580.  
    581.                         //Spawn Special Piece
    582.                         if (specialPieceType != PieceType.COUNT)
    583.                         {
    584.                             Destroy(pieces[specialPieceX, specialPieceY]);
    585.                             GamePiece newPiece = SpawnNewPiece(specialPieceX, specialPieceY, specialPieceType);
    586.  
    587.                             if (( specialPieceType == PieceType.ROW_CLEAR || specialPieceType == PieceType.COLUMN_CLEAR)
    588.                                 && newPiece.IsColored() && match[0].IsColored())
    589.                             {
    590.                                 newPiece.ColorComponent.SetColor(match[0].ColorComponent.Color);
    591.                             }
    592.                             else if ( specialPieceType == PieceType.COLORMATCH && newPiece.IsColored())
    593.                             {
    594.                                 newPiece.ColorComponent.SetColor(ColorPiece.ColorType.ANY);
    595.                             }
    596.                         }
    597.                     }
    598.                 }
    599.             }
    600.         }
    601.  
    602.         return needsRefill;
    603.     }
    604.  
    605.     }



    }
    [/I][/I][/I][/I]
     
    Last edited: Jul 28, 2017
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,334
    That is like a million lines of code, and there's no formatting. Please just post the part that's not working as expected, and use code tags.
     
  3. JordanCrosby

    JordanCrosby

    Joined:
    Jul 2, 2015
    Posts:
    4
    thank you for your reply.
    I apologize for the mass amount of code and lack of formatting, it was late when i posted this.

    I have delete much of the unnecessary code and formatted the rest..

    My issue is not code not working properly its I do not know how to make it so when spawning tiles at game start they don't drop down in sets of 3 or more.. I am having issues setting my score goal because sometimes you load level and so many matches drop that you start with a high score and a few special pieces and sometime you get nothing.