Advertisement
Zgragselus

Gameobject

Sep 24th, 2023
914
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.64 KB | None | 0 0
  1. ///////////////////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // GameObject.h
  4. //
  5. // Is an object tied 1:1 with entity that holds components for it. There can be single instance of
  6. // each component held on single game object.
  7. //
  8. ///////////////////////////////////////////////////////////////////////////////////////////////////
  9.  
  10. #ifndef __GAME_OBJECT__H__
  11. #define __GAME_OBJECT__H__
  12.  
  13. ///////////////////////////////////////////////////////////////////////////////////////////////////
  14. // Header section
  15.  
  16. #include "ComponentTypeId.h"
  17. #include "ComponentTypeList.h"
  18. #include <map>
  19.  
  20. ///////////////////////////////////////////////////////////////////////////////////////////////////
  21. // Class & Structures definition
  22.  
  23. namespace Engine
  24. {
  25.     class Entity;
  26.  
  27.     class Scene;
  28.  
  29.     /// <summary>
  30.     /// Game object class ties entity to components.
  31.     /// </summary>
  32.     class GameObject
  33.     {
  34.     private:
  35.         /// <summary>Associative container holding components with component type as index</summary>
  36.         std::map<ComponentId, Component*> mComponents;
  37.  
  38.         /// <summary>Pointer to entity to which this game object is tied</summary>
  39.         Entity* mEntity;
  40.  
  41.     public:
  42.         /// <summary>
  43.         /// Add component of specific type to this game object
  44.         /// </summary>
  45.         /// <param name="compIdx">Component type ID</param>
  46.         /// <param name="component">Pointer to component</param>
  47.         /// <returns>Pointer to this game object class instance</returns>
  48.         GameObject* AddComponent(const ComponentId compIdx, Component* component)
  49.         {
  50.             mComponents[compIdx] = component;
  51.             mComponents[compIdx]->mGameObject = this;
  52.             ComponentTypeList::AddComponent(compIdx, component);
  53.             return this;
  54.         }
  55.  
  56.         /// <summary>
  57.         /// Remove component of specific type from this game object
  58.         /// </summary>
  59.         /// <param name="compIdx">Component type ID</param>
  60.         /// <returns>Pointer to this game object class instance</returns>
  61.         GameObject* RemoveComponent(const ComponentId compIdx)
  62.         {
  63.             ComponentTypeList::RemoveComponent(compIdx, mComponents[compIdx]);
  64.             delete mComponents[compIdx];
  65.             mComponents.erase(compIdx);
  66.             return this;
  67.         }
  68.  
  69.         /// <summary>
  70.         /// Get component of specific type from this game object
  71.         /// </summary>
  72.         /// <param name="compIdx">Component type ID</param>
  73.         /// <returns>Pointer to component (throws exception when not found)</returns>
  74.         Component* GetComponent(const ComponentId compIdx) const
  75.         {
  76.             return mComponents.at(compIdx);
  77.         }
  78.  
  79.         /// <summary>
  80.         /// Does game object contain component of specific type?
  81.         /// </summary>
  82.         /// <param name="compIdx">Component type ID</param>
  83.         /// <returns>True when contains, false otherwise</returns>
  84.         bool HasComponent(const ComponentId compIdx) const
  85.         {
  86.             return (mComponents.find(compIdx) != mComponents.end());
  87.         }
  88.  
  89.     protected:
  90.         /// <summary>
  91.         /// Helper template function to pass in arguments to component creation function
  92.         /// </summary>
  93.         /// <typeparam name="T">Component type</typeparam>
  94.         /// <typeparam name="...TArgs">Argument types for component creation</typeparam>
  95.         /// <param name="...args">Arguments for component creation</param>
  96.         /// <returns>Pointer to newly created component</returns>
  97.         template <typename T, typename... TArgs> inline Component* CreateComponent(TArgs&&... args);
  98.  
  99.     public:
  100.         /// <summary>
  101.         /// Default destructor. Release all components, then clear components container.
  102.         /// </summary>
  103.         ~GameObject()
  104.         {
  105.             while (mComponents.size() > 0)
  106.             {
  107.                 RemoveComponent(mComponents.begin()->first);
  108.             }
  109.  
  110.             mComponents.clear();
  111.         }
  112.  
  113.         /// <summary>
  114.         /// Set game object's entity
  115.         /// </summary>
  116.         /// <param name="ent">Pointer to entity to set</param>
  117.         void SetEntity(Entity* ent)
  118.         {
  119.             mEntity = ent;
  120.         }
  121.  
  122.         /// <summary>
  123.         /// Get game object's entity
  124.         /// </summary>
  125.         /// <returns>Pointer to game object's entity</returns>
  126.         Entity* GetEntity()
  127.         {
  128.             return mEntity;
  129.         }
  130.  
  131.         /// <summary>
  132.         /// Add component of specific type to this game object, templated variant. Creates a new component.
  133.         /// </summary>
  134.         /// <typeparam name="T">Component type</typeparam>
  135.         /// <typeparam name="...TArgs">Argument types for component creation</typeparam>
  136.         /// <param name="...args">Arguments for component creation</param>
  137.         /// <returns>Pointer to this game object</returns>
  138.         template <typename T, typename... TArgs> inline GameObject* Add(TArgs&... args);
  139.  
  140.         /// <summary>
  141.         /// Remove component of specific type from this game object, templated variant
  142.         /// </summary>
  143.         /// <typeparam name="T">Component type</typeparam>
  144.         /// <returns>Pointer to this game object</returns>
  145.         template <typename T> inline GameObject* Remove();
  146.  
  147.         /// <summary>
  148.         /// Get component of specific type from this game object. Templated variant.
  149.         /// </summary>
  150.         /// <typeparam name="T">Component type</typeparam>
  151.         /// <returns>Pointer to component (throws exception when not found)</returns>
  152.         template <typename T> inline T* Get() const;
  153.  
  154.         /// <summary>
  155.         /// Does game object contain component of specific type? Templated variant.
  156.         /// </summary>
  157.         /// <typeparam name="T">Component type</typeparam>
  158.         /// <returns>True when contains, false otherwise</returns>
  159.         template <typename T> inline bool Has() const;
  160.  
  161.         /// <summary>
  162.         /// Get number of components on this game object
  163.         /// </summary>
  164.         /// <returns>Number of components on this game object</returns>
  165.         size_t GetCount() { return mComponents.size(); }
  166.  
  167.         /// <summary>
  168.         /// Get begin iterator for components (ordered by component type index)
  169.         /// </summary>
  170.         /// <returns>Begin iterator for components container</returns>
  171.         std::map<ComponentId, Component*>::iterator ComponentsItBegin() { return mComponents.begin(); }
  172.  
  173.         /// <summary>
  174.         /// Get end iterator for components (ordered by component type index)
  175.         /// </summary>
  176.         /// <returns>End iterator for components container</returns>
  177.         std::map<ComponentId, Component*>::iterator ComponentsItEnd() { return mComponents.end(); }
  178.  
  179.         /// <summary>
  180.         /// Update call for game object, update all components
  181.         /// </summary>
  182.         /// <param name="deltaTime">Delta time for step</param>
  183.         void Update(float deltaTime)
  184.         {
  185.             for (std::map<ComponentId, Component*>::iterator it = mComponents.begin(); it != mComponents.end(); it++)
  186.             {
  187.                 it->second->Update(deltaTime);
  188.             }
  189.         }
  190.        
  191.         ALIGNED_NEW_DELETE("Engine::GameObject")
  192.     };
  193.  
  194.     /// <summary>
  195.     /// Helper template function to pass in arguments to component creation function
  196.     /// </summary>
  197.     /// <typeparam name="T">Component type</typeparam>
  198.     /// <typeparam name="...TArgs">Argument types for component creation</typeparam>
  199.     /// <param name="...args">Arguments for component creation</param>
  200.     /// <returns>Pointer to newly created component</returns>
  201.     template <typename T, typename... TArgs>
  202.     Component* GameObject::CreateComponent(TArgs&&... args)
  203.     {
  204.         T* component = new T(std::forward<TArgs>(args)...);
  205.         return component;
  206.     }
  207.  
  208.     /// <summary>
  209.     /// Add component of specific type to this game object, templated variant. Creates a new component.
  210.     /// </summary>
  211.     /// <typeparam name="T">Component type</typeparam>
  212.     /// <typeparam name="...TArgs">Argument types for component creation</typeparam>
  213.     /// <param name="...args">Arguments for component creation</param>
  214.     /// <returns>Pointer to this game object</returns>
  215.     template <typename T, typename... TArgs> GameObject* GameObject::Add(TArgs&... args)
  216.     {
  217.         return AddComponent(ComponentTypeId::Get<T>(), CreateComponent<T>(std::forward<TArgs>(args)...));
  218.     }
  219.  
  220.     /// <summary>
  221.     /// Remove component of specific type from this game object, templated variant
  222.     /// </summary>
  223.     /// <typeparam name="T">Component type</typeparam>
  224.     /// <returns>Pointer to this game object</returns>
  225.     template <typename T> GameObject* GameObject::Remove()
  226.     {
  227.         return RemoveComponent(ComponentTypeId::Get<T>());
  228.     }
  229.  
  230.     /// <summary>
  231.     /// Get component of specific type from this game object. Templated variant.
  232.     /// </summary>
  233.     /// <typeparam name="T">Component type</typeparam>
  234.     /// <returns>Pointer to component (throws exception when not found)</returns>
  235.     template <typename T> T* GameObject::Get() const
  236.     {
  237.         return static_cast<T*>(GetComponent(ComponentTypeId::Get<T>()));
  238.     }
  239.  
  240.     /// <summary>
  241.     /// Does game object contain component of specific type? Templated variant.
  242.     /// </summary>
  243.     /// <typeparam name="T">Component type</typeparam>
  244.     /// <returns>True when contains, false otherwise</returns>
  245.     template <typename T> bool GameObject::Has() const
  246.     {
  247.         return HasComponent(ComponentTypeId::Get<T>());
  248.     }
  249. }
  250.  
  251. ///////////////////////////////////////////////////////////////////////////////////////////////////
  252. // EOH
  253.  
  254. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement