| 1 | namespace Oculus.Platform.Samples.VrBoardGame |
| 2 | { |
| 3 | using System; |
| 4 | using UnityEngine; |
| 5 | |
| 6 | // |
| 7 | // This script describes the game board along with the game pieces that |
| 8 | // are in play. The rules for the game board are: |
| 9 | // 1) Player can place a normal GamePiece on any empty BoardPosition |
| 10 | // 2) Player can place a power GamePiece on top of a normal piece |
| 11 | // 3) The board is full when all positions have a normal piece |
| 12 | // Player score is calculated as: |
| 13 | // 1) +10 points for each normal piece on the board |
| 14 | // 2) +10 points for each normal piece with 1 square of one of their power pieces |
| 15 | // 3) -10 points for each opponent normal piece within 1 square of their power pieces |
| 16 | // |
| 17 | public class GameBoard : MonoBehaviour |
| 18 | { |
| 19 | public const int LENGTH_X = 3; |
| 20 | public const int LENGTH_Y = 3; |
| 21 | public const int MAX_PLAYERS = 2; |
| 22 | |
| 23 | // the placed-piece color for each player |
| 24 | [SerializeField] private Color[] m_playerColors = new Color[MAX_PLAYERS]; |
| 25 | |
| 26 | // color for pice the player is considering moving to |
| 27 | [SerializeField] private Color m_proposedMoveColor = Color.white; |
| 28 | |
| 29 | // the player scores that are recalcuated after a pice is placed |
| 30 | private int[] m_scores = new int[MAX_PLAYERS]; |
| 31 | |
| 32 | // GameObjects that define each of the allowed piece positions |
| 33 | [SerializeField] private BoardPosition[] m_positions = new BoardPosition[9]; |
| 34 | |
| 35 | private struct PositionInfo |
| 36 | { |
| 37 | public GameObject piece; |
| 38 | public int pieceOwner; |
| 39 | public int powerPieceOwner; |
| 40 | } |
| 41 | |
| 42 | // pieces in play for the current game |
| 43 | private readonly PositionInfo[,] m_pieces = new PositionInfo[LENGTH_X, LENGTH_Y]; |
| 44 | |
| 45 | // removes all game pieces from the board |
| 46 | public void Reset() |
| 47 | { |
| 48 | for (int x = 0; x < LENGTH_X; x++) |
| 49 | { |
| 50 | for (int y = 0; y < LENGTH_Y; y++) |
| 51 | { |
| 52 | if (m_pieces[x,y].piece != null) |
| 53 | { |
| 54 | Destroy(m_pieces[x,y].piece); |
| 55 | m_pieces[x,y].piece = null; |
| 56 | m_pieces[x,y].pieceOwner = -1; |
| 57 | m_pieces[x,y].powerPieceOwner = -1; |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | #region Board status |
| 64 | |
| 65 | // returns true if all the board positions have a piece in them |
| 66 | public bool IsFull() |
| 67 | { |
| 68 | for (int x = 0; x < LENGTH_X; x++) |
| 69 | { |
| 70 | for (int y = 0; y < LENGTH_Y; y++) |
| 71 | { |
| 72 | if (m_pieces[x,y].piece == null) |
| 73 | { |
| 74 | return false; |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | return true; |
| 79 | } |
| 80 | |
| 81 | public bool CanPlayerMoveToPostion(int x, int y) |
| 82 | { |
| 83 | return m_pieces[x,y].piece == null; |
| 84 | } |
| 85 | |
| 86 | public bool CanPlayerPowerUpPosition(int x, int y) |
| 87 | { |
| 88 | return m_pieces[x,y].piece != null; |
| 89 | } |
| 90 | |
| 91 | #endregion |
| 92 | |
| 93 | #region creating game pieces |
| 94 | |
| 95 | public void AddPiece(int player, GameObject prefab, int x, int y) |
| 96 | { |
| 97 | var pos = m_positions[x * LENGTH_Y + y]; |
| 98 | var piece = Create(prefab, pos.gameObject, pos, Vector3.zero); |
| 99 | piece.GetComponent<Renderer>().material.color = m_playerColors[player]; |
| 100 | m_pieces[x,y].piece = piece.gameObject; |
| 101 | m_pieces[x,y].pieceOwner = player; |
| 102 | m_pieces[x,y].powerPieceOwner = -1; |
| 103 | |
| 104 | UpdateScores(); |
| 105 | } |
| 106 | |
| 107 | public GamePiece AddProposedPiece(GameObject prefab, BoardPosition pos) |
| 108 | { |
| 109 | var piece = Create(prefab, pos.gameObject, pos, Vector3.zero); |
| 110 | piece.GetComponent<Renderer>().material.color = m_proposedMoveColor; |
| 111 | return piece; |
| 112 | } |
| 113 | |
| 114 | public void AddPowerPiece(int player, GameObject prefab, int x, int y) |
| 115 | { |
| 116 | var piece = Create(prefab, m_pieces[x,y].piece, m_positions[x*LENGTH_Y+y], .2f*Vector3.up); |
| 117 | piece.GetComponent<Renderer>().material.color = m_playerColors[player]; |
| 118 | m_pieces[x,y].powerPieceOwner = player; |
| 119 | |
| 120 | UpdateScores(); |
| 121 | } |
| 122 | |
| 123 | public GamePiece AddProposedPowerPiece(GameObject prefab, BoardPosition pos) |
| 124 | { |
| 125 | var piece = Create(prefab, m_pieces[pos.x, pos.y].piece, pos, .2f*Vector3.up); |
| 126 | piece.GetComponent<Renderer>().material.color = m_proposedMoveColor; |
| 127 | return piece; |
| 128 | } |
| 129 | |
| 130 | private GamePiece Create(GameObject prefab, GameObject parent, BoardPosition pos, Vector3 off) |
| 131 | { |
| 132 | var go = Instantiate(prefab, parent.transform) as GameObject; |
| 133 | go.transform.position = parent.transform.position + off; |
| 134 | go.GetComponent<GamePiece>().Position = pos; |
| 135 | return go.GetComponent<GamePiece>(); |
| 136 | } |
| 137 | |
| 138 | #endregion |
| 139 | |
| 140 | #region scores |
| 141 | |
| 142 | public int GetPlayerScore(int player) |
| 143 | { |
| 144 | return m_scores[player]; |
| 145 | } |
| 146 | |
| 147 | private void UpdateScores() |
| 148 | { |
| 149 | for (int i = 0; i < MAX_PLAYERS; i++) |
| 150 | { |
| 151 | m_scores[i] = 0; |
| 152 | } |
| 153 | |
| 154 | for (int x = 0; x < LENGTH_X; x++) |
| 155 | { |
| 156 | for (int y = 0; y < LENGTH_Y; y++) |
| 157 | { |
| 158 | if (m_pieces[x,y].piece != null) |
| 159 | { |
| 160 | // for each piece on the board, the player gets 10 points |
| 161 | m_scores[m_pieces[x,y].pieceOwner] += 10; |
| 162 | |
| 163 | // for each power piece, the player gains or loses 10 points |
| 164 | // based on the ownership of nearby pieces |
| 165 | if (m_pieces[x,y].powerPieceOwner >= 0) |
| 166 | { |
| 167 | for (int px = x-1; px <= x+1; px++) |
| 168 | { |
| 169 | for (int py = y-1; py <= y+1; py++) |
| 170 | { |
| 171 | if (px >= 0 && py >= 0 && px < LENGTH_X && py < LENGTH_Y) |
| 172 | { |
| 173 | var powerup = |
| 174 | m_pieces[x,y].pieceOwner == m_pieces[x,y].powerPieceOwner ? |
| 175 | +10 : -10; |
| 176 | m_scores[m_pieces[x, y].powerPieceOwner] += powerup; |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | #endregion |
| 187 | } |
| 188 | } |