BVB Source Codes

CRYENGINE Show CustomAction.cpp Source code

Return Download CRYENGINE: download CustomAction.cpp Source code - Download CRYENGINE Source code - Type:.cpp
  1. // Copyright 2001-2016 Crytek GmbH / Crytek Group. All rights reserved.
  2.  
  3. #include "StdAfx.h"
  4.  
  5. #include "CustomAction.h"
  6. #include "CustomActionManager.h"
  7. #include "FlowSystem/Nodes/FlowCustomActionNodes.h"
  8.  
  9. #include <CryEntitySystem/IEntity.h>
  10.  
  11. ///////////////////////////////////////////////////
  12. // CCustomAction references a Flow Graph - sequence of elementary actions
  13. ///////////////////////////////////////////////////
  14.  
  15. //------------------------------------------------------------------------------------------------------------------------
  16. bool CCustomAction::StartAction()
  17. {
  18.         return SwitchState(CAS_Started, CAE_Started, "CustomAction:Start", "OnCustomActionStart");
  19. }
  20.  
  21. //------------------------------------------------------------------------------------------------------------------------
  22. bool CCustomAction::SucceedAction()
  23. {
  24.         return SwitchState(CAS_Succeeded, CAE_Succeeded, "CustomAction:Succeed", "OnCustomActionSucceed");
  25. }
  26.  
  27. //------------------------------------------------------------------------------------------------------------------------
  28. bool CCustomAction::SucceedWaitAction()
  29. {
  30.         return SwitchState(CAS_SucceededWait, CAE_SucceededWait, "CustomAction:SucceedWait", "OnCustomActionSucceedWait");
  31. }
  32.  
  33. //------------------------------------------------------------------------------------------------------------------------
  34. bool CCustomAction::SucceedWaitCompleteAction()
  35. {
  36.         return SwitchState(CAS_SucceededWaitComplete, CAE_SucceededWaitComplete, "CustomAction:SucceedWaitComplete", "OnCustomActionSucceedWaitComplete");
  37. }
  38.  
  39. //------------------------------------------------------------------------------------------------------------------------
  40. bool CCustomAction::EndActionSuccess()
  41. {
  42.         bool bSwitched = SwitchState(CAS_Ended, CAE_EndedSuccess, NULL, "OnCustomActionEndSuccess"); // NULL since not firing up another custom action node
  43.         if (bSwitched)
  44.         {
  45.                 m_listeners.Clear();
  46.  
  47.         }
  48.  
  49.         return bSwitched;
  50. }
  51.  
  52. //------------------------------------------------------------------------------------------------------------------------
  53. bool CCustomAction::EndActionFailure()
  54. {
  55.         bool bSwitched = SwitchState(CAS_Ended, CAE_EndedFailure, NULL, "OnCustomActionEndFailure"); // NULL since not firing up another custom action node
  56.         if (bSwitched)
  57.         {
  58.                 m_listeners.Clear();
  59.         }
  60.  
  61.         return bSwitched;
  62. }
  63.  
  64. //------------------------------------------------------------------------------------------------------------------------
  65. void CCustomAction::TerminateAction()
  66. {
  67.         m_currentState = CAS_Ended;
  68.  
  69.         IFlowGraph* pFlowGraph = GetFlowGraph();
  70.         if (pFlowGraph)
  71.         {
  72.                 pFlowGraph->SetCustomAction(0);
  73.         }
  74.  
  75.         m_pObjectEntity = NULL;
  76.  
  77.         this->NotifyListeners(CAE_Terminated, *this);
  78.  
  79.         m_listeners.Clear();
  80. }
  81.  
  82. //------------------------------------------------------------------------------------------------------------------------
  83. bool CCustomAction::AbortAction()
  84. {
  85.         if (m_currentState == CAS_Started) // Can only abort when starting, not in succeeded state
  86.         {
  87.                 return SwitchState(CAS_Aborted, CAE_Aborted, "CustomAction:Abort", "OnCustomActionAbort");
  88.         }
  89.  
  90.         return false;
  91. }
  92.  
  93. //------------------------------------------------------------------------------------------------------------------------
  94. void CCustomAction::Serialize(TSerialize ser)
  95. {
  96.         ser.BeginGroup("ActiveAction");
  97.         {
  98.                 ser.Value("m_customActionGraphName", m_customActionGraphName);
  99.  
  100.                 EntityId objectId = m_pObjectEntity && !ser.IsReading() ? m_pObjectEntity->GetId() : 0;
  101.                 ser.Value("objectId", objectId);
  102.                 if (ser.IsReading())
  103.                         m_pObjectEntity = gEnv->pEntitySystem->GetEntity(objectId);
  104.  
  105.                 if (ser.IsReading())
  106.                 {
  107.                         int currentState = CAS_Ended;
  108.                         ser.Value("m_currentState", currentState);
  109.  
  110.                         // Due to numerous flownodes not being serialized, don't allow custom actions to be in an intermediate state which
  111.                         // will never be set again
  112.                         if (currentState != CAS_Succeeded && currentState != CAS_SucceededWait)
  113.                         {
  114.                                 currentState = CAS_Ended;
  115.                         }
  116.  
  117.                         m_currentState = (ECustomActionState)currentState;
  118.                 }
  119.                 else
  120.                 {
  121.                         int currentState = (int)m_currentState;
  122.                         ser.Value("m_currentState", currentState);
  123.                 }
  124.  
  125.                 if (ser.BeginOptionalGroup("m_pFlowGraph", m_pFlowGraph != NULL))
  126.                 {
  127.                         if (ser.IsReading())
  128.                         {
  129.                                 ICustomAction* pAction = gEnv->pGameFramework->GetICustomActionManager()->GetCustomActionFromLibrary(m_customActionGraphName);
  130.                                 CRY_ASSERT(pAction);
  131.                                 if (pAction)
  132.                                         m_pFlowGraph = pAction->GetFlowGraph()->Clone();
  133.                                 if (m_pFlowGraph)
  134.                                         m_pFlowGraph->SetCustomAction(this);
  135.                         }
  136.                         if (m_pFlowGraph)
  137.                         {
  138.                                 m_pFlowGraph->SetGraphEntity(objectId, 0);
  139.                                 m_pFlowGraph->Serialize(ser);
  140.                         }
  141.                         ser.EndGroup(); //m_pFlowGraph
  142.                 }
  143.         }
  144.         ser.EndGroup();
  145. }
  146.  
  147. //------------------------------------------------------------------------------------------------------------------------
  148. bool CCustomAction::SwitchState(const ECustomActionState newState,
  149.                                 const ECustomActionEvent event,
  150.                                 const char* szNodeToCall,
  151.                                 const char* szLuaFuncToCall)
  152. {
  153.         if (m_currentState == newState)
  154.                 return false;
  155.  
  156.         CRY_ASSERT(m_pObjectEntity != NULL);
  157.  
  158.         m_currentState = newState;
  159.         this->NotifyListeners(event, *this);
  160.  
  161.         // Notify entity
  162.         if (szLuaFuncToCall && szLuaFuncToCall[0] != '\0')
  163.         {
  164.                 if (IScriptTable* entityScriptTable = m_pObjectEntity->GetScriptTable())
  165.                 {
  166.                         HSCRIPTFUNCTION functionToCall = 0;
  167.  
  168.                         if (entityScriptTable->GetValue(szLuaFuncToCall, functionToCall))
  169.                         {
  170.                                 Script::CallMethod(entityScriptTable, functionToCall);
  171.                                 gEnv->pScriptSystem->ReleaseFunc(functionToCall);
  172.                         }
  173.                 }
  174.         }
  175.  
  176.         // Activate action on all matching flownodes
  177.         if (m_pFlowGraph && szNodeToCall && szNodeToCall[0] != '\0') // Has a custom graph, otherwise its an instance custom action
  178.         {
  179.                 int idNode = 0;
  180.                 TFlowNodeTypeId nodeTypeId;
  181.                 TFlowNodeTypeId actionTypeId = gEnv->pFlowSystem->GetTypeId(szNodeToCall);
  182.                 while ((nodeTypeId = m_pFlowGraph->GetNodeTypeId(idNode)) != InvalidFlowNodeTypeId)
  183.                 {
  184.                         if (nodeTypeId == actionTypeId)
  185.                         {
  186.                                 m_pFlowGraph->SetRegularlyUpdated(idNode, true);
  187.                         }
  188.                         ++idNode;
  189.                 }
  190.         }
  191.  
  192.         return true;
  193. }
  194.  
  195. //------------------------------------------------------------------------------------------------------------------------
  196. /*
  197.    void CCustomAction::AttachToNodes()
  198.    {
  199.     TFlowNodeTypeId startActionTypeId = gEnv->pFlowSystem->GetTypeId("CustomAction:Start");
  200.     TFlowNodeTypeId succeedActionTypeId = gEnv->pFlowSystem->GetTypeId("CustomAction:Succeed");
  201.     TFlowNodeTypeId endActionTypeId = gEnv->pFlowSystem->GetTypeId("CustomAction:End");
  202.     TFlowNodeTypeId abortActionTypeId = gEnv->pFlowSystem->GetTypeId("CustomAction:Abort");
  203.  
  204.     int idNode = 0;
  205.     TFlowNodeTypeId nodeTypeId;
  206.     while ( (nodeTypeId = m_pFlowGraph->GetNodeTypeId( idNode )) != InvalidFlowNodeTypeId )
  207.     {
  208.       if ( (nodeTypeId == startActionTypeId ) ||
  209.            (nodeTypeId == succeedActionTypeId ) ||
  210.            (nodeTypeId == endActionTypeId ) ||
  211.            (nodeTypeId == abortActionTypeId ) )
  212.       {
  213.         IFlowNodeData* pFlowNodeData = m_pFlowGraph->GetNodeData(idNode);
  214.         if (pFlowNodeData)
  215.         {
  216.           CFlowNode_CustomActionBase* pCustomActionBaseNode = static_cast<CFlowNode_CustomActionBase*>(pFlowNodeData->GetNode());
  217.           CRY_ASSERT(pCustomActionBaseNode != NULL);
  218.           if (pCustomActionBaseNode != NULL)
  219.           {
  220.             pCustomActionBaseNode->SetCustomAction(this);
  221.           }
  222.         }
  223.       }
  224.     }
  225.    }
  226.  */
  227.  
downloadCustomAction.cpp Source code - Download CRYENGINE Source code
Related Source Codes/Software:
postal - 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top