示例#1
0
    private void OnStateChange()
    {
        switch (currentState)
        {
        case State.Begin: {
            currentState = State.NextEnemy;
            break;
        }

        case State.NextEnemy: {
            Unit nextUnit = currentBattle.NextUnit();

            if (nextUnit == null)
            {
                currentBattle.MarkAllEnemiesAsDead();
                currentState = State.PlayerWon;
            }
            else
            {
                currentUnit  = new UnitInstance(nextUnit);
                currentState = State.PlayerSelectAction;
                UpdateHUD();
            }

            break;
        }

        case State.PlayerSelectAction: {
            List <string> options = new List <string>();
            options.Add("Fight");
            options.Add("Run");

            SetDialogueContent("What do you want to do?", options).AddListener(index => {
                    if (index == 1)
                    {
                        currentState = State.PlayerLost;
                    }
                    else
                    {
                        currentState = State.PlayerSelectsWeapon;
                    }
                });

            break;
        }

        case State.PlayerSelectsWeapon: {
            List <string> names             = new List <string>();
            List <Weapon> selectableWeapons = new List <Weapon>();

            foreach (KeyValuePair <Weapon, uint> entry in weaponUsages)
            {
                Weapon weapon = entry.Key;
                uint   usages = entry.Value;

                if (weapon.numberOfUsesPerFight == 0)
                {
                    names.Add(weapon.name);
                    selectableWeapons.Add(weapon);
                }
                else if (usages > 0)
                {
                    string text = string.Format("{0} ({1})", weapon.name, usages);
                    names.Add(text);
                    selectableWeapons.Add(weapon);
                }
            }

            SetDialogueContent("Select a weapon", names).AddListener(index => {
                    Weapon selectedWeapon = selectableWeapons[index];

                    if (PlayerAttacksWithWeapon(selectedWeapon))
                    {
                        string message = String.Format("'{0}' was killed", currentUnit.unit.name);
                        SetDialogueContent(message, null);
                        SetStateDelayed(State.NextEnemy, 1.2f);
                    }
                    else
                    {
                        currentState = State.EnemyAttacks;
                    }
                });

            break;
        }

        case State.EnemyAttacks: {
            SetDialogueContent("The enemy attacks...", null);
            StartCoroutine(OnEnemyAttack());
            break;
        }

        case State.PlayerWon: {
            List <string> options = new List <string>();
            options.Add("Continue");

            SetDialogueContent("Congratulations! you won the battle", options).AddListener(_ => {
                    ReturnToPreviousScene();
                });

            break;
        }

        case State.PlayerLost: {
            List <string> options = new List <string>();
            options.Add("Continue");

            SetDialogueContent("Damn, looks like you lost. Tough luck", options).AddListener(_ => {
                    ReturnToBedroom();
                });

            break;
        }
        }
    }