1 #ifndef ACTOR_H_
2 #define ACTOR_H_
3 
4 #include <string>
5 #include <stdint.h>
6 #include <memory>
7 #include <map>
8 
9 #include "../SharedDefines.h"
10 
11 #include "ActorComponent.h"
12 
13 typedef std::map<uint32, StrongActorComponentPtr> ActorComponentsMap;
14 
15 class PositionComponent;
16 class TiXmlElement;
17 class Actor
18 {
19 public:
20     Actor(uint32_t actorGuid);
21     ~Actor();
22 
23     bool Init(TiXmlElement* data);
24     void PostInit();
25     void PostPostInit();
26     void Destroy();
27     void Update(uint32_t msDiff);
28 
29     std::string ToXML();
30 
GetGUID()31     inline uint32_t GetGUID() const { return _GUID; }
GetName()32     inline std::string GetName() const { return _name; }
33 
34     // Retrieves component from given ID or NULL if component not found
35     template <class ComponentType>
GetComponent(uint32 id)36     weak_ptr<ComponentType> GetComponent(uint32 id)
37     {
38         ActorComponentsMap::iterator findIter = _components.find(id);
39         if (findIter != _components.end())
40         {
41             StrongActorComponentPtr base(findIter->second);
42             shared_ptr<ComponentType> sub(std::static_pointer_cast<ComponentType>(base));  // cast to subclass version of the pointer
43             weak_ptr<ComponentType> weakSub(sub);  // convert strong pointer to weak pointer
44             return weakSub;  // return the weak pointer
45         }
46 
47         return weak_ptr<ComponentType>();
48     }
49 
50     // Retrieves component from given name or NULL if component not found
51     template <class ComponentType>
GetComponent(const char * name)52     weak_ptr<ComponentType> GetComponent(const char *name)
53     {
54         uint32 id = ActorComponent::GetIdFromName(name);
55         ActorComponentsMap::iterator findIter = _components.find(id);
56         if (findIter != _components.end())
57         {
58             StrongActorComponentPtr base(findIter->second);
59             shared_ptr<ComponentType> sub(static_pointer_cast<ComponentType>(base));  // cast to subclass version of the pointer
60             weak_ptr<ComponentType> weakSub(sub);  // convert strong pointer to weak pointer
61             return weakSub;  // return the weak pointer
62         }
63         else
64         {
65             return weak_ptr<ComponentType>();
66         }
67     }
68 
69     template <class ComponentType>
GetComponent()70     weak_ptr<ComponentType> GetComponent()
71     {
72         uint32 id = ActorComponent::GetIdFromName(ComponentType::g_Name);
73         ActorComponentsMap::iterator findIter = _components.find(id);
74         if (findIter != _components.end())
75         {
76             StrongActorComponentPtr base(findIter->second);
77             shared_ptr<ComponentType> sub(static_pointer_cast<ComponentType>(base));  // cast to subclass version of the pointer
78             weak_ptr<ComponentType> weakSub(sub);  // convert strong pointer to weak pointer
79             return weakSub;  // return the weak pointer
80         }
81         else
82         {
83             return weak_ptr<ComponentType>();
84         }
85     }
86 
87     template <class ComponentType>
88     ComponentType* GetRawComponent(bool bAssertNotNull = false)
89     {
90         uint32 id = ActorComponent::GetIdFromName(ComponentType::g_Name);
91         ActorComponentsMap::iterator findIter = _components.find(id);
92         if (findIter != _components.end())
93         {
94             StrongActorComponentPtr base(findIter->second);
95             shared_ptr<ComponentType> sub(static_pointer_cast<ComponentType>(base));  // cast to subclass version of the pointer
96             return sub.get();
97         }
98 
99         if (bAssertNotNull)
100         {
101             LOG_ASSERT("Failed to get component from actor: " + GetName());
102         }
103 
104         return NULL;
105     }
106 
GetComponents()107     const ActorComponentsMap* GetComponents() { return &_components; }
108 
109     void AddComponent(StrongActorComponentPtr pComponent);
110 
111     void OnWorldFinishedLoading();
112 
113     //=========================================================================
114     // Some components are accessed REALLY often so it makes sense to just
115     // put them here and dont use the templated GetComponent method
116     //=========================================================================
117 
GetPositionComponent()118     inline shared_ptr<PositionComponent> GetPositionComponent() { return m_pPositionComponent; }
119 
120 private:
121     friend class ActorFactory;
122 
123     uint32_t _GUID;
124     std::string _name;
125 
126     ActorComponentsMap _components;
127 
128     // Resource from which this actor was loaded
129     std::string _resource;
130 
131     shared_ptr<PositionComponent> m_pPositionComponent;
132 };
133 
134 #endif