123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222 |
- /*
- * Copyright (c) Contributors to the Open 3D Engine Project.
- * For complete copyright and license terms please see the LICENSE at the root of this distribution.
- *
- * SPDX-License-Identifier: Apache-2.0 OR MIT
- *
- */
- #include <Source/Effects/GameEffect.h>
- #include <AzCore/Console/IConsole.h>
- #if AZ_TRAIT_CLIENT
- # include <PopcornFX/PopcornFXBus.h>
- #endif
- namespace MultiplayerSample
- {
- AZ_CVAR(bool, cl_KillEffectOnRestart, false, nullptr, AZ::ConsoleFunctorFlags::Null, "Controls whether or not to kill current effects on restart");
- void GameEffect::Reflect(AZ::ReflectContext* context)
- {
- AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context);
- if (serializeContext)
- {
- serializeContext->Class<GameEffect>()
- ->Version(1)
- ->Field("ParticleAsset", &GameEffect::m_particleAssetId)
- ->Field("AudioTrigger", &GameEffect::m_audioTrigger)
- ->Field("EffectOffset", &GameEffect::m_effectOffset);
- AZ::EditContext* editContext = serializeContext->GetEditContext();
- if (editContext)
- {
- editContext->Class<GameEffect>("GameEffect", "A single game effect, consisting of a particle effect and a sound trigger pair")
- ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
- ->DataElement(AZ::Edit::UIHandlers::Default, &GameEffect::m_particleAssetId, "ParticleAsset", "The particle effect to play upon effect trigger")
- #if AZ_TRAIT_CLIENT
- ->Attribute(AZ_CRC_CE("SupportedAssetTypes"), []() { return AZStd::vector<AZ::Data::AssetType>({ PopcornFX::AssetTypeId }); })
- #endif
- ->DataElement(AZ::Edit::UIHandlers::Default, &GameEffect::m_audioTrigger, "AudioTrigger", "The audio trigger name of the sound to play upon effect trigger")
- ->DataElement(AZ::Edit::UIHandlers::Default, &GameEffect::m_effectOffset, "EffectOffset", "The offset to apply when triggering an effect");
- }
- }
- }
- GameEffect::~GameEffect()
- {
- #if AZ_TRAIT_CLIENT
- if (m_popcornFx != nullptr)
- {
- if (m_popcornFx->IsEffectAlive(m_emitter))
- {
- m_popcornFx->DestroyEffect(m_emitter);
- }
- m_emitter = nullptr;
- }
- if (m_audioSystem != nullptr)
- {
- m_audioTriggerId = INVALID_AUDIO_CONTROL_ID;
- if (m_audioProxy != nullptr)
- {
- m_audioSystem->RecycleAudioProxy(m_audioProxy);
- m_audioProxy = nullptr;
- }
- }
- #endif
- }
- void GameEffect::Initialize()
- {
- #if AZ_TRAIT_CLIENT
- m_popcornFx = PopcornFX::PopcornFXRequestBus::FindFirstHandler();
- m_audioSystem = AZ::Interface<Audio::IAudioSystem>::Get();
- if (m_popcornFx != nullptr)
- {
- if (m_particleAssetId.IsValid())
- {
- const PopcornFX::SpawnParams params = PopcornFX::SpawnParams(true, false, AZ::Transform::CreateIdentity());
- m_emitter = m_popcornFx->SpawnEffectById(m_particleAssetId, params);
- }
- }
- if (m_audioSystem != nullptr)
- {
- m_audioProxy = m_audioSystem->GetAudioProxy();
- m_audioProxy->Initialize(m_audioTrigger.c_str(), this);
- m_audioProxy->SetObstructionCalcType(Audio::ObstructionType::Ignore);
- m_audioTriggerId = m_audioSystem->GetAudioTriggerID(m_audioTrigger.c_str());
- }
- #endif
- }
- bool GameEffect::SetAttribute([[maybe_unused]] const char* attributeName, [[maybe_unused]] float value) const
- {
- #if AZ_TRAIT_CLIENT
- if (m_popcornFx != nullptr)
- {
- if (m_popcornFx->IsEffectAlive(m_emitter))
- {
- int32_t attrId = m_popcornFx->EffectGetAttributeId(m_emitter, attributeName);
- if (attrId >= 0)
- {
- return m_popcornFx->EffectSetAttributeAsFloat(m_emitter, attrId, value);
- }
- }
- else
- {
- AZ_Assert(false, "Setting attribute on an emitter that isn't active.");
- return false;
- }
- }
- #endif
- return false;
- }
- bool GameEffect::SetAttribute([[maybe_unused]] const char* attributeName, [[maybe_unused]] const AZ::Vector2& value) const
- {
- #if AZ_TRAIT_CLIENT
- if (m_popcornFx != nullptr)
- {
- if (m_popcornFx->IsEffectAlive(m_emitter))
- {
- int32_t attrId = m_popcornFx->EffectGetAttributeId(m_emitter, attributeName);
- if (attrId >= 0)
- {
- return m_popcornFx->EffectSetAttributeAsFloat2(m_emitter, attrId, value);
- }
- }
- else
- {
- AZ_Assert(false, "Setting attribute on an emitter that isn't active.");
- return false;
- }
- }
- #endif
- return false;
- }
- bool GameEffect::SetAttribute([[maybe_unused]] const char* attributeName, [[maybe_unused]] const AZ::Vector3& value) const
- {
- #if AZ_TRAIT_CLIENT
- if (m_popcornFx != nullptr)
- {
- if (m_popcornFx->IsEffectAlive(m_emitter))
- {
- int32_t attrId = m_popcornFx->EffectGetAttributeId(m_emitter, attributeName);
- if (attrId >= 0)
- {
- return m_popcornFx->EffectSetAttributeAsFloat3(m_emitter, attrId, value);
- }
- }
- else
- {
- AZ_Assert(false, "Setting attribute on an emitter that isn't active.");
- return false;
- }
- }
- #endif
- return false;
- }
- bool GameEffect::SetAttribute([[maybe_unused]] const char* attributeName, [[maybe_unused]] const AZ::Vector4& value) const
- {
- #if AZ_TRAIT_CLIENT
- if (m_popcornFx != nullptr)
- {
- if (m_popcornFx->IsEffectAlive(m_emitter))
- {
- int32_t attrId = m_popcornFx->EffectGetAttributeId(m_emitter, attributeName);
- if (attrId >= 0)
- {
- return m_popcornFx->EffectSetAttributeAsFloat4(m_emitter, attrId, value);
- }
- }
- else
- {
- AZ_Assert(false, "Setting attribute on an emitter that isn't active.");
- return false;
- }
- }
- #endif
- return false;
- }
- void GameEffect::TriggerEffect([[maybe_unused]] const AZ::Transform& transform) const
- {
- #if AZ_TRAIT_CLIENT
- const AZ::Vector3 offsetPosition = transform.TransformPoint(m_effectOffset);
- AZ::Transform transformOffset = transform;
- transformOffset.SetTranslation(offsetPosition);
- if (m_emitter != nullptr)
- {
- if (PopcornFX::PopcornFXRequests* popcornFx = PopcornFX::PopcornFXRequestBus::FindFirstHandler())
- {
- if (m_popcornFx->IsEffectAlive(m_emitter))
- {
- popcornFx->EffectSetTransform(m_emitter, transformOffset);
- popcornFx->EffectSetTeleportThisFrame(m_emitter);
- popcornFx->EffectRestart(m_emitter, cl_KillEffectOnRestart);
- }
- else
- {
- AZ_Assert(false, "Triggering an inactive emitter.");
- }
- }
- }
- if ((m_audioProxy != nullptr) && (m_audioTriggerId != INVALID_AUDIO_CONTROL_ID))
- {
- m_audioProxy->SetPosition(offsetPosition);
- m_audioProxy->ExecuteTrigger(m_audioTriggerId);
- }
- #endif
- }
- const AZ::Vector3& GameEffect::GetEffectOffset() const
- {
- return m_effectOffset;
- }
- }
|