开发者

Is this entity definition model too verbose?

开发者 https://www.devze.com 2023-04-03 20:07 出处:网络
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 ent

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 th开发者_StackOverflow社区e Behavior where the reference to the Attribute is to be placed
  • Attribute.Link — what Attribute to link to. Must be one of the value in the Name tags of the Attributes defined above.


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:

import json

x='''{"Field": [{"Name": "health", "Value": 100}, {"Name": "toxicity", "Value": 78}],
    "Name": "life",
    "Class": "hero.attributes.Life"}'''

attribute=json.loads(x)

# {u'Class': u'hero.attributes.Life',
#  u'Field': [{u'Name': u'health', u'Value': 100},
#             {u'Name': u'toxicity', u'Value': 78}],
#  u'Name': u'life'}

And to convert the dict back into json,

attr=json.dumps(attribute)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号