示例#1
0
    void Update()
    {
        if (GameStateManager._gameState != GameStateManager.GameState.RUNNING)
        {
            return;
        }

        if (Input.GetMouseButtonDown(0))
        {
            Ray        ray = mainCam.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            Physics.Raycast(ray, out hit);

            if (hit.transform != null && hit.transform.tag == "Peg")
            {
                if (currentPeg != null)
                {
                    currentPeg.Deselect();
                }
                currentPeg = hit.transform.GetComponent <Peg>();
                currentPeg.Select();
            }
            else
            {
                if (hit.transform != null && hit.transform.tag == "Slot")
                {
                    SlotIndicator tempIndicator = hit.transform.GetComponent <SlotIndicator>();
                    Slot          s             = boardSlots[tempIndicator.slotIndex];
                    if (!s.isFilled)
                    {
                        MovePeg(tempIndicator.slotIndex, DirectionFromTarget(tempIndicator));
                        if (!MovesExist())
                        {
                            int remainingPegs = 0;
                            for (int i = 0; i < pegLayout.Length; ++i)
                            {
                                if (pegLayout[i] != null)
                                {
                                    remainingPegs++;
                                }
                            }

                            if (remainingPegs == 1)
                            {
                                GameStateManager._gameState = GameStateManager.GameState.WIN;
                            }
                            else
                            {
                                GameStateManager._gameState = GameStateManager.GameState.LOSE;
                            }
                            Debug.Log("Remaining pegs " + remainingPegs);
                        }
                    }
                }
                if (currentPeg != null)
                {
                    currentPeg.Deselect();
                }
                currentPeg = null;
            }
        }
    }