Match Game Mechanics
This example shows how to implement basic matching mechanics.
It has two scripts; one for a custom cell, and a grid behaviour for the main logic. The cell has some maintenance fields, but is otherwise quite dumb. Keeping cells dumb is good practice. It helps centralize logic, and make cells more re-usable.
The grid behaviour is simple. It initializes cells to random values. When a cell is clicked, it tries to find a match. If the match is big enough (3 cells or more), the match cells are destroyed. New cells are created as necessary, and cells fall into the empty spaces. This last bit is the trickiest. For cells already in the grid, we count the number of empty spaces below each cell. We then lerp the cell from its current position to the correct position downwards (based on the number of empty cells below). We also count the number of empty cells in each column, and generate as many new cells. These cells are position above the grid, and made to fall down similar to the other cells.
To run the example:
- Import it into a new project.
- Import Grids 1.8.
- Open the scene MatchGame in the MatchGame folder.
- Run it.