Cafu Engine
cf::GameSys::ComponentBaseT Class Reference

This is the base class for the components that an entity is composed/aggregated of. More...

#include "CompBase.hpp"

Inheritance diagram for cf::GameSys::ComponentBaseT:

Public Member Functions

 ComponentBaseT ()
 The constructor. More...
 
 ComponentBaseT (const ComponentBaseT &Comp)
 The copy constructor. More...
 
virtual ComponentBaseTClone () const
 The virtual copy constructor. More...
 
virtual ~ComponentBaseT ()
 The virtual destructor. More...
 
virtual const char * GetName () const
 Returns the name of this component. More...
 
EntityTGetEntity () const
 Returns the parent entity that contains this component, or NULL if this component is currently not a part of any entity. More...
 
TypeSys::VarManTGetMemberVars ()
 Returns the variable manager that keeps generic references to our member variables, providing a simple kind of "reflection" or "type introspection" feature. More...
 
template<class T >
void SetMember (const char *Name, const T &Value)
 Sets the member variable with the given name to the given value. More...
 
ArrayT< ApproxBaseT * > & GetInterpolators ()
 Returns the interpolators that have been registered with this component. More...
 
bool InitClientApprox (const char *VarName)
 Registers the member variable with the given name for interpolation over client frames in order to bridge the larger intervals between server frames. More...
 
void Serialize (cf::Network::OutStreamT &Stream) const
 Writes the current state of this component into the given stream. More...
 
void Deserialize (cf::Network::InStreamT &Stream, bool IsIniting)
 Reads the state of this component from the given stream, and updates the component accordingly. More...
 
bool CallLuaMethod (const char *MethodName, int NumExtraArgs, const char *Signature="",...)
 Calls the given Lua method of this component. More...
 
virtual void UpdateDependencies (EntityT *Entity)
 This method is called whenever something "external" to this component has changed: More...
 
virtual unsigned int GetEditorColor () const
 Returns a color that the Map Editor can use to render the representation of this component's entity. More...
 
virtual BoundingBox3fT GetEditorBB () const
 Returns a bounding-box that the Map Editor can use to render the representation of this component's entity and for related hit tests in the 2D and 3D views after mouse clicks. More...
 
virtual BoundingBox3fT GetCullingBB () const
 This method returns a bounding-box that encloses the visual representation of this component. More...
 
virtual const
cf::ClipSys::ClipModelT
GetClipModel ()
 This method returns the clip model of this component, if any. More...
 
virtual void PreCache ()
 Initializes any resources that may be needed on the client or server ahead of time. More...
 
virtual bool Render (bool FirstPersonView, float LodDist) const
 This method implements the graphical output of this component. More...
 
virtual void PostRender (bool FirstPersonView)
 This method provides an opportunity for another render pass. More...
 
virtual void OnPostLoad (bool OnlyStatic)
 This method is called after all entities and their components have been loaded. More...
 
virtual bool OnInputEvent (const CaKeyboardEventT &KE)
 This method handles keyboard input events. More...
 
virtual bool OnInputEvent (const CaMouseEventT &ME, float PosX, float PosY)
 This method handles mouse input events. More...
 
void OnServerFrame (float t)
 Advances the component one frame (one "clock-tick") on the server. More...
 
void OnClientFrame (float t)
 Advances the component one frame (one "clock-tick") on the client. More...
 
virtual const
cf::TypeSys::TypeInfoT
GetType () const
 
- Public Member Functions inherited from RefCountedT
unsigned int GetRefCount () const
 

Static Public Member Functions

static void * CreateInstance (const cf::TypeSys::CreateParamsT &Params)
 

Static Public Attributes

static const cf::TypeSys::TypeInfoT TypeInfo
 

Static Protected Member Functions

static int Get (lua_State *LuaState)
 
static int Set (lua_State *LuaState)
 
static int GetExtraMessage (lua_State *LuaState)
 
static int Interpolate (lua_State *LuaState)
 
static int GetEntity (lua_State *LuaState)
 
static int InitClientApprox (lua_State *LuaState)
 
static int toString (lua_State *LuaState)
 

Static Protected Attributes

static const luaL_Reg MethodsList []
 The list of Lua methods for this class. More...
 
static const char * DocClass
 
static const cf::TypeSys::MethsDocT DocMethods []
 
static const cf::TypeSys::MethsDocT DocCallbacks []
 

Additional Inherited Members

- Protected Member Functions inherited from RefCountedT
 RefCountedT (const RefCountedT &)
 
RefCountedToperator= (const RefCountedT &)
 

Detailed Description

This is the base class for the components that an entity is composed/aggregated of.

Components are the basic building blocks of an entity: their composition defines the properties, the behaviour, and thus virtually every aspect of the entity.

Components can exist in two invariants:

  • Stand-alone, independent and not a part of any entity.
  • Normally, as an active part of a entity.

Stand-alone components typically occur when they're newly instantiated, for example when they are loaded from disk, when they are instantiated in scripts, or when they are kept in the clipboard or managed in the Undo/Redo system of the Map Editor. Newly created, copied or cloned components are initially stand-alone.

A component becomes a part of an entity via the EntityT::AddComponent() method. The entity then knows the component, because it hosts it, and reversely, the component then knows the parent entity that it is a component of.

Constructor & Destructor Documentation

ComponentBaseT::ComponentBaseT ( )

The constructor.

The newly created component is initially not a part of any entity.

ComponentBaseT::ComponentBaseT ( const ComponentBaseT Comp)

The copy constructor.

The newly copied component is initially not a part of any entity, even if the source component was.

Parameters
CompThe component to create a copy of.
ComponentBaseT::~ComponentBaseT ( )
virtual

The virtual destructor.

Member Function Documentation

bool ComponentBaseT::CallLuaMethod ( const char *  MethodName,
int  NumExtraArgs,
const char *  Signature = "",
  ... 
)

Calls the given Lua method of this component.

This method is analogous to UniScriptStateT::CallMethod(), see there for details.

Parameters
MethodNameThe name of the Lua method to call.
NumExtraArgsThe number of extra arguments that have been pushed on the stack.
SignatureSee UniScriptStateT::Call() for details.
ComponentBaseT * ComponentBaseT::Clone ( ) const
virtual
void ComponentBaseT::Deserialize ( cf::Network::InStreamT Stream,
bool  IsIniting 
)

Reads the state of this component from the given stream, and updates the component accordingly.

This method is called after the state of the component has been received over the network, has been loaded from disk, has been read from the clipboard, or must be "reset" for the purpose of (re-)prediction. The implementation calls DoDeserialize() that derived classes can override to read their own data, or to run any post-deserialization code.

Parameters
StreamThe stream to read the state data from.
IsInitingUsed to indicate that the call is part of the construction / first-time initialization of the component. The implementation will use this to not wrongly process the event counters, interpolation, etc.
virtual const cf::ClipSys::ClipModelT* cf::GameSys::ComponentBaseT::GetClipModel ( )
inlinevirtual

This method returns the clip model of this component, if any.

Reimplemented in cf::GameSys::ComponentCollisionModelT, and CompGameEntityT.

virtual BoundingBox3fT cf::GameSys::ComponentBaseT::GetCullingBB ( ) const
inlinevirtual

This method returns a bounding-box that encloses the visual representation of this component.

It is used to determine if the entity is in the view-frustum of a camera, how large a region must be updated in the 2D views of a Map Editor, if the entity is in the potentially-visibility-set (PVS) of another entity, and similar purposes.

The returned bounding-box is in local space, i.e. typically centered around the origin (0, 0, 0). If the component doesn't have a visual representation, the returned bounding-box may be uninitialized (!IsInited()). Also see EntityT::GetCullingBB() for additional details.

Reimplemented in cf::GameSys::ComponentHumanPlayerT, cf::GameSys::ComponentModelT, cf::GameSys::ComponentPointLightT, and CompGameEntityT.

virtual BoundingBox3fT cf::GameSys::ComponentBaseT::GetEditorBB ( ) const
inlinevirtual

Returns a bounding-box that the Map Editor can use to render the representation of this component's entity and for related hit tests in the 2D and 3D views after mouse clicks.

The Map Editor may use the bounding-box of an entity's first component as returned by this method to render the visual representation of the entity.

Note that the returned bounding-box is often smaller than the bounding-box returned by GetCullingBB(), e.g. for light sources (whose radius and thus their indirect effects on other objects it may not cover), for trees (whose trunk it usually covers, but maybe not their crown), or for models (that, when animated, may break the limits of the static bounding-box).

