예제 #1
0
        private void InstantiateGameBoard()
        {
            m_attackingPlayer = Instantiate(m_attackingPlayerPrefab).GetComponent <Player>();
            m_attackingPlayer.gameObject.name             = m_attackingPlayerGameObjectName;
            m_attackingPlayer.gameObject.transform.parent = gameObject.transform;
            m_defendingPlayer = Instantiate(m_defendingPlayerPrefab).GetComponent <Player>();
            m_defendingPlayer.gameObject.name             = m_defendingPlayerGameObjectName;
            m_defendingPlayer.gameObject.transform.parent = gameObject.transform;

            if (m_rooksPrefab != null)
            {
                GameObject tRooksPrefab = Instantiate <GameObject>(m_rooksPrefab);
                tRooksPrefab.transform.parent = gameObject.transform;
                m_rookPieces = tRooksPrefab.GetComponentsInChildren <Rook>();

                for (int i = 0; i < m_rookPieces.Length; i++)
                {
                    if (m_rookPieces[i].m_rookType == RookType.KING)
                    {
                        m_king = m_rookPieces[i];
                        break;
                    }
                }
            }

            if (m_boardPrefab != null)
            {
                GameObject tBoardPrefab = Instantiate <GameObject>(m_boardPrefab);
                tBoardPrefab.transform.parent = gameObject.transform;
                m_tiles = tBoardPrefab.GetComponentsInChildren <Tile>();

                for (int i = 0; i < m_tiles.Length; i++)
                {
                    m_tiles[i].InitializeTile();
                }
            }

            DisableGameBoard();
        }
예제 #2
0
 public void UpdateMovedRook(Rook rook)
 {
     m_lastMoved = rook;
 }
예제 #3
0
        public bool CanMoveToPosition(Rook rook, Tile targetPos)
        {
            //evaluate if the rook can be moved to the position
            if ((rook.m_rookType != RookType.KING) && (targetPos.m_isCorner || targetPos.m_isThrone))
            {
                return(false);
            }
            else if ((rook.m_rookType == RookType.KING) && (targetPos.m_isCorner || targetPos.m_isThrone))
            {
                return(true);
            }

            int dx = Mathf.Abs(targetPos.m_X - rook.m_owner.m_X);
            int dy = Mathf.Abs(targetPos.m_Y - rook.m_owner.m_Y);

            if (dx > 0 && dy > 0)
            {
                return(false);
            }
            else if (dx == 0 && dy > 0)
            {
                //check if there is a rook in the path
                int  diff      = targetPos.m_Y - rook.m_owner.m_Y;
                Tile startTile = rook.m_owner;
                Tile tempTile  = null;
                for (int i = 0; i < dy; i++)
                {
                    if (diff > 0)
                    {
                        //move right
                        tempTile = GetTileRight(startTile);
                        if (tempTile != null && tempTile.m_ownedRook != null)
                        {
                            return(false);
                        }

                        startTile = tempTile;
                    }
                    else if (diff < 0)
                    {
                        //move left
                        tempTile = GetTileLeft(startTile);
                        if (tempTile != null && tempTile.m_ownedRook != null)
                        {
                            return(false);
                        }

                        startTile = tempTile;
                    }
                }

                return(true);
            }
            else if (dy == 0 && dx > 0)
            {
                //check if there is a rook in the path
                int  diff      = targetPos.m_X - rook.m_owner.m_X;
                Tile startTile = rook.m_owner;
                Tile tempTile  = null;
                for (int i = 0; i < dx; i++)
                {
                    if (diff > 0)
                    {
                        //move down
                        tempTile = GetTileDown(startTile);
                        if (tempTile != null && tempTile.m_ownedRook != null)
                        {
                            return(false);
                        }

                        startTile = tempTile;
                    }
                    else if (diff < 0)
                    {
                        //move up
                        tempTile = GetTileUp(startTile);
                        if (tempTile != null && tempTile.m_ownedRook != null)
                        {
                            return(false);
                        }

                        startTile = tempTile;
                    }
                }

                return(true);
            }

            return(false);
        }
예제 #4
0
 public void ResetTile()
 {
     m_ownedRook = null;
     InitializeTile();
 }
예제 #5
0
        private void Update()
        {
            // pause game
            if (Input.GetAxis("Cancel") > 0.0f)
            {
                gameManager.m_pauseGame = true;
            }

            // if this player can take actions on the game board
            if (m_canPlayerTakeAction)
            {
                // camera rotation
                if (m_cameraRotation != null)
                {
                    if (Input.GetAxis("Rotate") > 0.0f)
                    {
                        m_cameraRotation.RotateAntiClockwise();
                    }
                    else if (Input.GetAxis("Rotate") < 0.0f)
                    {
                        m_cameraRotation.RotateClockwise();
                    }
                    else
                    {
                        m_cameraRotation.ResetRotation();
                    }
                }

                // mouse clicked and doesn't have a selected rook
                if (Input.GetMouseButtonDown(0) && !m_hasRook)
                {
                    RaycastHit hit = new RaycastHit();
                    if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit))
                    {
                        // check if the hit object is a rook
                        Rook tempRook = hit.transform.gameObject.GetComponent <Rook>();
                        if (tempRook != null)
                        {
                            // only attacking player can select the attacker rooks and defending player can select only the defending rooks
                            if ((tempRook.m_rookType == RookType.ATTACKER && m_playerType == PlayerType.ATTACKER) || (tempRook.m_rookType == RookType.DEFENDER && m_playerType == PlayerType.DEFENDER) || (tempRook.m_rookType == RookType.KING && m_playerType == PlayerType.DEFENDER))
                            {
                                SelectRook(tempRook);
                            }
                        }
                    }
                }

                // mouse clicked and has a selected rook
                else if (Input.GetMouseButtonDown(0) && m_hasRook)
                {
                    RaycastHit hit = new RaycastHit();
                    if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit))
                    {
                        // if raycast hit is on tile, check if rook can move to this position
                        Tile tempTile = hit.transform.gameObject.GetComponent <Tile>();
                        if (tempTile != null && gameManager.CanMoveToPosition(m_selectedRook, tempTile))
                        {
                            // move rook to new position
                            UpdateRookOwner(tempTile);
                            UpdateRookPosition(new Vector3(tempTile.transform.position.x, gameManager.m_rookYOffset, tempTile.transform.position.z));
                            gameManager.UpdateMovedRook(m_selectedRook);

                            // update the game board
                            gameManager.UpdateGameBoard();

                            // deselect the moved rook
                            DeselectRook(m_selectedRook.m_prevPosition);
                        }

                        // if raycast hit is on a rook
                        Rook tempRook = hit.transform.gameObject.GetComponent <Rook>();
                        if (tempRook != null)
                        {
                            // deselect the currently selected rook
                            DeselectRook(m_selectedRook.m_prevPosition);
                        }
                    }

                    // no raycast hit, deselect the rook
                    else
                    {
                        DeselectRook(m_selectedRook.m_prevPosition);
                    }
                }
            }
        }
예제 #6
0
 private void SelectRook(Rook rook)
 {
     m_selectedRook = rook;
     m_selectedRook.m_isSelected = true;
     m_hasRook = true;
 }