Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- //
- // GameObject.h
- //
- // Is an object tied 1:1 with entity that holds components for it. There can be single instance of
- // each component held on single game object.
- //
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- #ifndef __GAME_OBJECT__H__
- #define __GAME_OBJECT__H__
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- // Header section
- #include "ComponentTypeId.h"
- #include "ComponentTypeList.h"
- #include <map>
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- // Class & Structures definition
- namespace Engine
- {
- class Entity;
- class Scene;
- /// <summary>
- /// Game object class ties entity to components.
- /// </summary>
- class GameObject
- {
- private:
- /// <summary>Associative container holding components with component type as index</summary>
- std::map<ComponentId, Component*> mComponents;
- /// <summary>Pointer to entity to which this game object is tied</summary>
- Entity* mEntity;
- public:
- /// <summary>
- /// Add component of specific type to this game object
- /// </summary>
- /// <param name="compIdx">Component type ID</param>
- /// <param name="component">Pointer to component</param>
- /// <returns>Pointer to this game object class instance</returns>
- GameObject* AddComponent(const ComponentId compIdx, Component* component)
- {
- mComponents[compIdx] = component;
- mComponents[compIdx]->mGameObject = this;
- ComponentTypeList::AddComponent(compIdx, component);
- return this;
- }
- /// <summary>
- /// Remove component of specific type from this game object
- /// </summary>
- /// <param name="compIdx">Component type ID</param>
- /// <returns>Pointer to this game object class instance</returns>
- GameObject* RemoveComponent(const ComponentId compIdx)
- {
- ComponentTypeList::RemoveComponent(compIdx, mComponents[compIdx]);
- delete mComponents[compIdx];
- mComponents.erase(compIdx);
- return this;
- }
- /// <summary>
- /// Get component of specific type from this game object
- /// </summary>
- /// <param name="compIdx">Component type ID</param>
- /// <returns>Pointer to component (throws exception when not found)</returns>
- Component* GetComponent(const ComponentId compIdx) const
- {
- return mComponents.at(compIdx);
- }
- /// <summary>
- /// Does game object contain component of specific type?
- /// </summary>
- /// <param name="compIdx">Component type ID</param>
- /// <returns>True when contains, false otherwise</returns>
- bool HasComponent(const ComponentId compIdx) const
- {
- return (mComponents.find(compIdx) != mComponents.end());
- }
- protected:
- /// <summary>
- /// Helper template function to pass in arguments to component creation function
- /// </summary>
- /// <typeparam name="T">Component type</typeparam>
- /// <typeparam name="...TArgs">Argument types for component creation</typeparam>
- /// <param name="...args">Arguments for component creation</param>
- /// <returns>Pointer to newly created component</returns>
- template <typename T, typename... TArgs> inline Component* CreateComponent(TArgs&&... args);
- public:
- /// <summary>
- /// Default destructor. Release all components, then clear components container.
- /// </summary>
- ~GameObject()
- {
- while (mComponents.size() > 0)
- {
- RemoveComponent(mComponents.begin()->first);
- }
- mComponents.clear();
- }
- /// <summary>
- /// Set game object's entity
- /// </summary>
- /// <param name="ent">Pointer to entity to set</param>
- void SetEntity(Entity* ent)
- {
- mEntity = ent;
- }
- /// <summary>
- /// Get game object's entity
- /// </summary>
- /// <returns>Pointer to game object's entity</returns>
- Entity* GetEntity()
- {
- return mEntity;
- }
- /// <summary>
- /// Add component of specific type to this game object, templated variant. Creates a new component.
- /// </summary>
- /// <typeparam name="T">Component type</typeparam>
- /// <typeparam name="...TArgs">Argument types for component creation</typeparam>
- /// <param name="...args">Arguments for component creation</param>
- /// <returns>Pointer to this game object</returns>
- template <typename T, typename... TArgs> inline GameObject* Add(TArgs&... args);
- /// <summary>
- /// Remove component of specific type from this game object, templated variant
- /// </summary>
- /// <typeparam name="T">Component type</typeparam>
- /// <returns>Pointer to this game object</returns>
- template <typename T> inline GameObject* Remove();
- /// <summary>
- /// Get component of specific type from this game object. Templated variant.
- /// </summary>
- /// <typeparam name="T">Component type</typeparam>
- /// <returns>Pointer to component (throws exception when not found)</returns>
- template <typename T> inline T* Get() const;
- /// <summary>
- /// Does game object contain component of specific type? Templated variant.
- /// </summary>
- /// <typeparam name="T">Component type</typeparam>
- /// <returns>True when contains, false otherwise</returns>
- template <typename T> inline bool Has() const;
- /// <summary>
- /// Get number of components on this game object
- /// </summary>
- /// <returns>Number of components on this game object</returns>
- size_t GetCount() { return mComponents.size(); }
- /// <summary>
- /// Get begin iterator for components (ordered by component type index)
- /// </summary>
- /// <returns>Begin iterator for components container</returns>
- std::map<ComponentId, Component*>::iterator ComponentsItBegin() { return mComponents.begin(); }
- /// <summary>
- /// Get end iterator for components (ordered by component type index)
- /// </summary>
- /// <returns>End iterator for components container</returns>
- std::map<ComponentId, Component*>::iterator ComponentsItEnd() { return mComponents.end(); }
- /// <summary>
- /// Update call for game object, update all components
- /// </summary>
- /// <param name="deltaTime">Delta time for step</param>
- void Update(float deltaTime)
- {
- for (std::map<ComponentId, Component*>::iterator it = mComponents.begin(); it != mComponents.end(); it++)
- {
- it->second->Update(deltaTime);
- }
- }
- ALIGNED_NEW_DELETE("Engine::GameObject")
- };
- /// <summary>
- /// Helper template function to pass in arguments to component creation function
- /// </summary>
- /// <typeparam name="T">Component type</typeparam>
- /// <typeparam name="...TArgs">Argument types for component creation</typeparam>
- /// <param name="...args">Arguments for component creation</param>
- /// <returns>Pointer to newly created component</returns>
- template <typename T, typename... TArgs>
- Component* GameObject::CreateComponent(TArgs&&... args)
- {
- T* component = new T(std::forward<TArgs>(args)...);
- return component;
- }
- /// <summary>
- /// Add component of specific type to this game object, templated variant. Creates a new component.
- /// </summary>
- /// <typeparam name="T">Component type</typeparam>
- /// <typeparam name="...TArgs">Argument types for component creation</typeparam>
- /// <param name="...args">Arguments for component creation</param>
- /// <returns>Pointer to this game object</returns>
- template <typename T, typename... TArgs> GameObject* GameObject::Add(TArgs&... args)
- {
- return AddComponent(ComponentTypeId::Get<T>(), CreateComponent<T>(std::forward<TArgs>(args)...));
- }
- /// <summary>
- /// Remove component of specific type from this game object, templated variant
- /// </summary>
- /// <typeparam name="T">Component type</typeparam>
- /// <returns>Pointer to this game object</returns>
- template <typename T> GameObject* GameObject::Remove()
- {
- return RemoveComponent(ComponentTypeId::Get<T>());
- }
- /// <summary>
- /// Get component of specific type from this game object. Templated variant.
- /// </summary>
- /// <typeparam name="T">Component type</typeparam>
- /// <returns>Pointer to component (throws exception when not found)</returns>
- template <typename T> T* GameObject::Get() const
- {
- return static_cast<T*>(GetComponent(ComponentTypeId::Get<T>()));
- }
- /// <summary>
- /// Does game object contain component of specific type? Templated variant.
- /// </summary>
- /// <typeparam name="T">Component type</typeparam>
- /// <returns>True when contains, false otherwise</returns>
- template <typename T> bool GameObject::Has() const
- {
- return HasComponent(ComponentTypeId::Get<T>());
- }
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- // EOH
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement