使用随机字符串创建实例

时间:2017-10-24 07:32:59

标签: c#

我正在进行文字冒险游戏,但由于我想要随机的敌人,我在战斗系统方面遇到了一些困难。从

获取敌人名字的字符串
string enemyName = map.GetLocation().GetEnemyName();

但是如何从这个字符串转到实例,因为这不起作用:

Monster Monster = new enemyName(6);

还有其他很好的方法可以解决这个问题吗?

这是我目前的代码:

Program.cs的

static void Combat(ref Map map, ref List<string> menuItems, ref Inventory Inventory, ref Player Player)
        {
        int choice;
        Player.SetInCombat(true);
        bool inCombat = Player.GetInCombat();

        string enemyName = map.GetLocation().GetEnemyName();

        Console.WriteLine(enemyName);

        Monster Monster = new Skeleton(6);

        //Player
        int hpPlayer = Player.GetHealth();
        int dpPlayer = Convert.ToInt32(Player.GetDefence());
        int attackPlayer = Player.GetAttackPoints();

        bool run = false;

        //Enemy
        string name = Monster.GetName();
        int hpEnemy = Monster.GetHealth();
        int dpEnemy = Monster.GetDefence();
        int enemyLevel = Monster.GetLevel();
        int attackEnemy = Monster.GetAttack();
            string attackName = Monster.GetAttackName();
}

Monster.cs

abstract class Monster
    {
        protected int level;
        protected string name;
        protected string attackName;
        protected int maxHealth;
        protected int health;
        protected int attack;
        protected int defence;
        protected int healthMultiplier;
        protected int attackMultiplier;
        protected int defenceMultiplier;

        public Monster(int level)
        {
            this.level = level;
        }

        public string GetName()
        {
            return name;
        }

        public string GetAttackName()
        {
            return attackName;
        }

        public int GetMaxHealth()
        {
            return maxHealth;
        }

        public int GetHealth()
        {
            return health;
        }

        public int GetAttack()
        {
            return attack;
        }

        public int GetDefence()
        {
            return defence;
        }

        public abstract void TakeHit(int damage, ref Player Player);

        public int GetLevel()
        {
            return level;
        }
    }

以下是4种不同类型的敌人中的一种:

Skeleton.cs

class Skeleton : Monster
    {
        public Skeleton(int level)
            : base(level)
        {
            //Monster stats

            name = "Skeleton";
            attackName = "Bone_thrower";
            healthMultiplier = 2;
            attackMultiplier = 1;
            defenceMultiplier = 25;

            maxHealth = (level * healthMultiplier);
            health = maxHealth;
            attack = (level / attackMultiplier);
            defence = (level / defenceMultiplier);
        }

        public override void TakeHit(int damage, ref Player Player)
        {
            if (health - damage <= 0)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("{0} took {1} points of damage.", name, damage);
                Console.ForegroundColor = ConsoleColor.White;
                Console.WriteLine("Press any keys to continue...");
                Console.ReadKey();
                Player.SetInCombat(false);
            }
            else
            {
                health -= damage;
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("{0} took {1} points of damage.", name, damage);
                Console.ForegroundColor = ConsoleColor.White;
            }
        }
    }

1 个答案:

答案 0 :(得分:1)

像您这样的案例中使用的常见模式是工厂方法。您可以创建方法GetEnemyName,而不是GetEnemy方法。有一种不同的方法可以从随机数创建敌人。最简单的方法,当你只有几个怪物类时效果很好,将是一个switch语句:

Random numberGenerator = new Random();

public Monster GetEnemy(int level) {
    const int numberOfEnemies = 2; //Set appropriate value
    int enemyIndex = numberGenerator.Next(numberOfEnemies);

    switch (enemyIndex) {
        case 0:
            return new Skeleton(level);
        case 1:
            return new OtherMonsterType(level);
        default:
            //Should never be called, serves as a safeguard and to make the code compile
            throw new NotSupportedException() 
    }
}

如果您想要更多地动态管理怪物类型(例如,在更高级别添加新的怪物类型),您可以使用将创建怪物的方法列表(可以用简单的方式表达)使用lambda表达式):

Random numberGenerator = new Random();
List<Func<int, Monster>> monsterCreators = new List<Func<int, Monster>> {
    level => new Skeleton(level),
    level => new OtherMonsterType(level),
    //...
}

public Monster GetEnemy(int level) {
    int enemyIndex = numberGenerator.Next(monsterCreators.Count);    
    return monsterCreators[enemyIndex](level);
}