Plasma Engine  2.0
Loading...
Searching...
No Matches
RecastAgentComponent.h
1#pragma once
2
3#include <Core/World/Component.h>
4#include <Core/World/World.h>
5#include <DetourNavMeshQuery.h>
6#include <DetourPathCorridor.h>
7#include <RecastPlugin/Components/AgentSteeringComponent.h>
8#include <RecastPlugin/Components/RecastNavMeshComponent.h>
9#include <RecastPlugin/NavMeshBuilder/NavMeshBuilder.h>
10#include <RecastPlugin/RecastPluginDLL.h>
11
14struct plResourceEvent;
15
17
18class PL_RECASTPLUGIN_DLL plRcAgentComponentManager : public plComponentManager<class plRcAgentComponent, plBlockStorageType::FreeList>
19{
21
22public:
25
26 virtual void Initialize() override;
27 virtual void Deinitialize() override;
28
29 plRecastWorldModule* GetRecastWorldModule() const { return m_pWorldModule; }
30
31private:
32 void ResourceEventHandler(const plResourceEvent& e);
33 void Update(const plWorldModule::UpdateContext& context);
34
35 plPhysicsWorldModuleInterface* m_pPhysicsInterface = nullptr;
36 plRecastWorldModule* m_pWorldModule = nullptr;
37};
38
40
41class PL_RECASTPLUGIN_DLL plRcAgentComponent : public plAgentSteeringComponent
42{
44
46 // plComponent
47
48protected:
49 virtual void SerializeComponent(plWorldWriter& stream) const override;
50 virtual void DeserializeComponent(plWorldReader& stream) override;
51
53 // plAgentSteeringComponent
54
55public:
58
59 virtual void SetTargetPosition(const plVec3& vPosition) override;
60 virtual plVec3 GetTargetPosition() const override;
61 virtual void ClearTargetPosition() override;
62 virtual plAgentPathFindingState::Enum GetPathToTargetState() const override;
63
65 // Helper Functions
66
67public:
68 plResult FindNavMeshPolyAt(const plVec3& vPosition, dtPolyRef& out_polyRef, plVec3* out_pAdjustedPosition = nullptr, float fPlaneEpsilon = 0.01f,
69 float fHeightEpsilon = 1.0f) const;
70 bool HasReachedPosition(const plVec3& vPos, float fMaxDistance) const;
71 bool HasReachedGoal(float fMaxDistance) const;
72 bool IsPositionVisible(const plVec3& vPos) const;
73
75 // Debug Visualization Functions
76
77private:
78 void VisualizePathCorridorPosition();
79 void VisualizePathCorridor();
80 void VisualizeCurrentPath();
81 void VisualizeTargetPosition();
82
84 // Path Finding and Steering
85
86private:
87 plResult ComputePathToTarget();
88 plResult ComputePathCorridor(dtPolyRef startPoly, dtPolyRef endPoly, bool& bFoundPartialPath);
89 void ComputeSteeringDirection(float fMaxDistance);
90 void ApplySteering(const plVec3& vDirection, float fSpeed);
91 void SyncSteeringWithReality();
92 void PlanNextSteps();
93
94 plVec3 m_vTargetPosition;
95 plEnum<plAgentPathFindingState> m_PathToTargetState;
96 plVec3 m_vCurrentPositionOnNavmesh;
97 plUniquePtr<dtNavMeshQuery> m_pQuery; // careful, dtNavMeshQuery is not moveble
98 plUniquePtr<dtPathCorridor> m_pCorridor; // careful, dtPathCorridor is not moveble
99 dtQueryFilter m_QueryFilter;
100 plDynamicArray<dtPolyRef> m_PathCorridor;
101 // path following
102 plInt32 m_iFirstNextStep = 0;
103 plInt32 m_iNumNextSteps = 0;
104 plVec3 m_vNextSteps[16];
105 plVec3 m_vCurrentSteeringDirection;
106
107
109 // Properties
110public:
111 float m_fWalkSpeed = 4.0f; // [ property ]
113 // float m_fRadius = 0.2f;
114 // float m_fHeight = 1.0f;
115
116
118 // Other
119private:
120 plResult InitializeRecast();
121 void UninitializeRecast();
122 virtual void OnSimulationStarted() override;
123 void Update();
124
125 bool m_bRecastInitialized = false;
126 plComponentHandle m_hCharacterController;
127};
Base class for components that implement 'agent steering' behavior. If, moving from point A to point ...
Definition AgentSteeringComponent.h:44
Definition ComponentManager.h:88
Definition DynamicArray.h:81
Definition PhysicsWorldModule.h:109
Definition RecastAgentComponent.h:42
Definition RecastAgentComponent.h:19
Definition RecastWorldModule.h:16
A Unique ptr manages an object and destroys that object when it goes out of scope....
Definition UniquePtr.h:10
A world encapsulates a scene graph of game objects and various component managers and their component...
Definition World.h:22
Reads a world description from a stream. Allows to instantiate that world multiple times in different...
Definition WorldReader.h:47
Stores an entire plWorld in a stream.
Definition WorldWriter.h:13
A handle to a component.
Definition Declarations.h:138
A custom enum implementation that allows to define the underlying storage type to control its memory ...
Definition Enum.h:37
These events may be sent by a specific plResource or by the plResourceManager.
Definition Declarations.h:22
Default enum for returning failure or success, instead of using a bool.
Definition Types.h:54
Definition WorldModule.h:33