An object is built from components:
class Object3 {
map<string, shared_ptr<Component3>> mComponents;
Component3* getComponent(string iName);
};
class Component3 {
virtual MessageResult handleMessage(Message& iMessage);
Object3* mOwner;
};
class Message {
MessageType getMessageType() const;
private:
MessageType mType;
};
class UpdateMessage : Message {
UpdateMessage(real iTime) : Message(MT_UPDATE), time(iTime);
const real time;
};
I tell the objects to render and/or update with messages.
The DisplayComponent3 wants to know where to display the mesh, and the current skeleton pose(among other things). This is done with some ugly, but flexible, code somewhat similar to dataports:
class DataBase {
};
template<class T>
class Data : DataBase {
T t;
T& getReference() { return t; }
// some virtual void setValue(GenericTypeSuchAsStringOrXmlNode)=0;
};
class DataContainer {
map<string, shared_ptr<DataBase> > mData;
DataBase& getData(string iName);
};
shared_ptr<DataBase> BuildData(string& iType); // probably some factory
template<class T>
T& GetDataBase(DataContainer& iContainer, string iName, string iType) {
if( !iContainer.hasData(iName) ) iContainer.addData( iName, BuildData(iType) );
return ((Data<T>& )iContainer.getData(iName)).getReference();
}
// vec3 is a vector in 3d
vec3& GetDataVec3(DataContainer& iContainer, string iName) {
return GetDataBase<vec3>(iContainer, iName, "vec3");
}
// usage
class MyComponent3 : Component3 {
MyComponent3(Object3& iOwner) : Component3(iOwner), mLocation(GetDataVec3(getDataConatiner(), "location")) {}
vec3& mLocation;
};
Since it's a lazy eval Objects only store what the components exchange, so if the object doesn't need the location, it doesn't have it. Since the varaible source is string-based theese can be read from a cfg and with this principle it was simple to apply the RandomMovement (pretty useless but fun), and the WithinRect component to the 2d cursor(location based), from the background-animation(uv based).
I'm thinking of adding some sort of layer system, so I can add components that (temporarily) remove others from some/all messages. Such components may be a Tranqualized, or ResurectAfterSomeTime component.
I'm unsure why subsystems are hard to access with components. If some component want access to some subsystem all it does is something like this:
class SomeComponent3 : Component3 {
SomeComponent3(Object3& iOwner) : Component3(iOwner), mSomeSystem( (dynamic_cast<SomeSystem&>(iOwner.getWorld()
.getSystem("SomeSystem"))) ) {
}
SomeSystem& mSomeSystem;
};






