Example #1
0
    public void GoForth()
    {
        //GameObject.FindGameObjectWithTag("Interface").transform.FindChild("Roll Button").gameObject.SetActive(false);
        SetStatusText("The pawn is moving");

        int    stepCount    = DiceRoller.RollTheDie();
        var    queue        = new Queue <Action>();
        Action pawnMovement = () =>
        {
            if (!(CurrentPawnMover.NextSpotAction is FinishSpotAction))
            {
                StartCoroutine(CurrentPawnMover.Move(spotConnection => spotConnection.NextSpot,
                                                     () =>
                {
                    //try
                    //{
                    //    Action postAction = queue.Dequeue();
                    //    postAction();
                    //}
                    //catch (InvalidOperationException)
                    //{
                    //    CurrentPawn.GetComponent<SpotAction>().PerformAction();
                    //}

                    if (queue.Count != 0)     //If there are movements left perform them
                    {
                        Action postAction = queue.Dequeue();
                        postAction();
                    }
                    else     //Perform actions defined by the landing spot
                    {
                        CurrentPawnMover.CurrentSpotAction.PerformAction(() =>
                        {
                            SwitchCharacter();
                            GameObject.FindGameObjectWithTag("Interface")
                            .transform.FindChild("Roll Button")
                            .gameObject.SetActive(true);
                            GameObject.FindGameObjectWithTag("CurrentCharacterText").GetComponent <Text>().text =
                                CurrentPawnMover.CharacterName;
                            SetStatusText("Roll the dice");
                        });
                    }
                }));
            }
            else
            {
                CurrentPawnMover.CurrentSpotAction.PerformAction(() =>
                {
                    SwitchCharacter();
                    GameObject.FindGameObjectWithTag("Interface")
                    .transform.FindChild("Roll Button")
                    .gameObject.SetActive(true);
                    GameObject.FindGameObjectWithTag("CurrentCharacterText").GetComponent <Text>().text =
                        CurrentPawnMover.CharacterName;
                    SetStatusText("Roll the dice");
                });
            }
        };

        for (int i = 0; i < stepCount; ++i)
        {
            queue.Enqueue(pawnMovement);
        }

        Action action = queue.Dequeue();

        action();
    }