![](/img/trans.png)
[英]Beginner trying to make a weather forecast app in C#, can't figure out why my code won't work
[英]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
我想實例化一個新項目,並從一個已經建立的效果池中給它一個單一的效果。
例如,我想創建一個名為“健康葯水”的 HealthPotion,以及一個可以治愈給定實體/演員 # 生命值的效果。
實體 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;
}
}
class 項目:
class Item
{
public string Name;
public Effects effect;
public Item(string name, Effects effect)
{
Name = name;
this.effect = effect;
}
}
效果 class:
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;
}
我知道我做錯了什么,但我不知道該怎么做才能讓它工作,任何幫助將不勝感激。
你冷 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;
}
}
然后
Item potion = new Item("health potion", Player => Effects.Heal(Player, 25));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.