Advertisement
Guest User

Untitled

a guest
Sep 7th, 2016
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.69 KB | None | 0 0
  1. #include "StdAfx.h"
  2. #include "MyAreaTrigger.h"
  3.  
  4. #include "Game/GameFactory.h"
  5.  
  6. #include "FlowNodes/Helpers/FlowGameEntityNode.h"
  7.  
  8. #include <CryAnimation/ICryAnimation.h>
  9. #include <CryEntitySystem/IEntityProxy.h>
  10. #include <IActorSystem.h>
  11.  
  12. class CAreaTriggerRegistrator
  13.     : public IEntityRegistrator
  14. {
  15.     virtual void Register() override
  16.     {
  17.         CAreaTriggerEntity::Register();
  18.     }
  19. };
  20.  
  21. CAreaTriggerRegistrator g_areaRegistrator;
  22.  
  23. CAreaTriggerEntity::CAreaTriggerEntity()
  24. {
  25. }
  26.  
  27. void CAreaTriggerEntity::ProcessEvent(SEntityEvent& event)
  28. {
  29.     if (gEnv->IsDedicated())
  30.         return;
  31.  
  32.     switch (event.event)
  33.     {
  34.         // Physicalize on level start for Launcher
  35.     case ENTITY_EVENT_START_LEVEL:
  36.         // Editor specific, physicalize on reset, property change or transform change
  37.     case ENTITY_EVENT_RESET:
  38.     case ENTITY_EVENT_EDITOR_PROPERTY_CHANGED:
  39.     case ENTITY_EVENT_XFORM_FINISHED_EDITOR:
  40.         Reset();
  41.         break;
  42.     case ENTITY_EVENT_ENTERAREA:
  43.     {
  44.        
  45.         IEntity * pEntity = gEnv->pEntitySystem->GetEntity((EntityId)event.nParam[0]);
  46.         IActor* pActor = gEnv->pGame->GetIGameFramework()->GetIActorSystem()->GetActor((EntityId)event.nParam[0]);
  47.         EntityId PlayerId = gEnv->pGame->GetIGameFramework()->GetClientActorId();
  48.         IActor* pPlayerAc = gEnv->pGame->GetIGameFramework()->GetIActorSystem()->GetActor(PlayerId);
  49.         if (pActor == pPlayerAc)
  50.         {
  51.             gEnv->pLog->Log("%s: Player Entered the area", GetEntity()->GetName());
  52.            
  53.             TFlowInputData data;
  54.             data.SetUserFlag(true);
  55.             ActivateFlowNodeOutput(eOutputPort_AreaEnter, data);
  56.            
  57.             TFlowInputData dataNum;
  58.             dataNum.SetUserFlag(true);
  59.             dataNum.Set<EntityId>(static_cast<EntityId>(event.nParam[0]));
  60.             ActivateFlowNodeOutput(eOutputPort_AreaEnterEntityId, dataNum);
  61.  
  62.         }
  63.  
  64.         if (pActor != pPlayerAc)
  65.         {
  66.             gEnv->pLog->Log("%s: Something entered the area", GetEntity()->GetName());
  67.         }
  68.  
  69.         break;
  70.     }
  71.     case ENTITY_EVENT_LEAVEAREA:
  72.     {
  73.         IEntity * pEntity = gEnv->pEntitySystem->GetEntity((EntityId)event.nParam[0]);
  74.         IActor* pActor = gEnv->pGame->GetIGameFramework()->GetIActorSystem()->GetActor((EntityId)event.nParam[0]);
  75.         EntityId PlayerId = gEnv->pGame->GetIGameFramework()->GetClientActorId();
  76.         IActor* pPlayerAc = gEnv->pGame->GetIGameFramework()->GetIActorSystem()->GetActor(PlayerId);
  77.         if (pActor == pPlayerAc)
  78.         {
  79.             gEnv->pLog->Log("%s: Player Left the area", GetEntity()->GetName());
  80.  
  81.             TFlowInputData data;
  82.             data.SetUserFlag(true);
  83.             ActivateFlowNodeOutput(eOutputPort_AreaLeave, data);
  84.  
  85.             TFlowInputData dataNum;
  86.             dataNum.SetUserFlag(true);
  87.             dataNum.Set<EntityId>(static_cast<EntityId>(event.nParam[0]));
  88.             ActivateFlowNodeOutput(eOutputPort_AreaLeaveEntityId, dataNum);
  89.  
  90.         }
  91.  
  92.         if (pActor != pPlayerAc)
  93.         {
  94.             gEnv->pLog->Log("%s: Something left the area", GetEntity()->GetName());
  95.         }
  96.         break;
  97.     }
  98.     }
  99. }
  100.  
  101. void CAreaTriggerEntity::Reset()
  102. {
  103.     IEntity &entity = *GetEntity();
  104.  
  105.     // Check if the light is active
  106.     if (!GetPropertyBool(eProperty_Active))
  107.         return;
  108.  
  109.     IEntityTriggerProxy *pTriggerProxy = (IEntityTriggerProxy*)(GetEntity()->GetProxy(ENTITY_PROXY_TRIGGER));
  110.  
  111.     if (!pTriggerProxy)
  112.     {
  113.         GetEntity()->CreateProxy(ENTITY_PROXY_TRIGGER);
  114.         pTriggerProxy = (IEntityTriggerProxy*)GetEntity()->GetProxy(ENTITY_PROXY_TRIGGER);
  115.     }
  116.  
  117.     if (pTriggerProxy)
  118.     {
  119.         float radius = GetPropertyFloat(eProperty_Radius) * 0.5f;
  120.         AABB boundingBox = AABB(Vec3(-radius, -radius, -radius), Vec3(radius, radius, radius));
  121.         pTriggerProxy->SetTriggerBounds(boundingBox);
  122.     }
  123.     else
  124.     {
  125.         gEnv->pLog->Log("%s: Warning: Trigger Area Has Bad Params", GetEntity()->GetName());
  126.     }
  127. }
  128.  
  129. // Static function
  130. void CAreaTriggerEntity::Register()
  131. {
  132.     auto properties = new SNativeEntityPropertyInfo[eNumProperties];
  133.     memset(properties, 0, sizeof(SNativeEntityPropertyInfo) * eNumProperties);
  134.  
  135.     RegisterEntityProperty<bool>(properties, eProperty_Active, "Active", "1", "", 0, 1);
  136.  
  137.     RegisterEntityProperty<float>(properties, eProperty_Radius, "Radius", "4", "", 0.1f, 100.f);
  138.  
  139.     // Finally, register the entity class so that instances can be created later on either via Launcher or Editor
  140.     CGameFactory::RegisterNativeEntity<CAreaTriggerEntity>("MyAreaTrigger", "UserStuff", "Trigger.bmp", 0u, properties, eNumProperties);
  141.  
  142.     // Create flownode
  143.     CGameEntityNodeFactory &nodeFactory = CGameFactory::RegisterEntityFlowNode("MyAreaTrigger");
  144.  
  145.     // Define input ports, and the callback function for when they are triggered
  146.     std::vector<SInputPortConfig> inputs;
  147.     inputs.push_back(InputPortConfig_Void("TurnOn", "Turns the Area on"));
  148.     inputs.push_back(InputPortConfig_Void("TurnOff", "Turns the Area off"));
  149.     nodeFactory.AddInputs(inputs, OnFlowgraphActivation);
  150.  
  151.     std::vector<SOutputPortConfig> outputs;
  152.     outputs.push_back(OutputPortConfig_Void("AreaEnter", "If player enter to area"));
  153.     outputs.push_back(OutputPortConfig_Void("AreaLeave", "If player leave area"));
  154.     outputs.push_back(OutputPortConfig<int>("AreaEnterEntityId", "Entity id enter area"));
  155.     outputs.push_back(OutputPortConfig<int>("AreaLeaveEntityId", "Entity id leave area"));
  156.     //outputs.push_back(OutputPortConfig_AnyType("IsActive", "If player leave area"));
  157.     nodeFactory.AddOutputs(outputs);
  158.  
  159.  
  160.     // Mark the factory as complete, indicating that there will be no additional ports
  161.     nodeFactory.Close();
  162. }
  163.  
  164. void CAreaTriggerEntity::OnFlowgraphActivation(EntityId entityId, IFlowNode::SActivationInfo *pActInfo, const class CFlowGameEntityNode *pNode)
  165. {
  166.     if (auto *pLightSource = static_cast<CAreaTriggerEntity *>(QueryExtension(entityId)))
  167.     {
  168.         if (IsPortActive(pActInfo, eInputPort_TurnOn))
  169.         {
  170.             pLightSource->SetPropertyBool(eProperty_Active, true);
  171.         }
  172.         else if (IsPortActive(pActInfo, eInputPort_TurnOff))
  173.         {
  174.             pLightSource->SetPropertyBool(eProperty_Active, false);
  175.         }
  176.     }
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement