using UnityEngine; using UnityEngine.UI; namespace Oculus.Platform.Samples.VrBoardGame { public class GameController : MonoBehaviour { private enum GameState { None = 0, PracticingMyTurn = 1, PracticingAiTurn = 2, OnlineMatchMyTurn = 3, OnlineMatchRemoteTurn = 4 } [SerializeField] private MatchmakingManager m_matchmaking; [SerializeField] private GameBoard m_board; [SerializeField] private GamePiece m_pieceA; [SerializeField] private GamePiece m_pieceB; [SerializeField] private GamePiece m_powerPiece; [SerializeField] private Color m_unusableColor = Color.white; [SerializeField] private Color m_unselectedColor = Color.white; [SerializeField] private Color m_selectedColor = Color.white; [SerializeField] private Color m_highlightedColor = Color.white; [SerializeField] private Text m_ballCountText; [SerializeField] private Text m_player0Text; [SerializeField] private Text m_player1Text; private GameState m_state; private GamePiece m_interestedPiece; private GamePiece m_selectedPiece; private GamePiece m_proposedPiece; private uint m_powerBallcount; private string m_opponentName; private void Start() { TransitionToState(GameState.None); UpdateScores(); } private void Update() { PerFrameStateUpdate(); } private void TransitionToState(GameState state) { m_state = state; UpdateGamePieceColors(); } private void TransitionToNextState() { if (!m_board.IsFull()) { switch (m_state) { case GameState.PracticingAiTurn: TransitionToState(GameState.PracticingMyTurn); break; case GameState.PracticingMyTurn: TransitionToState(GameState.PracticingAiTurn); break; case GameState.OnlineMatchRemoteTurn: TransitionToState(GameState.OnlineMatchMyTurn); break; case GameState.OnlineMatchMyTurn: TransitionToState(GameState.OnlineMatchRemoteTurn); break; } } else { GameState state = m_state; if (state == GameState.OnlineMatchRemoteTurn || state == GameState.OnlineMatchMyTurn) { m_matchmaking.EndMatch(m_board.GetPlayerScore(0), m_board.GetPlayerScore(1)); } TransitionToState(GameState.None); } } private void PerFrameStateUpdate() { switch (m_state) { case GameState.PracticingAiTurn: if (Random.Range(1, 100) < 3) { MakeAIMove(1); } break; case GameState.PracticingMyTurn: case GameState.OnlineMatchMyTurn: if (Input.GetButton("Fire1")) { TrySelectPiece(); TryPlacePiece(); } break; } } public void PracticeButtonPressed() { m_opponentName = "* AI *"; GameState state = m_state; if (state == GameState.OnlineMatchMyTurn || state == GameState.OnlineMatchRemoteTurn) { m_matchmaking.EndMatch(m_board.GetPlayerScore(0), m_board.GetPlayerScore(1)); } m_board.Reset(); if (Random.Range(0, 2) == 1) { TransitionToState(GameState.PracticingMyTurn); } else { TransitionToState(GameState.PracticingAiTurn); } UpdateScores(); } private void MakeAIMove(int player) { bool flag = false; int num = Random.Range(0, 2); int num2 = Random.Range(0, 2); for (int i = 0; i < 3; i++) { if (flag) { break; } for (int j = 0; j < 3; j++) { if (flag) { break; } int x = (num + i) % 3; int y = (num2 + j) % 3; if (m_board.CanPlayerMoveToPostion(x, y)) { GamePiece gamePiece = ((Random.Range(0, 2) != 0) ? m_pieceB : m_pieceA); m_board.AddPiece(player, gamePiece.Prefab, x, y); flag = true; } else if (m_board.CanPlayerPowerUpPosition(x, y) && Random.Range(0, 8) < 2) { m_board.AddPowerPiece(player, m_powerPiece.Prefab, x, y); flag = true; } } } if (flag) { UpdateScores(); TransitionToNextState(); } } public void StartOnlineMatch(string opponentName, bool localUserGoesFirst) { m_board.Reset(); m_opponentName = opponentName; if (localUserGoesFirst) { TransitionToState(GameState.OnlineMatchMyTurn); } else { TransitionToState(GameState.OnlineMatchRemoteTurn); } UpdateScores(); } public void MakeRemoteMove(GamePiece.Piece piece, int x, int y) { GameObject prefab = m_pieceA.PrefabFor(piece); if (piece == GamePiece.Piece.PowerBall) { m_board.AddPowerPiece(1, prefab, x, y); } else { m_board.AddPiece(1, prefab, x, y); } UpdateScores(); } public void MarkRemoteTurnComplete() { if (m_state == GameState.OnlineMatchRemoteTurn) { TransitionToNextState(); } } public void RemoteMatchEnded() { m_matchmaking.EndMatch(m_board.GetPlayerScore(0), m_board.GetPlayerScore(1)); } public void StartedLookingAtPiece(GamePiece piece) { m_interestedPiece = piece; UpdateGamePieceColors(); } public void StoppedLookingAtPiece() { m_interestedPiece = null; UpdateGamePieceColors(); } public void StartedLookingAtPosition(BoardPosition position) { if (m_state != GameState.OnlineMatchMyTurn && m_state != GameState.PracticingMyTurn) { return; } GamePiece gamePiece = null; if ((m_selectedPiece == m_pieceA || m_selectedPiece == m_pieceB) && m_board.CanPlayerMoveToPostion(position.x, position.y)) { gamePiece = m_board.AddProposedPiece(m_selectedPiece.Prefab, position); } else if (m_selectedPiece == m_powerPiece && m_board.CanPlayerPowerUpPosition(position.x, position.y)) { gamePiece = m_board.AddProposedPowerPiece(m_selectedPiece.Prefab, position); } if (gamePiece != null) { if (m_proposedPiece != null) { Object.Destroy(m_proposedPiece.gameObject); } m_proposedPiece = gamePiece; } } public void ClearProposedMove() { if (m_proposedPiece != null) { Object.Destroy(m_proposedPiece.gameObject); } } public void TrySelectPiece() { if (m_interestedPiece == m_pieceA || m_interestedPiece == m_pieceB) { m_selectedPiece = m_interestedPiece; } else if (m_interestedPiece == m_powerPiece && (m_powerBallcount != 0 || m_state == GameState.PracticingMyTurn)) { m_selectedPiece = m_interestedPiece; } UpdateGamePieceColors(); } public void TryPlacePiece() { if (!(m_proposedPiece == null)) { BoardPosition position = m_proposedPiece.Position; switch (m_proposedPiece.Type) { case GamePiece.Piece.A: case GamePiece.Piece.B: m_board.AddPiece(0, m_proposedPiece.Prefab, position.x, position.y); break; case GamePiece.Piece.PowerBall: m_board.AddPowerPiece(0, m_proposedPiece.Prefab, position.x, position.y); break; } Object.Destroy(m_proposedPiece.gameObject); if (m_state == GameState.OnlineMatchMyTurn) { m_matchmaking.SendLocalMove(m_proposedPiece.Type, position.x, position.y); } UpdateScores(); TransitionToNextState(); } } public void QuitButtonPressed() { UnityEngine.Application.Quit(); } public void AddPowerballs(uint count) { m_powerBallcount += count; m_ballCountText.text = "x" + m_powerBallcount; } private void UpdateScores() { m_player0Text.text = string.Format("{0}\n\n{1}", PlatformManager.MyOculusID, m_board.GetPlayerScore(0)); m_player1Text.text = string.Format("{0}\n\n{1}", m_opponentName, m_board.GetPlayerScore(1)); } private void UpdateGamePieceColors() { switch (m_state) { case GameState.None: case GameState.PracticingAiTurn: case GameState.OnlineMatchRemoteTurn: m_pieceA.GetComponent().material.color = m_unusableColor; m_pieceB.GetComponent().material.color = m_unusableColor; m_powerPiece.GetComponent().material.color = m_unusableColor; if (m_proposedPiece != null) { Object.Destroy(m_proposedPiece.gameObject); } break; case GameState.PracticingMyTurn: case GameState.OnlineMatchMyTurn: m_pieceA.GetComponent().material.color = m_unselectedColor; m_pieceB.GetComponent().material.color = m_unselectedColor; m_powerPiece.GetComponent().material.color = m_unselectedColor; if (m_interestedPiece == m_pieceA || m_interestedPiece == m_pieceB || m_interestedPiece == m_powerPiece) { m_interestedPiece.GetComponent().material.color = m_highlightedColor; } if (m_selectedPiece != null) { m_selectedPiece.GetComponent().material.color = m_selectedColor; } break; } } } }