Item system - design patterns
Members - Reputation: 106
Posted 10 November 2012 - 04:35 AM
I want to design an easily extensible item system for my game where I don't need to modify existing code much. I want to add items with new effects on the fly.
Traits my items have in common:
an npc sell price,
if it can be equipped,
if it is usable in battle,
if it is usable outside of battle,
So I can encapsulate this already into a class. But now there are item effects.
heals x health points,
heals x mana points,
removes debuff x,
adds buff x,
gives x stat points on equipping,
has x% chance to create some other effect when equipped,
And those can be combined like: heals x health and mana points. The first four are examples for effects a usable item can have while the last two are examples for effects equipable items can have.
The idea behind it is that I have this structure in a database as well and I can add a new item with a new effect combination in my database, my code reads this then and builds it together into a new fancy item, without me having to modify much. The only time I need to modify my code is when I add new effects obviously.
How would you put this together in design pattern(s)? I am writing my game in a language which has support for OO obviously.
Members - Reputation: 695
Posted 10 November 2012 - 08:26 AM
Halfway down the trail to Hell...
Members - Reputation: 105
Posted 10 November 2012 - 10:23 AM
the effects are generic and you dont even have to name them or make them all in advance thats the work of you item creator.
now the effects need nothing unless you adress them BUT you can make new effect any time you need.
Crossbones+ - Reputation: 2980
Posted 12 November 2012 - 02:01 AM
I worked on an RPG-oriented game system years ago with a friend for his MUD. I don't remember much but in the end we had to classify all the possible effects in advance to resolve cross-dependancies when generating.
So for example, if an object dealt fire damage, it couldn't roll a ice damage enchant (he decided fire would negate ice).
Similarly, we had to merge damage bonus such as +30%, +20% ends up being +50% and not (130*120=+56%).
Stacking was surprisingly involved as we needed to figure out if similar effect would stack linearly (+), negate each other, not stack, or combine by (*).
Besides generation, game play was relatively straightforward, we designed a set of signals/callbacks/events to be broadcast to each object. They took care of implementing the per-object requisite testing, checking against various conditions etc.
I have to admit the way we made combat work was pretty ugly. I'd probably make it nicer nowadays but at the time I was really unsatisfied by it, magic such as "regen" or anything else enabling lasting effects went through several holes in the design.
Edited by Krohm, 12 November 2012 - 02:06 AM.
Members - Reputation: 756
Posted 12 November 2012 - 02:25 AM
The effect system manages a collection of effects.
Each effect has a unique ID (the ID can be either string or integer, whatever), and implement an interface, e.g IEffect.
IEffect has a function, something like, void perform(Item * item);
When you add new effect, just implement IEffect and add the interface to effect system.
The item system manages a collection of items.
The items can be read from database, or added by code.
An item holds a collection of effect IDs.
When you want to do the effect, just ask the effect system with the effect ID, and the call IEffect::perform with the given item.
With this design, your item only knows an effect ID. It doesn't care what's the effect, or how effect works.
Edited by wqking, 12 November 2012 - 02:27 AM.
cpgf library -- free C++ open source library for reflection, serialization, script binding, callbacks, and meta data for OpenGL Box2D, SFML and Irrlicht.
v1.5.5 was released. Now supports tween and timeline for ease animation.
Members - Reputation: 325
Posted 13 November 2012 - 11:49 AM
Effects were hardcover as a series of functions. Ie DealDamage(target&, amountOfDamage). I had a separate functions that would validate cost, target, etc.
If every thing passed another function would parse the effect and pass its parameters and the desired target to the abilities corresponding function which would apply the actual effect.
[ current projects' videos ]
[ Zolo Project ]
I'm not mean, I just like to get to the point.
Members - Reputation: 346
Posted 16 November 2012 - 10:28 AM