1 #include "FollowableComponent.h"
2 #include "PositionComponent.h"
3 #include "RenderComponent.h"
4 #include "../ActorTemplates.h"
5 #include "../Actor.h"
6 
7 #include "../../Events/EventMgr.h"
8 #include "../../Events/Events.h"
9 
10 //=====================================================================================================================
11 //
12 // FollowableComponent Implementation
13 //
14 //=====================================================================================================================
15 
16 const char* FollowableComponent::g_Name = "FollowableComponent";
17 
FollowableComponent()18 FollowableComponent::FollowableComponent() :
19     m_pPositionComponent(NULL),
20     m_pTargetPositionComponent(NULL),
21     m_pTargetRenderComponent(NULL),
22     m_MsDuration(0),
23     m_CurrentMsDuration(0)
24 { }
25 
~FollowableComponent()26 FollowableComponent::~FollowableComponent()
27 {
28     shared_ptr<EventData_Destroy_Actor> pEvent(new EventData_Destroy_Actor(m_pFollowingActor->GetGUID()));
29     IEventMgr::Get()->VQueueEvent(pEvent);
30 }
31 
VInit(TiXmlElement * pData)32 bool FollowableComponent::VInit(TiXmlElement* pData)
33 {
34     assert(pData);
35 
36     if (TiXmlElement* pElem = pData->FirstChildElement("Offset"))
37     {
38         pElem->Attribute("x", &m_Offset.x);
39         pElem->Attribute("y", &m_Offset.y);
40     }
41 
42     SetStringIfDefined(&m_ImageSet, pData->FirstChildElement("ImageSet"));
43     SetStringIfDefined(&m_AnimationPath, pData->FirstChildElement("AnimationPath"));
44 
45     return true;
46 }
47 
VPostInit()48 void FollowableComponent::VPostInit()
49 {
50     m_pPositionComponent =
51         MakeStrongPtr(m_pOwner->GetComponent<PositionComponent>(PositionComponent::g_Name)).get();
52 
53     Point ownerPos = m_pPositionComponent->GetPosition();
54     m_pFollowingActor = ActorTemplates::CreateRenderedActor(
55         Point(ownerPos.x + m_Offset.x, ownerPos.y + m_Offset.y), m_ImageSet, m_AnimationPath, 1020).get();
56     assert(m_pFollowingActor != NULL);
57 
58     m_pTargetPositionComponent =
59         MakeStrongPtr(m_pFollowingActor->GetComponent<PositionComponent>(PositionComponent::g_Name)).get();
60     m_pTargetRenderComponent =
61         MakeStrongPtr(m_pFollowingActor->GetComponent<ActorRenderComponent>(ActorRenderComponent::g_Name)).get();
62 
63     assert(m_pPositionComponent != NULL);
64     assert(m_pTargetPositionComponent != NULL);
65     assert(m_pTargetRenderComponent != NULL);
66 
67     m_pTargetRenderComponent->SetVisible(false);
68 }
69 
VGenerateXml()70 TiXmlElement* FollowableComponent::VGenerateXml()
71 {
72     // TODO: Implement
73     return NULL;
74 }
75 
VUpdate(uint32 msDiff)76 void FollowableComponent::VUpdate(uint32 msDiff)
77 {
78     if (m_CurrentMsDuration < m_MsDuration)
79     {
80         m_CurrentMsDuration += msDiff;
81 
82         Point ownerPos = m_pPositionComponent->GetPosition();
83         m_pTargetPositionComponent->SetPosition(ownerPos.x + m_Offset.x, ownerPos.y + m_Offset.y);
84 
85         shared_ptr<EventData_Move_Actor> pEvent(
86             new EventData_Move_Actor(m_pFollowingActor->GetGUID(), m_pTargetPositionComponent->GetPosition()));
87         IEventMgr::Get()->VTriggerEvent(pEvent);
88     }
89     else if (m_MsDuration > 0)
90     {
91         Deactivate();
92     }
93 }
94 
Activate(int msDuration)95 void FollowableComponent::Activate(int msDuration)
96 {
97     if (msDuration > 0)
98     {
99         m_pTargetRenderComponent->SetVisible(true);
100 
101         Point ownerPos = m_pPositionComponent->GetPosition();
102         m_pTargetPositionComponent->SetPosition(ownerPos.x + m_Offset.x, ownerPos.y + m_Offset.y);
103 
104         m_MsDuration = msDuration;
105         m_CurrentMsDuration = 0;
106     }
107 }
108 
Deactivate()109 void FollowableComponent::Deactivate()
110 {
111     m_MsDuration = -1;
112     m_CurrentMsDuration = 0;
113     m_pTargetRenderComponent->SetVisible(false);
114 }