基于ID的NPC系统

发布于 2024-11-19 22:29:17 字数 172 浏览 3 评论 0原文

我有很多不同的NPC。它们都有不同的属性和不同的 AI,因此我为每种类型创建了一个单独的类,并从基类 Entity 派生它。我想这样做,以便我可以为每种类型的实体分配一个 ID,这样我就可以调用 CreateNewEntity(int id, Vector2position)。我该怎么做?我也有一种感觉,我的设计很糟糕。是这样吗

I have a bunch of different npcs. They all have different properties and different AI, so I have made a separate class for each type, and derive it from a base class Entity. I want to make it so that I can assign an ID to each type of Entity, so I can just call CreateNewEntity(int id, Vector2 position). How would I do this? I also have a feeling that I'm designing this badly. Is that true

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

对你再特殊 2024-11-26 22:29:17

有几种方法可以做到这一点。
您可以创建一个属性,为该类型提供一些元数据,例如 ID
例如,

public class RenderableEntity{
}

[EntityTypeAttribute(Name = "Wizard"]
public class Wizard : RenderableEntity{
}

您可以将它们全部捆绑在命名空间或逻辑容器中,并按如下方式创建类型
伪:

//Create Entity
//Type Name
string entityName = "Wizard";

//Get the Type
from the namespace where the type has the custom attribute applied to it. Get the type

//Activator.Create(typeof(TheWizardType))

另一个是您可以获取名称与您传递到创建方法中的字符串相匹配的类型。

There are a couple of ways you could do this.
You could create an Attribute that will give the type some metadata such as ID
e.g.

public class RenderableEntity{
}

[EntityTypeAttribute(Name = "Wizard"]
public class Wizard : RenderableEntity{
}

you could then bundle them all in a namespace or a logical container and create the type as follows
pseudo:

//Create Entity
//Type Name
string entityName = "Wizard";

//Get the Type
from the namespace where the type has the custom attribute applied to it. Get the type

//Activator.Create(typeof(TheWizardType))

The other is that you could just get the Type where the name matches the string you passed into your create method.

秋意浓 2024-11-26 22:29:17

从基础实体继承听起来是一个好方法。所有对象之间共享的任何内容都应位于基本实体中,而特定于每种 NPC 类型的任何内容都应位于其自己的类中。具有相同目的但不同实现(例如 AI 或 CreateNewEntity)的事物应标记为虚拟,因此可以从基本实体列表中调用这些方法,但将运行正确的实现。 CreateNewEntity 可能应该是一个构造函数。

例子:

public class Entity
{
    public int Id { get; protected set; }
    public Vector2 Position { get; protected set; }
    public int Strength { get; protected set; }
    public Entity(int id, Vector2 position)
    {
        Id = id;
        Position = position;
        Strength = 1;
    }
    public virtual RunAI()
    {
        Position.X = Position.X + 1;
    }
}

public class SuperHero
{
    public SuperHero(int id, Vector2 position) : base (id, position)
    {
        Strength = 20;
    }
    public override void RunAI()
    {
        Position.X = Position.X + 500;
    }
}

Inheriting from a base entity sounds like a good approach. Anything that is shared between all your objects should be in the base entity, and anything that is specific to each type of NPC, should be in their own class. Things that have the same purpose, but different implementation (like AI, or CreateNewEntity) should be marked virtual, so the methods can be called from a list of base entities, but the correct implementation will run. CreateNewEntity should probably be a constructor.

example:

public class Entity
{
    public int Id { get; protected set; }
    public Vector2 Position { get; protected set; }
    public int Strength { get; protected set; }
    public Entity(int id, Vector2 position)
    {
        Id = id;
        Position = position;
        Strength = 1;
    }
    public virtual RunAI()
    {
        Position.X = Position.X + 1;
    }
}

public class SuperHero
{
    public SuperHero(int id, Vector2 position) : base (id, position)
    {
        Strength = 20;
    }
    public override void RunAI()
    {
        Position.X = Position.X + 500;
    }
}
何必那么矫情 2024-11-26 22:29:17

听起来,蝇量模式在这里会很有用: http: //www.primos.com.au/primos/Default.aspx?PageContentID=27&tabid=65

您可能想要区分使每个实体不同的核心状态,然后放置尽可能多的内容在共享实例中可能。

It sounds like the flyweight pattern would be beneficial here: http://www.primos.com.au/primos/Default.aspx?PageContentID=27&tabid=65

You probably want to differentiate the core state that makes each entity different and then put as much as possible in the shared instance.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文