Implementing game objects with builder?

Started by
5 comments, last by Juliean 10 years, 6 months ago

Right now I'm developing a 2d RPG in java and am stuck finding a way to add objects into my game. I want my game to be data driven using XML. I'm just unsure on the best way to add objects into my game. I've done some research and a lot of people seem to say that a "Builder pattern" would work well for this situation. The problem is that I'm not fully sure how to implement a builder pattern for my needs. Do I still need an inheritance hierarchy? Or would it be 100% composition? For example, would I build my item 100% out of components? Or would I still make a sword class, which extends MeleeWeapon, which extends Weapon, which extends GameItem?

Thanks for any help.

Advertisement

Do I still need an inheritance hierarchy? Or would it be 100% composition? For example, would I build my item 100% out of components? Or would I still make a sword class, which extends MeleeWeapon, which extends Weapon, which extends GameItem?

You can go both ways, but appearently the more common approach nowadays is the composition one. As from my own experience, and IHMO, I can heavily recommend a rigorous composition approach over the inheritance one - latter is most likely harder to maintain, debug and extent. A file from an component-based serialized object could look like this:


<World>
    <Entity>
        <Ambient b="1.0" g="1.0" i="3.4" r="1.0"/>
        <Light visible="1"/>
    </Entity>
    <Entity>
        <Diffuse b="1.0" g="1.0" i="70.8" r="1.0"/>
        <Direction x="-1.0" y="-1.0" z="-1.0"/>
        <Light visible="1"/>
        <Specular b="1.0" g="1.0" i="2500.0" r="1.0"/>
        <ShadowCaster />
    </Entity>
    <Entity>
        <Transform />
        <Position x="0.0" y="0.0" z="0.0"/>
        <Terrain name="Terrain"/>
    </Entity>
    <Entity>
        <Actor model="Sky" />
        <Position x="0.0" y="0.0" z="0.0"/>
        <Scalation x="100.0" y="100.0" z="100.0"/>
        <Transform />
    </Entity>
    <Entity>
        <Actor model="Barracks" />
        <Position x="-9" y="0.3" z="-7"/>
        <Scalation x="0.5" y="0.5" z="0.5"/>
        <Rotation x="0" y="-15" z="0" />
        <Transform />
        <Bounding x="0" y="0" z="0" sx="2" sy="1" sz="1.5" />
    </Entity>
    <Entity>
        <Actor model="Depot" />
        <Position x="27.5" y="0.2" z="4.5"/>
        <Scalation x="0.5" y="0.5" z="0.5" />
        <Rotation x="0" y="20" z="0" />
        <Transform />
        <Bounding x="0" y="0" z="0" sx="3" sy="1" sz="2.5" />
    </Entity>
    <Entity>
        <Actor model="Garage" />
        <Position x="23.0" y="0." z="17.5"/>
        <Scalation x="0.25" y="0.25" z="0.25" />
        <Rotation x="0" y="35" z="0" />
        <Transform />
        <Bounding x="0" y="0" z="0" sx="3" sy="1" sz="3" />
    </Entity>
</World>

Very easy to parse, and data-driven all the way.

I love the example Julien (not being sarcastic) ... but just so you know, Scalation is not related to the "scale" of an object. In english it is: Position, Rotation and Scale, not scalation. Scalation is a visual things about the scale patterns on reptiles - similar to Coloration.

I'm not sure how to set up an item builder or anything like that is my problem. I can get the data from an XML, but I don't know what to do with it. I want to basically have it so I can do something like this


Weapon wep = WeaponBuilder.name("Weapon1").enchantment(Enchantment.FIRE).build();

Where the arguments are just the data I pull from my XML


Weapon wep = WeaponBuilder.name("Weapon1").enchantment(Enchantment.FIRE).build();

I'm quite unsure what this example is about. With an entity/component system like I've posted, building the weapons is trivial. It would be just a constructor, and maybe some setter-calls:


class Weapon
{
public:
	Weapon(const std::wstring& stName, float damage);
	
	void AddElement(unsigned int id);
	
};

// usage:

const std::wstring stName = // from xml
const float damage = // from xml
const vector<unsigned int> vElements = // from xml


Weapon wep = entity.AttachComponent<Weapon>(stName, damage, elements); // only if you are using entity/component, otherwise normal constructor
for(auto element : vElements)
{
       wep.AddElement(element);
}

// from xml-file

<Entity>
        <Weapon name="Weapon1" damage="100">
                <Element>1>/Element>
        <Weapon/>
</Entity>

Note that really you would want to seperate the weapon definition and refer from the entity only by the name of it. But what would you need a WeaponBuilder-class for such a trivial task as constructing an object?


I love the example Julien (not being sarcastic) ... but just so you know, Scalation is not related to the "scale" of an object. In english it is: Position, Rotation and Scale, not scalation. Scalation is a visual things about the scale patterns on reptiles - similar to Coloration.

Thanks, and thanks for the pointer with the scalation thing... damn, thats one thing I really didn't know, one more mistake I'm not going to make anymore in english ;)

Where can I learn all about Component/Entity systems then?

My understanding is that your entity is no more than just a Unique ID, and it's linked to all it's components by that ID. I really don't fully understand how they work though.


Where can I learn all about Component/Entity systems then?

For example, you can take a look at EntityX manual/implementation, specially if you are using c++ (11).


My understanding is that your entity is no more than just a Unique ID, and it's linked to all it's components by that ID. I really don't fully understand how they work though.

Thats an implementation detail, which I didn't employ in my own ECS, and I'm fairing well so far. There is about a hundred different views on ecs. The main premise however is to seperate data and function, and employ composition rather than inheritance. You don't even need to go that full route, but thinking about having an inheritance-tree for different types of weapons is a tad bit frightning, honestly. If you design your weapons to be data-driven, like in my example, by adding parameters that determine appearance/parameters of that weapons, so that you can add hundreds of different weapons without creating a new derived class, you can get just as far with a regular parent entity class. You just shouldn't create a new element in an inheritance chain to implement content, thats the key essence, which is easier done with ECS IMHO.

This topic is closed to new replies.

Advertisement