Beispiel #1
0
        private void attackMonsters(IScene scene)
        {
            var didHitMonster = false;
            var monsters      = scene.GetItems("Monsters");

            foreach (var item in monsters)
            {
                var monster = (IMonster)item;

                var damage = monster.Defend();

                if (damage > 0)
                {
                    didHitMonster = true;

                    if (monster.Health == 0)
                    {
                        scene.Feedback = $"You killed a {monster.Name}!";

                        // if the monster has any treasure move it to the scene items
                        var treasures = monster.GetItems("Treasures");

                        if (treasures.Count > 0)
                        {
                            scene.Feedback += $"\nThe {monster.Name} dropped some treasure!";
                        }

                        foreach (var treasure in treasures)
                        {
                            scene.AddItem("Treasures", treasure);
                        }

                        // remove dead monsters from the scene
                        scene.RemoveItem("Monsters", item);

                        if (scene.GetItems("Monsters").Count == 0)
                        {
                            scene.Feedback += "\nThere are no monsters left.";
                        }
                    }
                    else
                    {
                        scene.Feedback = $"You hit a {monster.Name} and inflicted {damage} points of damage.";
                    }

                    break;
                }
            }

            if (!didHitMonster)
            {
                scene.Feedback = $"You missed!";
            }
        }