Plasma Engine  2.0
Loading...
Searching...
No Matches
FmodEventComponent.h
1#pragma once
2
3#include <Core/Messages/EventMessage.h>
4#include <Core/ResourceManager/Resource.h>
5#include <FmodPlugin/Components/FmodComponent.h>
6
8{
9public:
10 PL_ALWAYS_INLINE void Invalidate() { m_uiValue = -1; }
11 PL_ALWAYS_INLINE bool IsInvalidated() const { return m_uiValue == -1; }
12
13private:
14 plUInt64 m_uiValue = -1;
15};
16
19
20class plFmodEventComponentManager : public plComponentManager<class plFmodEventComponent, plBlockStorageType::FreeList>
21{
22public:
24
25 virtual void Initialize() override;
26 virtual void Deinitialize() override;
27
28private:
29 friend class plFmodEventComponent;
30
31 struct OcclusionState
32 {
33 plFmodEventComponent* m_pComponent = nullptr;
34 plFmodParameterId m_OcclusionParamId;
35 plUInt32 m_uiRaycastHits = 0;
36 plUInt8 m_uiNextRayIndex = 0;
37 plUInt8 m_uiNumUsedRays = 0;
38 float m_fRadius = 0.0f;
39 float m_fLastOcclusionValue = -1.0f;
40
41 float GetOcclusionValue(float fThreshold) const { return plMath::Clamp((m_fLastOcclusionValue - fThreshold) / plMath::Max(1.0f - fThreshold, 0.0001f), 0.0f, 1.0f); }
42 };
43
44 plDynamicArray<OcclusionState> m_OcclusionStates;
45
46 plUInt32 AddOcclusionState(plFmodEventComponent* pComponent, plFmodParameterId occlusionParamId, float fRadius);
47 void RemoveOcclusionState(plUInt32 uiIndex);
48 const OcclusionState& GetOcclusionState(plUInt32 uiIndex) const { return m_OcclusionStates[uiIndex]; }
49
50 void ShootOcclusionRays(
51 OcclusionState& state, plVec3 listenerPos, plUInt32 uiNumRays, const plPhysicsWorldModuleInterface* pPhysicsWorldModule, plTime deltaTime);
52 void UpdateOcclusion(const plWorldModule::UpdateContext& context);
53 void UpdateEvents(const plWorldModule::UpdateContext& context);
54
55 void ResourceEventHandler(const plResourceEvent& e);
56};
57
59
60struct plResourceEvent;
61
63
65struct PL_FMODPLUGIN_DLL plMsgFmodSoundFinished : public plEventMessage
66{
67 PL_DECLARE_MESSAGE_TYPE(plMsgFmodSoundFinished, plEventMessage);
68};
69
70
72
76class PL_FMODPLUGIN_DLL plFmodEventComponent : public plFmodComponent
77{
79
81 // plComponent
82
83public:
84 virtual void SerializeComponent(plWorldWriter& inout_stream) const override;
85 virtual void DeserializeComponent(plWorldReader& inout_stream) override;
86
87protected:
88 virtual void OnSimulationStarted() override;
89 virtual void OnDeactivated() override;
90
91
93 // plFmodComponent
94
95private:
96 virtual void plFmodComponentIsAbstract() override {}
97 friend class plComponentManagerSimple<class plFmodEventComponent, plComponentUpdateType::WhenSimulating>;
98
99
101 // plFmodEventComponent
102
103public:
106
107 void SetPaused(bool b); // [ property ]
108 bool GetPaused() const { return m_bPaused; } // [ property ]
109
110 void SetUseOcclusion(bool b); // [ property ]
111 bool GetUseOcclusion() const { return m_bUseOcclusion; } // [ property ]
112
113 void SetOcclusionCollisionLayer(plUInt8 uiCollisionLayer); // [ property ]
114 plUInt8 GetOcclusionCollisionLayer() const { return m_uiOcclusionCollisionLayer; } // [ property ]
115
116 void SetOcclusionThreshold(float fThreshold); // [ property ]
117 float GetOcclusionThreshold() const; // [ property ]
118
119 void SetPitch(float f); // [ property ]
120 float GetPitch() const { return m_fPitch; } // [ property ]
121
122 void SetVolume(float f); // [ property ]
123 float GetVolume() const { return m_fVolume; } // [ property ]
124
125 void SetSoundEventFile(const char* szFile); // [ property ]
126 const char* GetSoundEventFile() const; // [ property ]
127
128 void SetSoundEvent(const plFmodSoundEventResourceHandle& hSoundEvent);
129 const plFmodSoundEventResourceHandle& GetSoundEvent() const { return m_hSoundEvent; }
130
131 plEnum<plOnComponentFinishedAction> m_OnFinishedAction; // [ property ]
132
133 void SetShowDebugInfo(bool bShow); // [ property ]
134 bool GetShowDebugInfo() const; // [ property ]
135
138 void Restart(); // [ scriptable ]
139
146 void StartOneShot(); // [ scriptable ]
147
149 void StopSound(bool bImmediate); // [ scriptable ]
150
152 void SoundCue(); // [ scriptable ]
153
155 plFmodParameterId FindParameter(const char* szName) const;
156
158 void SetParameter(plFmodParameterId paramId, float fValue);
159
161 float GetParameter(plFmodParameterId paramId) const;
162
164 void SetEventParameter(const char* szParamName, float fValue); // [ scriptable ]
165
169 void OnMsgSetFloatParameter(plMsgSetFloatParameter& ref_msg); // [ msg handler ]
170
171protected:
172 void OnMsgDeleteGameObject(plMsgDeleteGameObject& msg); // [ msg handler ]
173
174 void Update();
175 void UpdateParameters(FMOD::Studio::EventInstance* pInstance);
176 void UpdateOcclusion();
177
179 void InvalidateResource(bool bTryToRestore);
180
181
182 bool m_bPaused;
183 bool m_bUseOcclusion;
184 plUInt8 m_uiOcclusionThreshold;
185 plUInt8 m_uiOcclusionCollisionLayer;
186 float m_fPitch;
187 float m_fVolume;
188 plInt32 m_iTimelinePosition = -1; // used to restore a sound after reloading the resource
189 plUInt32 m_uiOcclusionStateIndex = plInvalidIndex;
190 plFmodSoundEventResourceHandle m_hSoundEvent;
191
192 FMOD::Studio::EventDescription* m_pEventDesc;
193 FMOD::Studio::EventInstance* m_pEventInstance;
194
195 plEventMessageSender<plMsgFmodSoundFinished> m_SoundFinishedEventSender; // [ event ]
196};
Definition ComponentManager.h:88
Simple component manager implementation that calls an update method on all components every frame.
Definition ComponentManager.h:142
Definition DynamicArray.h:81
A message sender that sends all messages to the next component derived from plEventMessageHandlerComp...
Definition EventMessage.h:39
Base class for all Fmod components, such that they all have a common ancestor.
Definition FmodComponent.h:9
Represents a sound (called an 'event') in the Fmod sound system.
Definition FmodEventComponent.h:77
Definition FmodEventComponent.h:21
virtual void Deinitialize() override
This method is called before the destructor. A derived type can override this method to do deinitiali...
Definition FmodEventComponent.cpp:95
virtual void Initialize() override
This method is called after the constructor. A derived type can override this method to do initializa...
Definition FmodEventComponent.cpp:72
Definition PhysicsWorldModule.h:109
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
constexpr PL_ALWAYS_INLINE T Clamp(T value, T min_val, T max_val)
Clamps "value" to the range [min; max]. Returns "value", if it is inside the range already.
Definition Math_inl.h:51
constexpr PL_ALWAYS_INLINE T Max(T f1, T f2)
Returns the greater value, f1 or f2.
Definition Math_inl.h:39
Definition ComponentManager.h:131
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 FmodEventComponent.h:8
Definition DeleteObjectMessage.h:7
Sent when a plFmodEventComponent finishes playing a sound. Not sent for one-shot sound events.
Definition FmodEventComponent.h:66
Basic message to set some generic parameter to a float value.
Definition CommonMessages.h:17
These events may be sent by a specific plResource or by the plResourceManager.
Definition Declarations.h:22
The time class encapsulates a double value storing the time in seconds.
Definition Time.h:12
Definition WorldModule.h:33