Game Development Reference
In-Depth Information
data structure changes in the future, I don
t have to update this code. It also makes the code a bit easier
to read. Since the variable type is deduced statically (at compile time), there
'
s no runtime cost at all.
In fact, if you hover over the variable itself in Visual Studio 2010, a tooltip will even tell you what the
type is.
'
First, this function grabs the name of the component from the XML node passed in.
Then it searches the component creator map to find the specific creator function and
calls it to instantiate the component. If it can
t find the creator, it tosses up an error
message and returns in disgrace. The creator functions are trivially simple. They just
return the appropriate instantiated object.
'
ActorComponent* CreateCubePhysicsComponent()
{
return GCC_NEW BoxPhysicsComponent;
}
Back to the CreateComponent() function, the newly created component is then
initialized by calling its Init() function. Assuming this succeeds, the newly initial-
ized component is returned back to the CreateActor() function.
And there you have it! That
'
s the process for creating and initializing an actor from a
data file.
Defining Actors and Components
Now that you have an understanding of how actors get into the game, it
'
s time to
show you what an actor really looks like. Here
'
s the Actor class:
class Actor
{
friend class ActorFactory;
typedef std::map<ComponentId, StrongActorComponentPtr> ActorComponents;
ActorId m_id; // unique id for the actor
ActorComponents m_components; // all components this actor has
public:
explicit Actor(ActorId id);
˜
Actor(void);
bool Init(TiXmlElement* pData);
void PostInit(void);
void Destroy(void);
 
 
Search WWH ::




Custom Search