On the other hand, the returned bounding-box may also be larger than the bounding-box returned by GetCullingBB(), e.g. for models that are not initialized. Such models would be "invisible" in the 2D and 3D views of the Map Editor if we didn't return "dummy" bounding-boxes for them so that users can see and work with them.

The returned bounding-box is in local entity space and is always initialized (IsInited() == true).

Reimplemented in cf::GameSys::ComponentMoverT, cf::GameSys::ComponentModelT, cf::GameSys::ComponentPlayerStartT, and cf::GameSys::ComponentTargetT.

virtual unsigned int cf::GameSys::ComponentBaseT::GetEditorColor ( ) const
inlinevirtual

Returns a color that the Map Editor can use to render the representation of this component's entity.

The Map Editor may use the color of an entity's first component as returned by this method to render the visual representation of the entity.

Reimplemented in cf::GameSys::ComponentMoverT, cf::GameSys::ComponentModelT, cf::GameSys::ComponentScriptT, cf::GameSys::ComponentCollisionModelT, cf::GameSys::ComponentParticleSystemOldT, cf::GameSys::ComponentPlayerStartT, cf::GameSys::ComponentTargetT, cf::GameSys::ComponentSoundT, and cf::GameSys::ComponentLightT.

EntityT* cf::GameSys::ComponentBaseT::GetEntity ( ) const
inline

Returns the parent entity that contains this component, or NULL if this component is currently not a part of any entity.

ArrayT<ApproxBaseT*>& cf::GameSys::ComponentBaseT::GetInterpolators ( )
inline

Returns the interpolators that have been registered with this component.

TypeSys::VarManT& cf::GameSys::ComponentBaseT::GetMemberVars ( )
inline

Returns the variable manager that keeps generic references to our member variables, providing a simple kind of "reflection" or "type introspection" feature.

bool ComponentBaseT::InitClientApprox ( const char *  VarName)

Registers the member variable with the given name for interpolation over client frames in order to bridge the larger intervals between server frames.

This method only works with variables whose related type is float, double, Vector2fT, Vector3fT or Vector3dT.

void ComponentBaseT::OnClientFrame ( float  t)

Advances the component one frame (one "clock-tick") on the client.

It typically updates eye-candy that is not sync'ed over the network. (State that is sync'ed over the network must be updated in OnServerFrame() instead.)

Note that the implementation calls DoClientFrame() that derived classes can override to implement their own custom behaviour.

Parameters
tThe time in seconds since the last client frame.
virtual bool cf::GameSys::ComponentBaseT::OnInputEvent ( const CaKeyboardEventT KE)
inlinevirtual

This method handles keyboard input events.

Parameters
KEKeyboard event instance.
Returns
Whether the component handled ("consumed") the event.
virtual bool cf::GameSys::ComponentBaseT::OnInputEvent ( const CaMouseEventT ME,
float  PosX,
float  PosY 
)
inlinevirtual

This method handles mouse input events.

Parameters
MEMouse event instance.
PosXx-coordinate of the mouse cursor position.
PosYy-coordinate of the mouse cursor position.
Returns
Whether the component handled ("consumed") the event.
virtual void cf::GameSys::ComponentBaseT::OnPostLoad ( bool  OnlyStatic)
inlinevirtual

This method is called after all entities and their components have been loaded.

It is called only once when the static part of world initializatzion is complete, i.e. after the initial values of all entities and their components have been set. Components can override this method in order act / do something / add custom behaviour at that time.

For example, a choice component can use it to set the associated text component to the initial selection, a script component can forward it to the script by calling a related script function, a component that for backwards-compatibility supports reading old variables can convert to new ones, etc.

Parameters
OnlyStatictrue if only the loading of static data is desired, e.g. when the world is instantiated in the Map Editor, false if also user-defined scripts with custom, initial behaviour should be loaded. Also see WorldT::InitFlagsT::InitFlag_OnlyStatic for related information.

Reimplemented in cf::GameSys::ComponentScriptT, and cf::GameSys::ComponentCarriedWeaponT.

void ComponentBaseT::OnServerFrame ( float  t)

Advances the component one frame (one "clock-tick") on the server.

It typically updates all game-relevant state that is sync'ed over the network to all connected game clients.

