Example #1
0
    private Directions DirectionFromTarget(SlotIndicator targetSlot)
    {
        if (currentPeg != null)
        {
            int currentIndex = currentPeg.currentIndex;
            int currentRow   = currentPeg.row;

            if (currentIndex > targetSlot.slotIndex)
            {
                if (currentIndex - ((2 * currentRow) - 3) == targetSlot.slotIndex)
                {
                    return(Directions.UP_RIGHT);
                }
                if (currentIndex - ((2 * currentRow) - 1) == targetSlot.slotIndex)
                {
                    return(Directions.UP_LEFT);
                }
                if (currentIndex - targetSlot.slotIndex == 2)
                {
                    return(Directions.LEFT);
                }
            }
            else
            {
                if (currentIndex + ((2 * currentRow) + 3) == targetSlot.slotIndex)
                {
                    return(Directions.DOWN_RIGHT);
                }
                if (currentIndex + ((2 * currentRow) + 1) == targetSlot.slotIndex)
                {
                    return(Directions.DOWN_LEFT);
                }
                if (currentIndex + 2 == targetSlot.slotIndex)
                {
                    return(Directions.RIGHT);
                }
            }

            Debug.Log("Cannot move to given position.");
            return(Directions.NONE);
        }
        else
        {
            Debug.Log("Peg is never assigned");
            return(Directions.NONE);
        }
    }
Example #2
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;
            }
        }
    }