我正在尝试制作一个简单的流氓,例如在 c# 控制台应用程序中进行练习,但无法弄清楚如何将各种效果添加到项目中



我想实例化一个新项目,并从已经建立的效果池中为其提供一个效果。

例如,我想使用名称"保健药"以及治愈给定实体/参与者#生命值的效果。

实体类:

class Entity
{
public string Name;
public float Health;
public List<Item> items;
public Entity(string name, float health, List<Item> items)
{
Name = name;
Health = health;
this.items = items;
}
}

物品类别:

class Item
{
public string Name;
public Effects effect;
public Item(string name, Effects effect)
{
Name = name;
this.effect = effect;
}
}

效果类别:

class Effects
{
public static void Heal(Entity entity, float amount)
{
entity.Health += amount;
Console.WriteLine($"{entity.Name} gained {amount} health");
}
}

主程序:

static void Main(string[] args)
{
Entity Player = new Entity("Main Player", 50);
Item potion = new Item("health potion", Effects.Heal(Player, 25));
List<Item> itemsOnPlayer = new List<Item>();
itemsOnPlayer.Add(potion);
Player.items = itemsOnPlayer;
}

我知道我做错了什么,但我不知道该怎么做才能让它发挥作用,任何帮助都将不胜感激。

您冷模型效果为[Action<Entity>][1]

class Item
{
public string Name;
public Action<Entity> Effect;
public Item(string name, Action<Entity> effect)
{
Name = name;
this.Effect = effect;
}
}

然后

Item potion = new Item("health potion", Player => Effects.Heal(Player, 25));

相关内容

最新更新