基于身份的NPC系统

时间:2011-07-15 03:58:07

标签: entity-framework xna theory

我有一堆不同的npcs。它们都有不同的属性和不同的AI,所以我为每种类型创建了一个单独的类,并从基类Entity派生出来。我想这样做,以便我可以为每种类型的实体分配一个ID,所以我可以调用CreateNewEntity(int id,Vector2位置)。我该怎么做?我也有一种感觉,我设计得很糟糕。是真的

3 个答案:

答案 0 :(得分:1)

从基础实体继承听起来像是一种好方法。所有对象之间共享的任何内容都应该在基本实体中,并且每种类型的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;
    }
}

答案 1 :(得分:1)

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

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))

另一个是你可以获得名称与传递给你的create方法的字符串相匹配的Type。

答案 2 :(得分:0)

听起来像flyweight模式在这里是有益的:http://www.primos.com.au/primos/Default.aspx?PageContentID=27&tabid=65

您可能希望区分使每个实体不同的核心状态,然后尽可能多地放在共享实例中。

相关问题