using UnityEngine; namespace Oculus.Platform.Samples.VrBoardGame { public class GameBoard : MonoBehaviour { private struct PositionInfo { public GameObject piece; public int pieceOwner; public int powerPieceOwner; } public const int LENGTH_X = 3; public const int LENGTH_Y = 3; public const int MAX_PLAYERS = 2; [SerializeField] private Color[] m_playerColors = new Color[2]; [SerializeField] private Color m_proposedMoveColor = Color.white; private int[] m_scores = new int[2]; [SerializeField] private BoardPosition[] m_positions = new BoardPosition[9]; private readonly PositionInfo[,] m_pieces = new PositionInfo[3, 3]; public void Reset() { for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (m_pieces[i, j].piece != null) { Object.Destroy(m_pieces[i, j].piece); m_pieces[i, j].piece = null; m_pieces[i, j].pieceOwner = -1; m_pieces[i, j].powerPieceOwner = -1; } } } } public bool IsFull() { for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (m_pieces[i, j].piece == null) { return false; } } } return true; } public bool CanPlayerMoveToPostion(int x, int y) { return m_pieces[x, y].piece == null; } public bool CanPlayerPowerUpPosition(int x, int y) { return m_pieces[x, y].piece != null; } public void AddPiece(int player, GameObject prefab, int x, int y) { BoardPosition boardPosition = m_positions[x * 3 + y]; GamePiece gamePiece = Create(prefab, boardPosition.gameObject, boardPosition, Vector3.zero); gamePiece.GetComponent().material.color = m_playerColors[player]; m_pieces[x, y].piece = gamePiece.gameObject; m_pieces[x, y].pieceOwner = player; m_pieces[x, y].powerPieceOwner = -1; UpdateScores(); } public GamePiece AddProposedPiece(GameObject prefab, BoardPosition pos) { GamePiece gamePiece = Create(prefab, pos.gameObject, pos, Vector3.zero); gamePiece.GetComponent().material.color = m_proposedMoveColor; return gamePiece; } public void AddPowerPiece(int player, GameObject prefab, int x, int y) { GamePiece gamePiece = Create(prefab, m_pieces[x, y].piece, m_positions[x * 3 + y], 0.2f * Vector3.up); gamePiece.GetComponent().material.color = m_playerColors[player]; m_pieces[x, y].powerPieceOwner = player; UpdateScores(); } public GamePiece AddProposedPowerPiece(GameObject prefab, BoardPosition pos) { GamePiece gamePiece = Create(prefab, m_pieces[pos.x, pos.y].piece, pos, 0.2f * Vector3.up); gamePiece.GetComponent().material.color = m_proposedMoveColor; return gamePiece; } private GamePiece Create(GameObject prefab, GameObject parent, BoardPosition pos, Vector3 off) { GameObject gameObject = Object.Instantiate(prefab, parent.transform); gameObject.transform.position = parent.transform.position + off; gameObject.GetComponent().Position = pos; return gameObject.GetComponent(); } public int GetPlayerScore(int player) { return m_scores[player]; } private void UpdateScores() { for (int i = 0; i < 2; i++) { m_scores[i] = 0; } for (int j = 0; j < 3; j++) { for (int k = 0; k < 3; k++) { if (!(m_pieces[j, k].piece != null)) { continue; } m_scores[m_pieces[j, k].pieceOwner] += 10; if (m_pieces[j, k].powerPieceOwner < 0) { continue; } for (int l = j - 1; l <= j + 1; l++) { for (int m = k - 1; m <= k + 1; m++) { if (l >= 0 && m >= 0 && l < 3 && m < 3) { int num = ((m_pieces[j, k].pieceOwner != m_pieces[j, k].powerPieceOwner) ? (-10) : 10); m_scores[m_pieces[j, k].powerPieceOwner] += num; } } } } } } } }