Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "StdAfx.h"
- #include "MyAreaTrigger.h"
- #include "Game/GameFactory.h"
- #include "FlowNodes/Helpers/FlowGameEntityNode.h"
- #include <CryAnimation/ICryAnimation.h>
- #include <CryEntitySystem/IEntityProxy.h>
- #include <IActorSystem.h>
- class CAreaTriggerRegistrator
- : public IEntityRegistrator
- {
- virtual void Register() override
- {
- CAreaTriggerEntity::Register();
- }
- };
- CAreaTriggerRegistrator g_areaRegistrator;
- CAreaTriggerEntity::CAreaTriggerEntity()
- {
- }
- void CAreaTriggerEntity::ProcessEvent(SEntityEvent& event)
- {
- if (gEnv->IsDedicated())
- return;
- switch (event.event)
- {
- // Physicalize on level start for Launcher
- case ENTITY_EVENT_START_LEVEL:
- // Editor specific, physicalize on reset, property change or transform change
- case ENTITY_EVENT_RESET:
- case ENTITY_EVENT_EDITOR_PROPERTY_CHANGED:
- case ENTITY_EVENT_XFORM_FINISHED_EDITOR:
- Reset();
- break;
- case ENTITY_EVENT_ENTERAREA:
- {
- IEntity * pEntity = gEnv->pEntitySystem->GetEntity((EntityId)event.nParam[0]);
- IActor* pActor = gEnv->pGame->GetIGameFramework()->GetIActorSystem()->GetActor((EntityId)event.nParam[0]);
- EntityId PlayerId = gEnv->pGame->GetIGameFramework()->GetClientActorId();
- IActor* pPlayerAc = gEnv->pGame->GetIGameFramework()->GetIActorSystem()->GetActor(PlayerId);
- if (pActor == pPlayerAc)
- {
- gEnv->pLog->Log("%s: Player Entered the area", GetEntity()->GetName());
- TFlowInputData data;
- data.SetUserFlag(true);
- ActivateFlowNodeOutput(eOutputPort_AreaEnter, data);
- TFlowInputData dataNum;
- dataNum.SetUserFlag(true);
- dataNum.Set<EntityId>(static_cast<EntityId>(event.nParam[0]));
- ActivateFlowNodeOutput(eOutputPort_AreaEnterEntityId, dataNum);
- }
- if (pActor != pPlayerAc)
- {
- gEnv->pLog->Log("%s: Something entered the area", GetEntity()->GetName());
- }
- break;
- }
- case ENTITY_EVENT_LEAVEAREA:
- {
- IEntity * pEntity = gEnv->pEntitySystem->GetEntity((EntityId)event.nParam[0]);
- IActor* pActor = gEnv->pGame->GetIGameFramework()->GetIActorSystem()->GetActor((EntityId)event.nParam[0]);
- EntityId PlayerId = gEnv->pGame->GetIGameFramework()->GetClientActorId();
- IActor* pPlayerAc = gEnv->pGame->GetIGameFramework()->GetIActorSystem()->GetActor(PlayerId);
- if (pActor == pPlayerAc)
- {
- gEnv->pLog->Log("%s: Player Left the area", GetEntity()->GetName());
- TFlowInputData data;
- data.SetUserFlag(true);
- ActivateFlowNodeOutput(eOutputPort_AreaLeave, data);
- TFlowInputData dataNum;
- dataNum.SetUserFlag(true);
- dataNum.Set<EntityId>(static_cast<EntityId>(event.nParam[0]));
- ActivateFlowNodeOutput(eOutputPort_AreaLeaveEntityId, dataNum);
- }
- if (pActor != pPlayerAc)
- {
- gEnv->pLog->Log("%s: Something left the area", GetEntity()->GetName());
- }
- break;
- }
- }
- }
- void CAreaTriggerEntity::Reset()
- {
- IEntity &entity = *GetEntity();
- // Check if the light is active
- if (!GetPropertyBool(eProperty_Active))
- return;
- IEntityTriggerProxy *pTriggerProxy = (IEntityTriggerProxy*)(GetEntity()->GetProxy(ENTITY_PROXY_TRIGGER));
- if (!pTriggerProxy)
- {
- GetEntity()->CreateProxy(ENTITY_PROXY_TRIGGER);
- pTriggerProxy = (IEntityTriggerProxy*)GetEntity()->GetProxy(ENTITY_PROXY_TRIGGER);
- }
- if (pTriggerProxy)
- {
- float radius = GetPropertyFloat(eProperty_Radius) * 0.5f;
- AABB boundingBox = AABB(Vec3(-radius, -radius, -radius), Vec3(radius, radius, radius));
- pTriggerProxy->SetTriggerBounds(boundingBox);
- }
- else
- {
- gEnv->pLog->Log("%s: Warning: Trigger Area Has Bad Params", GetEntity()->GetName());
- }
- }
- // Static function
- void CAreaTriggerEntity::Register()
- {
- auto properties = new SNativeEntityPropertyInfo[eNumProperties];
- memset(properties, 0, sizeof(SNativeEntityPropertyInfo) * eNumProperties);
- RegisterEntityProperty<bool>(properties, eProperty_Active, "Active", "1", "", 0, 1);
- RegisterEntityProperty<float>(properties, eProperty_Radius, "Radius", "4", "", 0.1f, 100.f);
- // Finally, register the entity class so that instances can be created later on either via Launcher or Editor
- CGameFactory::RegisterNativeEntity<CAreaTriggerEntity>("MyAreaTrigger", "UserStuff", "Trigger.bmp", 0u, properties, eNumProperties);
- // Create flownode
- CGameEntityNodeFactory &nodeFactory = CGameFactory::RegisterEntityFlowNode("MyAreaTrigger");
- // Define input ports, and the callback function for when they are triggered
- std::vector<SInputPortConfig> inputs;
- inputs.push_back(InputPortConfig_Void("TurnOn", "Turns the Area on"));
- inputs.push_back(InputPortConfig_Void("TurnOff", "Turns the Area off"));
- nodeFactory.AddInputs(inputs, OnFlowgraphActivation);
- std::vector<SOutputPortConfig> outputs;
- outputs.push_back(OutputPortConfig_Void("AreaEnter", "If player enter to area"));
- outputs.push_back(OutputPortConfig_Void("AreaLeave", "If player leave area"));
- outputs.push_back(OutputPortConfig<int>("AreaEnterEntityId", "Entity id enter area"));
- outputs.push_back(OutputPortConfig<int>("AreaLeaveEntityId", "Entity id leave area"));
- //outputs.push_back(OutputPortConfig_AnyType("IsActive", "If player leave area"));
- nodeFactory.AddOutputs(outputs);
- // Mark the factory as complete, indicating that there will be no additional ports
- nodeFactory.Close();
- }
- void CAreaTriggerEntity::OnFlowgraphActivation(EntityId entityId, IFlowNode::SActivationInfo *pActInfo, const class CFlowGameEntityNode *pNode)
- {
- if (auto *pLightSource = static_cast<CAreaTriggerEntity *>(QueryExtension(entityId)))
- {
- if (IsPortActive(pActInfo, eInputPort_TurnOn))
- {
- pLightSource->SetPropertyBool(eProperty_Active, true);
- }
- else if (IsPortActive(pActInfo, eInputPort_TurnOff))
- {
- pLightSource->SetPropertyBool(eProperty_Active, false);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement