Reading several other questions and answers on using a component based system to define items I want to use one for the items and spells in a web game written in PHP. I'm just stuck on the implementation.
I'm going to use a DB schema suggested in this series (part 5 describes the schema);
http://t-machine.org/index.php/2007/09/03/entity-systems-are-the-future-of-mmog-development-part-1/
This means I'll have an items table with generic item properties, a table listing all of the components for an item and finally records in each component table used to make up the item.
Assuming I can select the first two together in a single query, I'm still going to do N queries for each component type. I'm kind of fine with this because I can cache the data into memcache and check there first before doing any queries. I'll need to build up the items on every request they are used in so the implementation needs to be on the lean side even if they're pulled from memcache.
But right there is where I feel confident about implementing a component system for my items ends. I figure I'd need to bring attributes and behaviors into the container from each component it uses. I'm just not sure how to do that effectively and not end up writing a lot of specialized code to deal with each component.
For example an AttackComponent might need to know how to filter targets inside of a battle context and also maybe provide an attack behavior. That same item might also have a UsableComponent which allows the item to be used and apply some effect onto a different set of targets filtered differently from the same battle context. Then not every part of an item is an active part, an AttributeBonusComponent might need to only kick in when the item is in an equipped state or when displaying the item details page.
Ultimately, how should I bring all of the components together into the container so when I use an item as a weapon I get the correct list of targets? Know when a weapon can also be used as an item? Or to apply the bonuses the item provides to a character object?
I feel like I've gone too far down the rabbit hole and I can't grasp onto the simple solution in front of me. (If that makes any sense at all.)
Likewise if I were to implement the best answer from here I feel like I'd have a lot of the same questions.
How to model multiple "uses" (e.g. weapon) for usable-inventory/object/items (e.g. katana) within a relational database
Answer
It looks like you are using relational modeling. There is an alternative method: property/prototype modeling, which Steve Yegge used to create his "ultimate extensible" MMORPG, Wyvern. Basically, each game object is stored as a single blob (only one query per object) which is then parsed into a property list after retrieval. The flexibility of property lists allows different game objects to have different sets of properties as needed.
Yegge's The Universal Design Pattern goes into depth about property/prototype modeling. Steve's blog posts tend to be quite long, so I will try to point you to the relevant sections and summarize the points relevant to your question:
- Wyvern uses an implementation of the Properties Pattern. A game object is basically a bag of arbitrary properties. Any object to can serve as the prototype for any other object, resulting in amazing open-ended flexibility.
- Persisting property lists. There are various methods of serializing and saving property lists. Wyvern uses a database, "shoving the XML-serialized property list into a text/clob column, and denormalizing the twenty or thirty fields ... needed for queries into their own columns."
- The datastore of property lists now needs a method of making queries on them. Steve makes several suggestions. Simple text-based queries don't work well for hierarchical data. XML databases or JavaScript/JSON + jQuery might be answers to this problem.
No comments:
Post a Comment