EntityAudioComponent.h
Code: Select all
#pragma once
#include <CryAudio/IAudioInterfacesCommonData.h>
#include <CrySerialization/Forward.h>
#include <CrySchematyc/Reflection/TypeDesc.h>
#include <CrySchematyc/Env/IEnvRegistrar.h>
#include <CryString/CryString.h>
struct SEntityAudioComponent final : public IEntityComponent
{
public:
SEntityAudioComponent::SEntityAudioComponent(){}
virtual ~SEntityAudioComponent() {}
virtual void Initialize() override;
inline void ReflectType(Schematyc::CTypeDesc<SEntityAudioComponent>& desc)
{
desc.SetGUID("{924B2A79-B026-4592-B215-1CBFB1BC8169}"_cry_guid);
}
void PlayByName(string sSoundName);
void StopByName(string sSoundName);
IEntityAudioComponent *pAudioComponent = nullptr;
CryAudio::AuxObjectId auxObjectId = CryAudio::InvalidAuxObjectId;
};
Code: Select all
#include "StdAfx.h"
#include "EntityAudioComponent.h"
void SEntityAudioComponent::Initialize()
{
pAudioComponent = m_pEntity->CreateComponent<IEntityAudioComponent>();
CRY_ASSERT(auxObjectId == CryAudio::InvalidAuxObjectId);
auxObjectId = CryAudio::DefaultAuxObjectId;
}
void SEntityAudioComponent::PlayByName(string sSoundName)
{
CryAudio::ControlId audioControlId = CryAudio::StringToId(sSoundName);
if (pAudioComponent != nullptr && audioControlId != CryAudio::InvalidControlId)
{
CryAudio::SRequestUserData const userData(CryAudio::ERequestFlags::DoneCallbackOnExternalThread | CryAudio::ERequestFlags::DoneCallbackOnExternalThread, this);
pAudioComponent->ExecuteTrigger(audioControlId, auxObjectId, userData);
}
}
void SEntityAudioComponent::StopByName(string sSoundName)
{
CryAudio::ControlId audioControlId = CryAudio::StringToId(sSoundName);
if (pAudioComponent != nullptr && audioControlId != CryAudio::InvalidControlId)
{
CryAudio::SRequestUserData const userData(CryAudio::ERequestFlags::DoneCallbackOnExternalThread | CryAudio::ERequestFlags::DoneCallbackOnExternalThread, this);
pAudioComponent->StopTrigger(audioControlId, auxObjectId, userData);
}
}