[英]I am trying to make a simple rogue like in c# console app for practice and can't figure out how to add various Effects to items
I want to instantiate a new item and give it a single effect from a pool of already established effects.我想实例化一个新项目,并从一个已经建立的效果池中给它一个单一的效果。
For example, I want to create a HealthPotion with the name "health potion" and an effect that heals a given entity/actor for # amount of health.例如,我想创建一个名为“健康药水”的 HealthPotion,以及一个可以治愈给定实体/演员 # 生命值的效果。
The Entity class:实体 class:
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;
}
}
The Item class: class 项目:
class Item
{
public string Name;
public Effects effect;
public Item(string name, Effects effect)
{
Name = name;
this.effect = effect;
}
}
The Effects class:效果 class:
class Effects
{
public static void Heal(Entity entity, float amount)
{
entity.Health += amount;
Console.WriteLine($"{entity.Name} gained {amount} health");
}
}
The Main Program:主程序:
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;
}
I know I'm doing something really wrong but I have no clue what to do to get this to work, any help would be greatly appreciated.我知道我做错了什么,但我不知道该怎么做才能让它工作,任何帮助将不胜感激。
You cold model Effect as an [Action<Entity>][1]
你冷 model 效果作为[Action<Entity>][1]
class Item
{
public string Name;
public Action<Entity> Effect;
public Item(string name, Action<Entity> effect)
{
Name = name;
this.Effect = effect;
}
}
then然后
Item potion = new Item("health potion", Player => Effects.Heal(Player, 25));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.