/Code/CryEngine/CryEntitySystem/FlowGraphProxy.cpp

https://gitlab.com/dahbearz/CRYENGINE · C++ · 224 lines · 164 code · 29 blank · 31 comment · 40 complexity · d987b22a2877fbfcf88a6b4627489d8f MD5 · raw file

  1. // Copyright 2001-2016 Crytek GmbH / Crytek Group. All rights reserved.
  2. // -------------------------------------------------------------------------
  3. // File name: FlowGraphProxy.h
  4. // Version: v1.00
  5. // Created: 6/6/2005 by Timur.
  6. // Compilers: Visual Studio.NET 2003
  7. // Description:
  8. // -------------------------------------------------------------------------
  9. // History:
  10. //
  11. ////////////////////////////////////////////////////////////////////////////
  12. #include "stdafx.h"
  13. #include "FlowGraphProxy.h"
  14. #include "Entity.h"
  15. #include <CryFlowGraph/IFlowSystem.h>
  16. #include <CryNetwork/ISerialize.h>
  17. //////////////////////////////////////////////////////////////////////////
  18. CFlowGraphProxy::CFlowGraphProxy()
  19. {
  20. m_pEntity = NULL;
  21. m_pFlowGraph = 0;
  22. }
  23. //////////////////////////////////////////////////////////////////////////
  24. CFlowGraphProxy::~CFlowGraphProxy()
  25. {
  26. }
  27. //////////////////////////////////////////////////////////////////////////
  28. void CFlowGraphProxy::Initialize(const SComponentInitializer& init)
  29. {
  30. m_pEntity = (CEntity*)init.m_pEntity;
  31. }
  32. //////////////////////////////////////////////////////////////////////////
  33. void CFlowGraphProxy::Done()
  34. {
  35. if (m_pFlowGraph)
  36. m_pFlowGraph->Release();
  37. m_pFlowGraph = 0;
  38. };
  39. //////////////////////////////////////////////////////////////////////////
  40. void CFlowGraphProxy::SetFlowGraph(IFlowGraph* pFlowGraph)
  41. {
  42. if (m_pFlowGraph)
  43. m_pFlowGraph->Release();
  44. m_pFlowGraph = pFlowGraph;
  45. if (m_pFlowGraph)
  46. m_pFlowGraph->AddRef();
  47. }
  48. //////////////////////////////////////////////////////////////////////////
  49. IFlowGraph* CFlowGraphProxy::GetFlowGraph()
  50. {
  51. return m_pFlowGraph;
  52. }
  53. //////////////////////////////////////////////////////////////////////////
  54. void CFlowGraphProxy::ProcessEvent(SEntityEvent& event)
  55. {
  56. // Assumes only 1 current listener can be deleted as a result of the event.
  57. Listeners::iterator next;
  58. Listeners::iterator it = m_listeners.begin();
  59. while (it != m_listeners.end())
  60. {
  61. next = it;
  62. ++next;
  63. (*it)->OnEntityEvent(m_pEntity, event);
  64. it = next;
  65. }
  66. // respond to entity activation/deactivation. enable/disable flowgraph
  67. switch (event.event)
  68. {
  69. case ENTITY_EVENT_INIT:
  70. case ENTITY_EVENT_DONE:
  71. {
  72. if (m_pFlowGraph)
  73. {
  74. const bool bActive = (event.event == ENTITY_EVENT_INIT) ? true : false;
  75. const bool bIsActive = m_pFlowGraph->IsActive();
  76. if (bActive != bIsActive)
  77. m_pFlowGraph->SetActive(bActive);
  78. }
  79. }
  80. break;
  81. case ENTITY_EVENT_POST_SERIALIZE:
  82. if (m_pFlowGraph)
  83. m_pFlowGraph->PostSerialize();
  84. break;
  85. }
  86. }
  87. //////////////////////////////////////////////////////////////////////////
  88. void CFlowGraphProxy::AddEventListener(IEntityEventListener* pListener)
  89. {
  90. // Does not check uniquiness due to performance reasons.
  91. m_listeners.push_back(pListener);
  92. }
  93. void CFlowGraphProxy::RemoveEventListener(IEntityEventListener* pListener)
  94. {
  95. stl::find_and_erase(m_listeners, pListener);
  96. }
  97. void CFlowGraphProxy::Update(SEntityUpdateContext& ctx)
  98. {
  99. // if (m_pFlowGraph)
  100. // m_pFlowGraph->Update();
  101. }
  102. void CFlowGraphProxy::Reload(IEntity* pEntity, SEntitySpawnParams& params)
  103. {
  104. m_pEntity = (CEntity*)pEntity;
  105. SAFE_RELEASE(m_pFlowGraph);
  106. m_listeners.clear();
  107. }
  108. void CFlowGraphProxy::SerializeXML(XmlNodeRef& entityNode, bool bLoading)
  109. {
  110. // don't serialize flowgraphs from the editor
  111. // (editor needs more information, so it saves them specially)
  112. if (gEnv->IsEditor() && !bLoading)
  113. return;
  114. XmlNodeRef flowGraphNode;
  115. if (bLoading)
  116. {
  117. flowGraphNode = entityNode->findChild("FlowGraph");
  118. if (!flowGraphNode)
  119. return;
  120. // prevents loading of disabled flowgraphs for optimization
  121. // only in game mode, of course
  122. #if 1
  123. bool bEnabled = true;
  124. if (flowGraphNode->getAttr("enabled", bEnabled) && bEnabled == false)
  125. {
  126. EntityWarning("[FlowGraphProxy] Skipped loading of FG for Entity %d '%s'. FG was disabled on export.", m_pEntity->GetId(), m_pEntity->GetName());
  127. return;
  128. }
  129. #endif
  130. enum EMultiPlayerType
  131. {
  132. eMPT_ServerOnly = 0,
  133. eMPT_ClientOnly,
  134. eMPT_ClientServer
  135. };
  136. EMultiPlayerType mpType = eMPT_ServerOnly;
  137. const char* mpTypeAttr = flowGraphNode->getAttr("MultiPlayer");
  138. if (mpTypeAttr)
  139. {
  140. if (strcmp("ClientOnly", mpTypeAttr) == 0)
  141. mpType = eMPT_ClientOnly;
  142. else if (strcmp("ClientServer", mpTypeAttr) == 0)
  143. mpType = eMPT_ClientServer;
  144. }
  145. const bool bIsServer = gEnv->bServer;
  146. const bool bIsClient = gEnv->IsClient();
  147. if (mpType == eMPT_ServerOnly && !bIsServer)
  148. return;
  149. else if (mpType == eMPT_ClientOnly && !bIsClient)
  150. return;
  151. if (gEnv->pFlowSystem)
  152. {
  153. SetFlowGraph(gEnv->pFlowSystem->CreateFlowGraph());
  154. }
  155. }
  156. else
  157. {
  158. flowGraphNode = entityNode->createNode("FlowGraph");
  159. entityNode->addChild(flowGraphNode);
  160. }
  161. assert(!!flowGraphNode);
  162. if (m_pFlowGraph)
  163. {
  164. m_pFlowGraph->SetGraphEntity(m_pEntity->GetId());
  165. m_pFlowGraph->SerializeXML(flowGraphNode, bLoading);
  166. }
  167. }
  168. //////////////////////////////////////////////////////////////////////////
  169. bool CFlowGraphProxy::NeedSerialize()
  170. {
  171. return m_pFlowGraph != 0;
  172. };
  173. //////////////////////////////////////////////////////////////////////////
  174. bool CFlowGraphProxy::GetSignature(TSerialize signature)
  175. {
  176. signature.BeginGroup("FlowGraphProxy");
  177. signature.EndGroup();
  178. return true;
  179. }
  180. //////////////////////////////////////////////////////////////////////////
  181. void CFlowGraphProxy::Serialize(TSerialize ser)
  182. {
  183. bool hasFlowGraph = m_pFlowGraph != 0;
  184. if (ser.BeginOptionalGroup("FlowGraph", hasFlowGraph))
  185. {
  186. if (ser.IsReading() && m_pFlowGraph == 0)
  187. {
  188. EntityWarning("Unable to reload flowgraph here");
  189. return;
  190. }
  191. if (m_pFlowGraph)
  192. m_pFlowGraph->Serialize(ser);
  193. ser.EndGroup();
  194. }
  195. }