| 1 | namespace Oculus.Platform.Samples.VrBoardGame |
| 2 | { |
| 3 | using UnityEngine; |
| 4 | using UnityEngine.UI; |
| 5 | |
| 6 | // This is the primary class that implements the game logic. |
| 7 | public class GameController : MonoBehaviour |
| 8 | { |
| 9 | // instance of the object interfacing with the matchmaking service |
| 10 | [SerializeField] private MatchmakingManager m_matchmaking = null; |
| 11 | |
| 12 | [SerializeField] private GameBoard m_board = null; |
| 13 | [SerializeField] private GamePiece m_pieceA = null; |
| 14 | [SerializeField] private GamePiece m_pieceB = null; |
| 15 | [SerializeField] private GamePiece m_powerPiece = null; |
| 16 | |
| 17 | // colors for the various states of the selectable games pieces |
| 18 | [SerializeField] private Color m_unusableColor = Color.white; |
| 19 | [SerializeField] private Color m_unselectedColor = Color.white; |
| 20 | [SerializeField] private Color m_selectedColor = Color.white; |
| 21 | [SerializeField] private Color m_highlightedColor = Color.white; |
| 22 | |
| 23 | [SerializeField] private Text m_ballCountText = null; |
| 24 | [SerializeField] private Text m_player0Text = null; |
| 25 | [SerializeField] private Text m_player1Text = null; |
| 26 | |
| 27 | private enum GameState { |
| 28 | None, |
| 29 | PracticingMyTurn, PracticingAiTurn, |
| 30 | OnlineMatchMyTurn, OnlineMatchRemoteTurn |
| 31 | } |
| 32 | |
| 33 | private GameState m_state; |
| 34 | |
| 35 | // the game piece the player is currently looking at |
| 36 | private GamePiece m_interestedPiece; |
| 37 | |
| 38 | // the piece the player selected with the Fire button |
| 39 | private GamePiece m_selectedPiece; |
| 40 | |
| 41 | // the piece that would be placed if the player pressed the Fire button |
| 42 | private GamePiece m_proposedPiece; |
| 43 | |
| 44 | // how many IAP power-balls the user has |
| 45 | private uint m_powerBallcount; |
| 46 | |
| 47 | // the name of the current opponent |
| 48 | private string m_opponentName; |
| 49 | |
| 50 | void Start() |
| 51 | { |
| 52 | TransitionToState(GameState.None); |
| 53 | UpdateScores(); |
| 54 | } |
| 55 | |
| 56 | void Update() |
| 57 | { |
| 58 | PerFrameStateUpdate(); |
| 59 | } |
| 60 | |
| 61 | #region Game State |
| 62 | |
| 63 | private void TransitionToState(GameState state) |
| 64 | { |
| 65 | m_state = state; |
| 66 | |
| 67 | UpdateGamePieceColors(); |
| 68 | } |
| 69 | |
| 70 | private void TransitionToNextState() |
| 71 | { |
| 72 | if (!m_board.IsFull()) |
| 73 | { |
| 74 | switch (m_state) |
| 75 | { |
| 76 | case GameState.PracticingAiTurn: |
| 77 | TransitionToState(GameState.PracticingMyTurn); |
| 78 | break; |
| 79 | case GameState.PracticingMyTurn: |
| 80 | TransitionToState(GameState.PracticingAiTurn); |
| 81 | break; |
| 82 | case GameState.OnlineMatchRemoteTurn: |
| 83 | TransitionToState(GameState.OnlineMatchMyTurn); |
| 84 | break; |
| 85 | case GameState.OnlineMatchMyTurn: |
| 86 | TransitionToState(GameState.OnlineMatchRemoteTurn); |
| 87 | break; |
| 88 | } |
| 89 | } |
| 90 | else |
| 91 | { |
| 92 | switch (m_state) |
| 93 | { |
| 94 | case GameState.OnlineMatchRemoteTurn: |
| 95 | case GameState.OnlineMatchMyTurn: |
| 96 | m_matchmaking.EndMatch(m_board.GetPlayerScore(0), m_board.GetPlayerScore(1)); |
| 97 | break; |
| 98 | } |
| 99 | TransitionToState(GameState.None); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | private void PerFrameStateUpdate() |
| 104 | { |
| 105 | switch (m_state) |
| 106 | { |
| 107 | case GameState.PracticingAiTurn: |
| 108 | // don't move immediately to give the AI time to 'think' |
| 109 | if (Random.Range(1, 100) < 3) |
| 110 | { |
| 111 | MakeAIMove(1); |
| 112 | } |
| 113 | break; |
| 114 | |
| 115 | case GameState.PracticingMyTurn: |
| 116 | case GameState.OnlineMatchMyTurn: |
| 117 | if (Input.GetButton("Fire1")) |
| 118 | { |
| 119 | TrySelectPiece(); |
| 120 | TryPlacePiece(); |
| 121 | } |
| 122 | break; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | #endregion |
| 127 | |
| 128 | #region Practicing with an AI Player |
| 129 | |
| 130 | public void PracticeButtonPressed() |
| 131 | { |
| 132 | m_opponentName = "* AI *"; |
| 133 | |
| 134 | switch (m_state) |
| 135 | { |
| 136 | case GameState.OnlineMatchMyTurn: |
| 137 | case GameState.OnlineMatchRemoteTurn: |
| 138 | m_matchmaking.EndMatch(m_board.GetPlayerScore(0), m_board.GetPlayerScore(1)); |
| 139 | break; |
| 140 | } |
| 141 | m_board.Reset(); |
| 142 | |
| 143 | // randomly decised whether the player or AI goes first |
| 144 | if (Random.Range(0, 2) == 1) |
| 145 | { |
| 146 | TransitionToState(GameState.PracticingMyTurn); |
| 147 | } |
| 148 | else |
| 149 | { |
| 150 | TransitionToState(GameState.PracticingAiTurn); |
| 151 | } |
| 152 | |
| 153 | UpdateScores(); |
| 154 | } |
| 155 | |
| 156 | private void MakeAIMove(int player) |
| 157 | { |
| 158 | bool moved = false; |
| 159 | |
| 160 | // pick a random search start position |
| 161 | int rx = Random.Range(0, GameBoard.LENGTH_X - 1); |
| 162 | int ry = Random.Range(0, GameBoard.LENGTH_Y - 1); |
| 163 | |
| 164 | // from (rx,ry) search of an available move |
| 165 | for (int i = 0; i < GameBoard.LENGTH_X && !moved; i++) |
| 166 | { |
| 167 | for (int j = 0; j < GameBoard.LENGTH_Y && !moved; j++) |
| 168 | { |
| 169 | int x = (rx + i) % GameBoard.LENGTH_X; |
| 170 | int y = (ry + j) % GameBoard.LENGTH_Y; |
| 171 | |
| 172 | // first try to place a piece on the current position |
| 173 | if (m_board.CanPlayerMoveToPostion(x, y)) |
| 174 | { |
| 175 | GamePiece p = Random.Range(0, 2) == 0 ? m_pieceA : m_pieceB; |
| 176 | m_board.AddPiece(player, p.Prefab, x, y); |
| 177 | moved = true; |
| 178 | } |
| 179 | // a random percentage of the time, try to powerup this position |
| 180 | else if (m_board.CanPlayerPowerUpPosition(x, y) && Random.Range(0, 8) < 2) |
| 181 | { |
| 182 | m_board.AddPowerPiece(player, m_powerPiece.Prefab, x, y); |
| 183 | moved = true; |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | if (moved) |
| 189 | { |
| 190 | UpdateScores(); |
| 191 | TransitionToNextState(); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | #endregion |
| 196 | |
| 197 | #region Playing Online Match |
| 198 | |
| 199 | // called from the MatchmakingManager was a successly online match is made |
| 200 | public void StartOnlineMatch (string opponentName, bool localUserGoesFirst) |
| 201 | { |
| 202 | m_board.Reset(); |
| 203 | m_opponentName = opponentName; |
| 204 | |
| 205 | if (localUserGoesFirst) |
| 206 | { |
| 207 | TransitionToState(GameState.OnlineMatchMyTurn); |
| 208 | } |
| 209 | else |
| 210 | { |
| 211 | TransitionToState(GameState.OnlineMatchRemoteTurn); |
| 212 | } |
| 213 | |
| 214 | UpdateScores(); |
| 215 | } |
| 216 | |
| 217 | // called from the Matchmaking Manager when the remote users their next move |
| 218 | public void MakeRemoteMove(GamePiece.Piece piece, int x, int y) |
| 219 | { |
| 220 | GameObject prefab = m_pieceA.PrefabFor(piece); |
| 221 | |
| 222 | if (piece == GamePiece.Piece.PowerBall) |
| 223 | { |
| 224 | m_board.AddPowerPiece(1, prefab, x, y); |
| 225 | } |
| 226 | else |
| 227 | { |
| 228 | m_board.AddPiece(1, prefab, x, y); |
| 229 | } |
| 230 | |
| 231 | UpdateScores(); |
| 232 | } |
| 233 | |
| 234 | // called from the MatchmakingManager when the local user becomes the room |
| 235 | // owner and thus it's safe for the local user to make their move |
| 236 | public void MarkRemoteTurnComplete() |
| 237 | { |
| 238 | if (m_state == GameState.OnlineMatchRemoteTurn) |
| 239 | { |
| 240 | TransitionToNextState(); |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | // the match ended from a player leaving before the board was complete |
| 245 | public void RemoteMatchEnded() |
| 246 | { |
| 247 | m_matchmaking.EndMatch(m_board.GetPlayerScore(0), m_board.GetPlayerScore(1)); |
| 248 | } |
| 249 | |
| 250 | #endregion |
| 251 | |
| 252 | #region Selecting and Placing a Game Place |
| 253 | |
| 254 | public void StartedLookingAtPiece(GamePiece piece) |
| 255 | { |
| 256 | m_interestedPiece = piece; |
| 257 | UpdateGamePieceColors(); |
| 258 | } |
| 259 | |
| 260 | public void StoppedLookingAtPiece() |
| 261 | { |
| 262 | m_interestedPiece = null; |
| 263 | UpdateGamePieceColors(); |
| 264 | } |
| 265 | |
| 266 | // This method is used to display an example piece where the player is looking |
| 267 | // so they know what to expect when they press the Fire button. |
| 268 | public void StartedLookingAtPosition(BoardPosition position) |
| 269 | { |
| 270 | if (m_state != GameState.OnlineMatchMyTurn && m_state != GameState.PracticingMyTurn) |
| 271 | return; |
| 272 | |
| 273 | GamePiece newPiece = null; |
| 274 | |
| 275 | if ((m_selectedPiece == m_pieceA || m_selectedPiece == m_pieceB) && |
| 276 | m_board.CanPlayerMoveToPostion(position.x, position.y)) |
| 277 | { |
| 278 | newPiece = m_board.AddProposedPiece(m_selectedPiece.Prefab, position); |
| 279 | } |
| 280 | else if (m_selectedPiece == m_powerPiece && |
| 281 | m_board.CanPlayerPowerUpPosition(position.x, position.y)) |
| 282 | { |
| 283 | newPiece = m_board.AddProposedPowerPiece(m_selectedPiece.Prefab, position); |
| 284 | } |
| 285 | |
| 286 | if (newPiece != null) |
| 287 | { |
| 288 | if (m_proposedPiece != null) |
| 289 | { |
| 290 | Destroy(m_proposedPiece.gameObject); |
| 291 | } |
| 292 | m_proposedPiece = newPiece; |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | public void ClearProposedMove() |
| 297 | { |
| 298 | if (m_proposedPiece != null) |
| 299 | { |
| 300 | Destroy(m_proposedPiece.gameObject); |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | public void TrySelectPiece() |
| 305 | { |
| 306 | if (m_interestedPiece == m_pieceA || m_interestedPiece == m_pieceB) |
| 307 | { |
| 308 | m_selectedPiece = m_interestedPiece; |
| 309 | } |
| 310 | else if (m_interestedPiece == m_powerPiece && |
| 311 | (m_powerBallcount > 0 || m_state == GameState.PracticingMyTurn)) |
| 312 | { |
| 313 | m_selectedPiece = m_interestedPiece; |
| 314 | } |
| 315 | UpdateGamePieceColors(); |
| 316 | } |
| 317 | |
| 318 | public void TryPlacePiece() |
| 319 | { |
| 320 | if (m_proposedPiece == null) |
| 321 | return; |
| 322 | |
| 323 | var position = m_proposedPiece.Position; |
| 324 | switch(m_proposedPiece.Type) |
| 325 | { |
| 326 | case GamePiece.Piece.A: |
| 327 | case GamePiece.Piece.B: |
| 328 | m_board.AddPiece(0, m_proposedPiece.Prefab, position.x, position.y); |
| 329 | break; |
| 330 | case GamePiece.Piece.PowerBall: |
| 331 | m_board.AddPowerPiece(0, m_proposedPiece.Prefab, position.x, position.y); |
| 332 | break; |
| 333 | } |
| 334 | Destroy(m_proposedPiece.gameObject); |
| 335 | |
| 336 | if (m_state == GameState.OnlineMatchMyTurn) |
| 337 | { |
| 338 | m_matchmaking.SendLocalMove(m_proposedPiece.Type, position.x, position.y); |
| 339 | } |
| 340 | |
| 341 | UpdateScores(); |
| 342 | TransitionToNextState(); |
| 343 | } |
| 344 | |
| 345 | #endregion |
| 346 | |
| 347 | #region UI |
| 348 | |
| 349 | public void QuitButtonPressed() |
| 350 | { |
| 351 | UnityEngine.Application.Quit(); |
| 352 | } |
| 353 | |
| 354 | public void AddPowerballs(uint count) |
| 355 | { |
| 356 | m_powerBallcount += count; |
| 357 | m_ballCountText.text = "x" + m_powerBallcount.ToString(); |
| 358 | } |
| 359 | |
| 360 | private void UpdateScores() |
| 361 | { |
| 362 | m_player0Text.text = string.Format("{0}\n\n{1}", |
| 363 | PlatformManager.MyOculusID, m_board.GetPlayerScore(0)); |
| 364 | |
| 365 | m_player1Text.text = string.Format("{0}\n\n{1}", |
| 366 | m_opponentName, m_board.GetPlayerScore(1)); |
| 367 | } |
| 368 | |
| 369 | private void UpdateGamePieceColors() |
| 370 | { |
| 371 | switch (m_state) |
| 372 | { |
| 373 | case GameState.None: |
| 374 | case GameState.PracticingAiTurn: |
| 375 | case GameState.OnlineMatchRemoteTurn: |
| 376 | m_pieceA.GetComponent<Renderer>().material.color = m_unusableColor; |
| 377 | m_pieceB.GetComponent<Renderer>().material.color = m_unusableColor; |
| 378 | m_powerPiece.GetComponent<Renderer>().material.color = m_unusableColor; |
| 379 | if (m_proposedPiece != null) |
| 380 | { |
| 381 | Destroy(m_proposedPiece.gameObject); |
| 382 | } |
| 383 | break; |
| 384 | |
| 385 | case GameState.PracticingMyTurn: |
| 386 | case GameState.OnlineMatchMyTurn: |
| 387 | m_pieceA.GetComponent<Renderer>().material.color = m_unselectedColor; |
| 388 | m_pieceB.GetComponent<Renderer>().material.color = m_unselectedColor; |
| 389 | m_powerPiece.GetComponent<Renderer>().material.color = m_unselectedColor; |
| 390 | if (m_interestedPiece == m_pieceA || m_interestedPiece == m_pieceB || |
| 391 | m_interestedPiece == m_powerPiece) |
| 392 | { |
| 393 | m_interestedPiece.GetComponent<Renderer>().material.color = m_highlightedColor; |
| 394 | } |
| 395 | if (m_selectedPiece != null) |
| 396 | { |
| 397 | m_selectedPiece.GetComponent<Renderer>().material.color = m_selectedColor; |
| 398 | } |
| 399 | break; |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | #endregion |
| 404 | } |
| 405 | } |