Example #1
0
    void Update()
    {
        var mouseTile = GetTileUnderMouse();

        lastSelectedAlly = currentState.source ?? gridPlayer;
        UpdateSkillMenu();

        if (waiting)
        {
            return;
        }
        if (Input.GetKeyDown(KeyCode.Tab))
        {
            ToggleSkillMenu();
        }

        if (currentState is NoSelectionState)
        {
            if (mouseTile == null)
            {
                return;
            }
            else if (Input.GetMouseButtonDown(0) && mouseTile.occupier != null)
            {
                currentState = TransitionOnClick(currentState, mouseTile);
            }
            // this needs to be able to be triggered in any state
            else if (Input.GetKeyDown(KeyCode.G))
            {
                currentState = TransitionOnEndTurn();
            }
        }

        else if (currentState is AllySelectedState)
        {
            if (mouseTile == null)
            {
                return;
            }
            else if (Input.GetMouseButtonDown(0))
            {
                currentState = TransitionOnClick(currentState, mouseTile);
            }
            else if (InputUtils.GetKeyPressed(skillKeys) != null)
            {
                skillKeys.ForEach(keyPressed => {
                    if (Input.GetKeyDown(keyPressed))
                    {
                        currentState = TransitionOnSkillKeyPress(currentState, keyPressed);
                    }
                });
                StartCoroutine(WaitAMoment(waitTime, "Skill Activation"));
            }
            else if (Input.GetKeyDown(KeyCode.T))
            {
                currentState = TransitionOnTeleportKeyPress(currentState);
            }
        }

        else if (currentState is EnemySelectedState)
        {
            if (mouseTile == null)
            {
                return;
            }
            else if (Input.GetMouseButtonDown(0))
            {
                currentState = TransitionOnClick(currentState, mouseTile);
            }
        }

        else if (currentState is SelectSkillActivatedState)
        {
            if (Input.GetMouseButtonDown(0))
            {
                currentState = TransitionOnClick(currentState, mouseTile);
            }
            else if (InputUtils.GetKeyPressed(skillKeys) != null)
            {
                skillKeys.ForEach(keyPressed => {
                    if (Input.GetKeyDown(keyPressed))
                    {
                        currentState = TransitionOnSkillKeyPress(currentState, keyPressed);
                    }
                });
                StartCoroutine(WaitAMoment(waitTime, "Skill Activation"));
            }
        }

        else if (currentState is TeleportActivatedState)
        {
            if (Input.GetMouseButtonDown(0))
            {
                currentState = TransitionOnClick(currentState, mouseTile);
            }
            else if (Input.GetKeyDown(KeyCode.T))
            {
                currentState = TransitionOnTeleportKeyPress(currentState);
            }
        }

        else if (currentState is EnemyTurnState)
        {
            var stateData = (EnemyTurnState)currentState;
            if (stateData.aiSteps == null)
            {
                stateData.enemies.ForEach(enemy => {
                    var afraid = enemy.ChangeFear(CalculateTideOfBattle());
                    if (afraid)
                    {
                        Debug.Log($"{enemy.gameObject.name} was overwhelmed by the battle!");
                    }
                });
                stateData.aiSteps = stateMachine.DetermineAITurns();
            }
            else
            {
                currentState = stateMachine.ExecuteAITurn(currentState);
                if (stateData.aiSteps.Count == 0)
                {
                    // all AI entities have completed their actions
                    var newEnemies = stateData.enemies
                                     .Where(entity => entity.isElite && entity.currentHP > 0).ToList()
                                     .SelectMany(elite => elite.CallReinforcements(this));
                    stateData.enemies.AddRange(newEnemies);
                    nextFaction.entities = stateData.enemies;
                    currentState         = TransitionOnEndTurn();
                    StartCoroutine(WaitAMoment(aiStepTime, "Ending AI Turn"));
                }
                else
                {
                    var outputString = String.Format("Finishing actions on target {0}", stateData.aiSteps.First().Key);
                    StartCoroutine(WaitAMoment(aiStepTime, outputString));
                }
            }
        }
    }