Plasma Engine  2.0
Loading...
Searching...
No Matches
SensorComponent.h
1#pragma once
2
3#include <Core/Messages/EventMessage.h>
4#include <Core/Utils/IntervalScheduler.h>
5#include <Core/World/World.h>
6#include <GameEngine/GameEngineDLL.h>
7
9
10struct PL_GAMEENGINE_DLL plMsgSensorDetectedObjectsChanged : public plEventMessage
11{
12 PL_DECLARE_MESSAGE_TYPE(plMsgSensorDetectedObjectsChanged, plEventMessage);
13
14 plArrayPtr<plGameObjectHandle> m_DetectedObjects;
15};
16
18
26class PL_GAMEENGINE_DLL plSensorComponent : public plComponent
27{
28 PL_DECLARE_ABSTRACT_COMPONENT_TYPE(plSensorComponent, plComponent);
29
31 // plComponent
32
33public:
34 virtual void SerializeComponent(plWorldWriter& inout_stream) const override;
35 virtual void DeserializeComponent(plWorldReader& inout_stream) override;
36
37protected:
38 virtual void OnActivated() override;
39 virtual void OnDeactivated() override;
40
42 // plSensorComponent
43
44public:
47
48 virtual void GetObjectsInSensorVolume(plDynamicArray<plGameObject*>& out_objects) const = 0;
49 virtual void DebugDrawSensorShape() const = 0;
50
51 void SetSpatialCategory(const char* szCategory); // [ property ]
52 const char* GetSpatialCategory() const; // [ property ]
53
54 bool m_bTestVisibility = true; // [ property ]
55 plUInt8 m_uiCollisionLayer = 0; // [ property ]
56
57 void SetUpdateRate(const plEnum<plUpdateRate>& updateRate); // [ property ]
58 const plEnum<plUpdateRate>& GetUpdateRate() const; // [ property ]
59
60 void SetShowDebugInfo(bool bShow); // [ property ]
61 bool GetShowDebugInfo() const; // [ property ]
62
63 void SetColor(plColorGammaUB color); // [ property ]
64 plColorGammaUB GetColor() const; // [ property ]
65
66 plTagSet m_IncludeTags; // [ property ]
67 plTagSet m_ExcludeTags; // [ property ]
68
70 plArrayPtr<const plGameObjectHandle> GetLastDetectedObjects() const { return m_LastDetectedObjects; }
71
85 bool RunSensorCheck(plPhysicsWorldModuleInterface* pPhysicsWorldModule, plDynamicArray<plGameObject*>& out_objectsInSensorVolume, plDynamicArray<plGameObjectHandle>& ref_detectedObjects, bool bPostChangeMsg) const;
86
87protected:
88 void UpdateSpatialCategory();
89 void UpdateScheduling();
90 void UpdateDebugInfo();
91
92 plEnum<plUpdateRate> m_UpdateRate;
93 bool m_bShowDebugInfo = false;
94 plColorGammaUB m_Color = plColorScheme::LightUI(plColorScheme::Orange);
95
96 plHashedString m_sSpatialCategory;
97 plSpatialData::Category m_SpatialCategory = plInvalidSpatialDataCategory;
98
99 friend class plSensorWorldModule;
100 mutable plDynamicArray<plGameObjectHandle> m_LastDetectedObjects;
101
102#if PL_ENABLED(PL_COMPILE_FOR_DEVELOPMENT)
103 mutable plDynamicArray<plVec3> m_LastOccludedObjectPositions;
104#endif
105};
106
108
110
111class PL_GAMEENGINE_DLL plSensorSphereComponent : public plSensorComponent
112{
114
116 // plComponent
117
118public:
119 virtual void SerializeComponent(plWorldWriter& inout_stream) const override;
120 virtual void DeserializeComponent(plWorldReader& inout_stream) override;
121
123 // plSensorComponent
124
125 virtual void GetObjectsInSensorVolume(plDynamicArray<plGameObject*>& out_objects) const override;
126 virtual void DebugDrawSensorShape() const override;
127
129 // plSensorSphereComponent
130
131public:
134
135 float m_fRadius = 10.0f; // [ property ]
136};
137
139
141
142class PL_GAMEENGINE_DLL plSensorCylinderComponent : public plSensorComponent
143{
145
147 // plComponent
148
149public:
150 virtual void SerializeComponent(plWorldWriter& inout_stream) const override;
151 virtual void DeserializeComponent(plWorldReader& inout_stream) override;
152
154 // plSensorComponent
155
156 virtual void GetObjectsInSensorVolume(plDynamicArray<plGameObject*>& out_objects) const override;
157 virtual void DebugDrawSensorShape() const override;
158
160 // plSensorCylinderComponent
161
162public:
165
166 float m_fRadius = 10.0f; // [ property ]
167 float m_fHeight = 10.0f; // [ property ]
168};
169
171
173
174class PL_GAMEENGINE_DLL plSensorConeComponent : public plSensorComponent
175{
177
179 // plComponent
180
181public:
182 virtual void SerializeComponent(plWorldWriter& inout_stream) const override;
183 virtual void DeserializeComponent(plWorldReader& inout_stream) override;
184
186 // plSensorComponent
187
188 virtual void GetObjectsInSensorVolume(plDynamicArray<plGameObject*>& out_objects) const override;
189 virtual void DebugDrawSensorShape() const override;
190
192 // plSensorConeComponent
193
194public:
197
198 float m_fNearDistance = 0.0f; // [ property ]
199 float m_fFarDistance = 10.0f; // [ property ]
200 plAngle m_Angle = plAngle::MakeFromDegree(90.0f); // [ property ]
201};
202
204
206{
207 PL_DECLARE_WORLD_MODULE();
208 PL_ADD_DYNAMIC_REFLECTION(plSensorWorldModule, plWorldModule);
209
210public:
212
213 virtual void Initialize() override;
214
215 void AddComponentToSchedule(plSensorComponent* pComponent, plUpdateRate::Enum updateRate);
216 void RemoveComponentToSchedule(plSensorComponent* pComponent);
217
218 void AddComponentForDebugRendering(plSensorComponent* pComponent);
219 void RemoveComponentForDebugRendering(plSensorComponent* pComponent);
220
221private:
222 void UpdateSensors(const plWorldModule::UpdateContext& context);
223 void DebugDrawSensors(const plWorldModule::UpdateContext& context);
224
226 plPhysicsWorldModuleInterface* m_pPhysicsWorldModule = nullptr;
227
228 plDynamicArray<plGameObject*> m_ObjectsInSensorVolume;
229 plDynamicArray<plGameObjectHandle> m_DetectedObjects;
230
231 plDynamicArray<plComponentHandle> m_DebugComponents;
232};
Float wrapper struct for a safe usage and conversions of angles.
Definition Angle.h:10
static constexpr plAngle MakeFromDegree(float fDegree)
Creates an instance of plAngle that was initialized from degree. (Performs a conversion)
Definition Angle_inl.h:33
This class encapsulates an array and it's size. It is recommended to use this class instead of plain ...
Definition ArrayPtr.h:37
A 8bit per channel unsigned normalized (values interpreted as 0-1) color storage format that represen...
Definition Color8UNorm.h:99
static PL_ALWAYS_INLINE plColor LightUI(Enum schemeColor)
Get a scheme color with predefined brightness and saturation to look good as highlight color in PL to...
Definition ColorScheme.h:66
Base class of all component types.
Definition Component.h:25
Definition ComponentManager.h:88
Definition DynamicArray.h:81
This class is optimized to take nearly no memory (sizeof(void*)) and to allow very fast checks whethe...
Definition HashedString.h:25
Definition IntervalScheduler.h:65
Definition PhysicsWorldModule.h:109
Base class for sensor components that can be used for AI perception like vision or hearing.
Definition SensorComponent.h:27
plArrayPtr< const plGameObjectHandle > GetLastDetectedObjects() const
Returns the list of objects that this sensor has detected during its last update.
Definition SensorComponent.h:70
Definition SensorComponent.h:175
Definition SensorComponent.h:143
Definition SensorComponent.h:112
Definition SensorComponent.h:206
virtual void Initialize() override
This method is called after the constructor. A derived type can override this method to do initializa...
Definition SensorComponent.cpp:616
A dynamic collection of tags featuring fast lookups.
Definition TagSet.h:23
A world encapsulates a scene graph of game objects and various component managers and their component...
Definition World.h:22
Definition WorldModule.h:10
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 custom enum implementation that allows to define the underlying storage type to control its memory ...
Definition Enum.h:37
Base class for all messages that are sent as 'events'.
Definition EventMessage.h:8
Definition SensorComponent.h:11
Definition SpatialData.h:27
Definition WorldModule.h:33