1 // Copyright 2001-2018 Crytek GmbH / Crytek Group. All rights reserved.
4 #include "RuntimeAreaObject.h"
6 CRuntimeAreaObject::TAudioControlMap
CRuntimeAreaObject::m_audioControls
;
8 ///////////////////////////////////////////////////////////////////////////
9 CRuntimeAreaObject::CRuntimeAreaObject()
13 ///////////////////////////////////////////////////////////////////////////
14 CRuntimeAreaObject::~CRuntimeAreaObject()
18 //////////////////////////////////////////////////////////////////////////
19 bool CRuntimeAreaObject::Init(IGameObject
* pGameObject
)
21 SetGameObject(pGameObject
);
26 //////////////////////////////////////////////////////////////////////////
27 bool CRuntimeAreaObject::ReloadExtension(IGameObject
* pGameObject
, const SEntitySpawnParams
& params
)
31 CRY_ASSERT_MESSAGE(false, "CRuntimeAreaObject::ReloadExtension not implemented");
36 ///////////////////////////////////////////////////////////////////////////
37 bool CRuntimeAreaObject::NetSerialize(TSerialize ser
, EEntityAspects aspect
, uint8 profile
, int flags
)
39 CRY_ASSERT_MESSAGE(false, "CRuntimeAreaObject::NetSerialize not implemented");
44 ///////////////////////////////////////////////////////////////////////////
45 void CRuntimeAreaObject::ProcessEvent(const SEntityEvent
& entityEvent
)
47 switch (entityEvent
.event
)
49 case ENTITY_EVENT_ENTERAREA
:
51 EntityId
const nEntityID
= static_cast<EntityId
>(entityEvent
.nParam
[0]);
53 IEntity
* const pEntity
= gEnv
->pEntitySystem
->GetEntity(nEntityID
);
54 if ((pEntity
!= NULL
) && ((pEntity
->GetFlagsExtended() & ENTITY_FLAG_EXTENDED_CAN_COLLIDE_WITH_MERGED_MESHES
) != 0))
56 TAudioParameterMap cNewEntityParamMap
;
58 UpdateParameterValues(pEntity
, cNewEntityParamMap
);
60 m_activeEntitySounds
.insert(
61 std::pair
<EntityId
, TAudioParameterMap
>(static_cast<EntityId
>(nEntityID
), cNewEntityParamMap
));
66 case ENTITY_EVENT_LEAVEAREA
:
68 EntityId
const nEntityID
= static_cast<EntityId
>(entityEvent
.nParam
[0]);
70 TEntitySoundsMap::iterator iFoundPair
= m_activeEntitySounds
.find(nEntityID
);
71 if (iFoundPair
!= m_activeEntitySounds
.end())
73 StopEntitySounds(iFoundPair
->first
, iFoundPair
->second
);
74 m_activeEntitySounds
.erase(nEntityID
);
79 case ENTITY_EVENT_MOVEINSIDEAREA
:
81 EntityId
const nEntityID
= static_cast<EntityId
>(entityEvent
.nParam
[0]);
82 TEntitySoundsMap::iterator iFoundPair
= m_activeEntitySounds
.find(nEntityID
);
84 if (iFoundPair
!= m_activeEntitySounds
.end())
86 IEntity
* const pEntity
= gEnv
->pEntitySystem
->GetEntity(nEntityID
);
87 if ((pEntity
!= NULL
) && ((pEntity
->GetFlagsExtended() & ENTITY_FLAG_EXTENDED_CAN_COLLIDE_WITH_MERGED_MESHES
) != 0))
89 UpdateParameterValues(pEntity
, iFoundPair
->second
);
98 ///////////////////////////////////////////////////////////////////////////
99 uint64
CRuntimeAreaObject::GetEventMask() const
101 return BIT64(ENTITY_EVENT_ENTERAREA
) | BIT64(ENTITY_EVENT_LEAVEAREA
) | BIT64(ENTITY_EVENT_MOVEINSIDEAREA
);
104 ///////////////////////////////////////////////////////////////////////////
105 void CRuntimeAreaObject::GetMemoryUsage(ICrySizer
* pSizer
) const
107 pSizer
->AddObject(this, sizeof(*this));
110 void CRuntimeAreaObject::OnShutDown()
112 // Stop all of the currently playing sounds controlled by this RuntimeAreaObject instance.
113 for (TEntitySoundsMap::iterator iEntityData
= m_activeEntitySounds
.begin(),
114 iEntityDataEnd
= m_activeEntitySounds
.end(); iEntityData
!= iEntityDataEnd
; ++iEntityData
)
116 StopEntitySounds(iEntityData
->first
, iEntityData
->second
);
120 //////////////////////////////////////////////////////////////////////////
121 void CRuntimeAreaObject::UpdateParameterValues(IEntity
* const pEntity
, TAudioParameterMap
& paramMap
)
123 static float const fParamEpsilon
= 0.001f
;
124 static float const fMaxDensity
= 256.0f
;
126 IEntityAudioComponent
* const pAudioProxy
= pEntity
->GetOrCreateComponent
<IEntityAudioComponent
>();
127 if (pAudioProxy
!= NULL
)
129 ISurfaceType
* aSurfaceTypes
[MMRM_MAX_SURFACE_TYPES
];
130 memset(aSurfaceTypes
, 0x0, sizeof(aSurfaceTypes
));
132 float aDensities
[MMRM_MAX_SURFACE_TYPES
];
133 memset(aDensities
, 0x0, sizeof(aDensities
));
135 gEnv
->p3DEngine
->GetIMergedMeshesManager()->QueryDensity(pEntity
->GetPos(), aSurfaceTypes
, aDensities
);
137 for (int i
= 0; i
< MMRM_MAX_SURFACE_TYPES
&& (aSurfaceTypes
[i
] != NULL
); ++i
)
139 float const fNewParamValue
= aDensities
[i
] / fMaxDensity
;
140 TSurfaceCRC
const nSurfaceCrc
= CCrc32::ComputeLowercase(aSurfaceTypes
[i
]->GetName());
142 TAudioParameterMap::iterator iSoundPair
= paramMap
.find(nSurfaceCrc
);
143 if (iSoundPair
== paramMap
.end())
145 if (fNewParamValue
> 0.0f
)
147 // The sound for this surface is not yet playing on this entity, needs to be started.
148 TAudioControlMap::const_iterator
const iAudioControls
= m_audioControls
.find(nSurfaceCrc
);
149 if (iAudioControls
!= m_audioControls
.end())
151 SAudioControls
const& rAudioControls
= iAudioControls
->second
;
153 pAudioProxy
->SetParameter(rAudioControls
.audioRtpcId
, fNewParamValue
);
154 pAudioProxy
->ExecuteTrigger(rAudioControls
.audioTriggerId
);
157 std::pair
<TSurfaceCRC
, SAreaSoundInfo
>(
159 SAreaSoundInfo(rAudioControls
, fNewParamValue
)));
165 SAreaSoundInfo
& oSoundInfo
= iSoundPair
->second
;
166 if (fabs_tpl(fNewParamValue
- oSoundInfo
.parameter
) >= fParamEpsilon
)
168 oSoundInfo
.parameter
= fNewParamValue
;
169 pAudioProxy
->SetParameter(oSoundInfo
.audioControls
.audioRtpcId
, oSoundInfo
.parameter
);
176 ///////////////////////////////////////////////////////////////////////////
177 void CRuntimeAreaObject::StopEntitySounds(EntityId
const entityId
, TAudioParameterMap
& paramMap
)
179 IEntity
* const pEntity
= gEnv
->pEntitySystem
->GetEntity(entityId
);
182 IEntityAudioComponent
* const pAudioProxy
= pEntity
->GetOrCreateComponent
<IEntityAudioComponent
>();
183 if (pAudioProxy
!= NULL
)
185 for (TAudioParameterMap::const_iterator iSoundPair
= paramMap
.begin(), iSoundPairEnd
= paramMap
.end(); iSoundPair
!= iSoundPairEnd
; ++iSoundPair
)
187 pAudioProxy
->StopTrigger(iSoundPair
->second
.audioControls
.audioTriggerId
);
188 pAudioProxy
->SetParameter(iSoundPair
->second
.audioControls
.audioRtpcId
, 0.0f
);