这个实体定义模型是否太冗长了?
我正在开发视频游戏,我希望它具有高度的可修改性。目前,我所有的游戏逻辑都是用 Python 定义的,我的引擎逻辑用 C++ 定义,我的数据用 XML 定义。我想向您解释一下我的游戏中实体是如何定义的,并听听您的想法;例如,它是否太冗长了?
首先,一些背景知识:
- 实体由组件组成(基于组件的实体模型)。
- 组件可以有两种类型:属性和行为。
- 属性只是“哑”数据。它们是健康、速度等。
- 行为定义了实体如何与其属性、世界、其他实体交互。
定义属性
我将逐条讨论这一点。首先我将解释如何定义属性。在 Python 中,属性可能看起来像这样:
class Life(Attribute):
def __init__(self):
# Default values
self.health = 100
self.toxicity = 0
非常基本。现在,在 XML 文件中,可以为我们定义的每个实体赋予属性中的每个字段不同的值:
<Attribute>
<Name>life</Name>
<Class>hero.attributes.Life</Class>
<Field>
<Name>health</Name>
<Value>100</Value>
</Field>
<Field>
<Name>toxicity</Name>
<Value>78</Value>
</Field>
</Attribute>
Name
— 属性的标识符(有点像 Python 中的对象)。当我们定义行为时将会很有用。Class
— 此属性的 Python 类。Field
— 属性的字段。Field.Name
— 必须与 Python 字段相同('self.health' → 'health')。Field.Value
— 该字段的值。
定义行为
正如我之前所说,行为与实体的属性交互。例如,下面的伤害行为需要了解生命属性。
class Damage(Behavior):
def __init__(self):
self.life = None
用于设置行为的 XML 代码与设置属性的 XML 代码类似,但并不完全相同:
<Behavior>
<Class>hero.behaviors.Damage</Class>
<Attribute>
<Field>life</Field>
<Link>life</Link>
</Attribute>
</Behavior>
Class
— 行为的 Python 类;Attribute.Field
— 放置对属性的引用的行为字段Attribute.Link
— 要链接到的属性。必须是上面定义的属性的 Name 标记中的值之一。
I'm developing video game, and I'd like it to be highly modable. Currently, all my game logic is defined in Python, my engine logic in C++, and my data in XML. I'd like to explain to you how an entity is defined in my game and listen to what you think; for example, is it too verbose?
First, some background:
- Entities are made out of components (component-based entity model).
- Components can be of two types: Attributes and Behaviors.
- Attributes are just 'dumb' data. They are things like health, speed, etc.
- Behaviors define how the entity interacts with its Attributes, the world, other entities.
Defining an Attribute
I'll go over this piece by piece. First I'll explain how to define an Attribute. In Python, and Attribute might look something like this:
class Life(Attribute):
def __init__(self):
# Default values
self.health = 100
self.toxicity = 0
Pretty basic. Now, in the XML file, each field in the Attribute can be given a different value for each entity we define:
<Attribute>
<Name>life</Name>
<Class>hero.attributes.Life</Class>
<Field>
<Name>health</Name>
<Value>100</Value>
</Field>
<Field>
<Name>toxicity</Name>
<Value>78</Value>
</Field>
</Attribute>
Name
— an identifier for the attribute (kinda like objects in Python). Will be useful when we define Behaviors.Class
— the Python class of this attribute.Field
— a field of the attribute.Field.Name
— must be the same as the Python field ('self.health' → 'health').Field.Value
— value of this field.
Defining a Behavior
As I've said earlier, Behaviors interact with the entity's Attributes. For example, the Damage Behavior below needs to know about the Life Attribute.
class Damage(Behavior):
def __init__(self):
self.life = None
The XML code for setting up a Behavior is similar to that of an Attribute, but not identical:
<Behavior>
<Class>hero.behaviors.Damage</Class>
<Attribute>
<Field>life</Field>
<Link>life</Link>
</Attribute>
</Behavior>
Class
— Python class of the Behavior;Attribute.Field
— the field of the Behavior where the reference to the Attribute is to be placedAttribute.Link
— what Attribute to link to. Must be one of the value in the Name tags of the Attributes defined above.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否考虑过使用 json 而不是 XML?它不那么冗长,更具可读性,并且可以转换为现成的 Python 数据结构:
例如:
并将 dict 转换回 json,
Have you considered using json instead of XML? It is less verbose, more readable, and can be converted into a ready-to-use Python data structure:
For example:
And to convert the dict back into json,