Highlighting valid positions

bandicam 2016-01-22 15-22-39-459

This tutorial will show you how to highlight grid cells which are available positions for the player to move to.

Positions1

This example game has the following rules:

  • Red starts.
  • One turn per player.
  • You can only move to empty cells at specific distances.
  • You can’t capture enemy pieces.

The distance that pieces can move can be changed in the Inspector of the Logical piece GameObject.

Positions2.

Before starting the tutorial we recommend you download the example and test changing this value.

Make the initialization game script: As in any game we need to do the normal script you would do to initialize the game, the difference on this one is that the GridGameManager variable must be static in order to be access for the other two Scripts. On the Game example this script is called HighlightValidPositionsGame.

Make a custom move rule script: By making a script that extend from RelativeMoveRule and re-implement the GetvalidDestinationPositions method we can capture the available movements of the turn in a lists depending of the RelativeMoveType State like the following code shows:

switch (moveType)
 {
 case RelativeMoveType.Any:
 validPositions.Add(translatedPosition);
 yield return translatedPosition;
 break;
 case RelativeMoveType.Empty:
 if (cell.IsEmpty())
 {
 validPositions.Add(translatedPosition);
 yield return translatedPosition;
 }
 break;
 case RelativeMoveType.Occupied:
 if (state.TestPosition(translatedPosition, occupiedPieceID, occupiedPlayerID) != null)
 {
 validPositions.Add(translatedPosition);
 yield return translatedPosition;
 }
 break;
 }

Then, after the main foreach we will add a for to change the color to the grids that are valid positions:

for (int i = 0; i < validPositions.Count; i++)
 {
 var currentPlayer = HighlightValidPositionsGame.manager.CurrentPlayer;
var newColor = new Color(Color.red.r, Color.red.g, Color.red.b, 0.3f);
if (currentPlayer.PlayerID == "blue")
 {
 newColor = new Color(Color.blue.r, Color.blue.g, Color.blue.b, 0.3f);
 }
if (i%2 != 0)
 {
 newColor = new Color(newColor.r, newColor.g, newColor.b, 0.5f);
 }
previousColors.Add(HighlightValidPositionsGame.manager.Grid[validPositions[i]].Color);
 HighlightValidPositionsGame.manager.Grid[validPositions[i]].Color = newColor;
 }

As you can see the colors are intercalated to show the cell colors. The two main list on this Scripts: validPositions and previousColors are static so they can be access by our three script.

You can see this implemented on the HighlightRelativeMoveRule Script on the example.

Make a costume Grid Game Rules script:

On this script you need to override OnMoveEnd to restart the color of cells when the movement end.

protected override void OnMoveEnd(GameMove<RectPoint, GridGamePieceSettings> EndedMove)
 {
 base.OnMoveEnd(EndedMove);
for (int i = 0; i < HighlightRelativeMoveRule.validPositions.Count; i++)
 {
 HighlightValidPositionsGame.manager.Grid[HighlightRelativeMoveRule.validPositions[i]].Color = HighlightRelativeMoveRule.previousColors[i];
 }
 }

You can see this implemented on the HighlightRelativeMoveRule Script on the example.

With these three scripts you can highlight the cells that are valid move positions for the players.

Scroll to Top