GameEffect.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #pragma once
  9. #include <AzCore/Asset/AssetCommon.h>
  10. #include <AzCore/Asset/AssetSerializer.h>
  11. #include <AzCore/std/string/fixed_string.h>
  12. #include <AzCore/Serialization/EditContext.h>
  13. #include <AzCore/Serialization/SerializeContext.h>
  14. #include <AzCore/Math/Transform.h>
  15. #if AZ_TRAIT_CLIENT
  16. # include <IAudioSystem.h>
  17. #endif
  18. namespace PopcornFX
  19. {
  20. struct StandaloneEmitter;
  21. class PopcornFXRequests;
  22. }
  23. namespace MultiplayerSample
  24. {
  25. //! A class containing a singular game effect, consisting of a graphical particle effect and an attached sound trigger.
  26. class GameEffect final
  27. {
  28. public:
  29. enum class EmitterType
  30. {
  31. // Fire-and-forget emitters will create a new emitter on each TriggerEffect and won't be tracked by GameEffect,
  32. // so they will always run until they're done and then get auto-removed by PopcornFX.
  33. FireAndForget,
  34. // Reusable emitters create a single emitter per GameEffect and will reuse the emitter on each TriggerEffect.
  35. // The emitter will be immediately destroyed when the GameEffect is destroyed.
  36. ReusableEmitter
  37. };
  38. AZ_TYPE_INFO(GameEffect, "{E9A6959E-C52A-4BCF-907A-C880C2BD94F0}");
  39. static void Reflect(AZ::ReflectContext* context);
  40. GameEffect() = default;
  41. GameEffect(const GameEffect& gameEffect);
  42. GameEffect& operator=(const GameEffect& gameEffect);
  43. ~GameEffect();
  44. //! Initializes the effect emitter.
  45. void Initialize(EmitterType emitterType = EmitterType::ReusableEmitter);
  46. //! Destroys the effect emitter;
  47. void Destroy();
  48. //! True if the effect is initialized, false if it isn't.
  49. bool IsInitialized() const;
  50. //! Setters for setting custom effect attributes.
  51. //! These only work for reusable emitters because we don't track the emitter pointer for fire-and-forget emitters.
  52. //! @{
  53. bool SetAttribute(const char* attributeName, float value) const;
  54. bool SetAttribute(const char* attributeName, const AZ::Vector2& value) const;
  55. bool SetAttribute(const char* attributeName, const AZ::Vector3& value) const;
  56. bool SetAttribute(const char* attributeName, const AZ::Vector4& value) const;
  57. //! @}
  58. //! Triggers the attached effect at the provided transform.
  59. //! @param transform the root transform to move the effect to prior to triggering
  60. void TriggerEffect(const AZ::Transform& transform) const;
  61. //! Stops the attached effect if it's executing.
  62. void StopEffect() const;
  63. //! Returns the configured effect offset.
  64. //! @return the effect offset
  65. const AZ::Vector3& GetEffectOffset() const;
  66. private:
  67. AZ::Data::AssetId m_particleAssetId; // The particle effect to play upon effect activation
  68. AZStd::string m_audioTrigger; // The name of the audio trigger to use on effect activation
  69. AZ::Vector3 m_effectOffset = AZ::Vector3::CreateZero(); // The offset to use when triggering an effect
  70. // Tracks whether to reuse the emitter or to fire-and-forget each effect trigger.
  71. [[maybe_unused]] EmitterType m_emitterType = EmitterType::ReusableEmitter;
  72. #if AZ_TRAIT_CLIENT
  73. PopcornFX::StandaloneEmitter* m_emitter = nullptr;
  74. Audio::IAudioProxy* m_audioProxy = nullptr;
  75. Audio::TATLIDType m_audioTriggerId = INVALID_AUDIO_CONTROL_ID;
  76. PopcornFX::PopcornFXRequests* m_popcornFx = nullptr;
  77. Audio::IAudioSystem* m_audioSystem = nullptr;
  78. #endif
  79. };
  80. }