Note that the implementation calls DoServerFrame() that derived classes can override to implement their own custom behaviour.

Parameters
tThe time in seconds since the last server frame.
virtual void cf::GameSys::ComponentBaseT::PostRender ( bool  FirstPersonView)
inlinevirtual

This method provides an opportunity for another render pass.

Reimplemented in cf::GameSys::ComponentHumanPlayerT.

virtual void cf::GameSys::ComponentBaseT::PreCache ( )
inlinevirtual

Initializes any resources that may be needed on the client or server ahead of time.

The goal is to avoid in-game disruptions on frame-rate caused by lazily loaded assets.

Reimplemented in cf::GameSys::ComponentCarriedWeaponT.

virtual bool cf::GameSys::ComponentBaseT::Render ( bool  FirstPersonView,
float  LodDist 
) const
inlinevirtual

This method implements the graphical output of this component.

Parameters
FirstPersonViewIf the world is rendered from the perspective of this component's entity.
LodDistThe distance of the viewer entity to this component's entity.
Returns
true if "something" was rendered, false otherwise (in this case the Map Editor may choose to render another visual representation of this component's entity).

Reimplemented in cf::GameSys::ComponentModelT.

void ComponentBaseT::Serialize ( cf::Network::OutStreamT Stream) const

Writes the current state of this component into the given stream.

This method is called to send the state of the component over the network, to save it to disk, or to store it in the clipboard. The implementation calls DoSerialize() that derived classes can override to add their own data.

Parameters
StreamThe stream to write the state data to.
template<class T >
void cf::GameSys::ComponentBaseT::SetMember ( const char *  Name,
const T &  Value 
)
inline

Sets the member variable with the given name to the given value.

The purpose of this method is to allow C++ code an easier access to the m_MemberVars, especially when initializing newly created components (e.g. in the Map Editor). (Alternatively, most derived classes had to provide some SetXY() methods on their own, and thereby clutter their own public interfaces with quasi unimportant methods, or the user code had to deal cumbersomely with the TypeSys::VarManT instance itself.)

void ComponentBaseT::UpdateDependencies ( EntityT Entity)
virtual

This method is called whenever something "external" to this component has changed:

  • if the parent entity has changed, because this component was added to or removed from it,
  • if other components in the parent entity have changed. The component can use the opportunity to search the entity for "sibling" components that it depends on, and store direct pointers to them. Note however that dependencies among components must not be cyclic, or else the deletion of an entity will leave a memory leak.
    Parameters
    EntityThe parent entity that contains this component, or NULL to indicate that this component is removed from the entity that it used to be a part of.

Reimplemented in cf::GameSys::ComponentPlayerPhysicsT, cf::GameSys::ComponentModelT, cf::GameSys::ComponentPhysicsT, cf::GameSys::ComponentCollisionModelT, and CompGameEntityT.

Member Data Documentation

const cf::TypeSys::MethsDocT ComponentBaseT::DocCallbacks
staticprotected
Initial value:
=
{
{ "OnInit",
"This method is called for each component of each entity as the last step of\n"
"initializing a newly loaded map.",
"", "" },
{ "OnClientFrame",
"This method is called for each component of each entity before the client renders the\n"
"next frame.",
"", "(number t)" },
{ NULL, NULL, NULL, NULL }
}
const char * ComponentBaseT::DocClass
staticprotected
Initial value:
=
"This is the base class for the components that a game entity is composed/aggregated of.\n"
"Components are the basic building blocks of an entity: their composition defines\n"
"the properties, the behaviour, and thus virtually every aspect of the entity."
const cf::TypeSys::MethsDocT ComponentBaseT::DocMethods
staticprotected
Initial value:
=
{
META_Get,
META_Set,
META_GetExtraMessage,
META_Interpolate,
META_GetEntity,
META_InitClientApprox,
META_toString,
{ NULL, NULL, NULL, NULL }
}
const luaL_Reg ComponentBaseT::MethodsList
staticprotected
Initial value:
=
{
{ "get", Get },
{ "set", Set },
{ "GetExtraMessage", GetExtraMessage },
{ "interpolate", Interpolate },
{ "GetEntity", GetEntity },
{ "InitClientApprox", InitClientApprox },
{ "__tostring", toString },
{ NULL, NULL }
}

The list of Lua methods for this class.


The documentation for this class was generated from the following files: