简体   繁体   English

我正在尝试在 c# 控制台应用程序中制作一个简单的流氓进行练习,但不知道如何为项目添加各种效果

[英]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.

相关问题 初学者尝试在 C# 中制作天气预报应用程序,无法弄清楚为什么我的代码不起作用 - Beginner trying to make a weather forecast app in C#, can't figure out why my code won't work 无法弄清楚如何去除 C# 中 windows 控制台底部的空白空间 - Can't figure out how to get rid of blank space at the bottom of windows console in C# 我正在尝试制作音乐播放器,但我无法弄清楚 openfiledialog 多选是如何工作的 - I'm trying to make a music player, but I can't figure out how does the openfiledialog multiselect work 尝试使用链表在C#中创建迷宫。 无法弄清楚如何开始游戏 - Trying to create a maze in C# using a linked list. Can't figure out how to start the game C#:我无法弄清的三个构建错误 - C#: Three build errors that I can't figure out 我可以让 Windows C# 控制台应用程序使用不同的终端,例如 PowerShell 或 MSYS 吗? - Can I make a Windows C# console app use a different terminal, like PowerShell or MSYS? 无法弄清楚如何将此 JSON 反序列化为 C# - Can't figure out how to deserialize this JSON into C# 如何判断是否从桌面或cmd shell启动了C#应用程序? - How can I figure out whether a C# app was launched from the desktop or the cmd shell? 无法在c#中找出这个公式 - Can't figure out this formula in c# 试图为战列舰建立坐标,无法弄清楚如何制作x行-a,b,c,…和y行,1、2、3…,10 - Trying to make coordinates for battleship, can't figure out how to make x row - a,b,c,… and y row- 1,2,3…,10
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM