GemSpawnerComponent.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. * 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.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0 OR MIT
  5. *
  6. */
  7. #pragma once
  8. #include <AzCore/Component/EntityBus.h>
  9. #include <AzFramework/Spawnable/SpawnableEntitiesInterface.h>
  10. #include <LmbrCentral/Scripting/TagComponentBus.h>
  11. #include <Source/AutoGen/GemSpawnerComponent.AutoComponent.h>
  12. namespace MultiplayerSample
  13. {
  14. class GemSpawnerComponent
  15. : public GemSpawnerComponentBase
  16. {
  17. public:
  18. AZ_MULTIPLAYER_COMPONENT(MultiplayerSample::GemSpawnerComponent, s_gemSpawnerComponentConcreteUuid, MultiplayerSample::GemSpawnerComponentBase);
  19. static void Reflect(AZ::ReflectContext* context);
  20. void OnActivate([[maybe_unused]] Multiplayer::EntityIsMigrating entityIsMigrating) override {}
  21. void OnDeactivate([[maybe_unused]] Multiplayer::EntityIsMigrating entityIsMigrating) override {}
  22. };
  23. class GemSpawnerComponentController
  24. : public GemSpawnerComponentControllerBase
  25. {
  26. public:
  27. explicit GemSpawnerComponentController(GemSpawnerComponent& parent);
  28. void OnActivate(Multiplayer::EntityIsMigrating entityIsMigrating) override;
  29. void OnDeactivate(Multiplayer::EntityIsMigrating entityIsMigrating) override;
  30. #if AZ_TRAIT_SERVER
  31. void SpawnGems();
  32. void SpawnGem(const AZ::Vector3& location, const AZ::Crc32& type);
  33. void RemoveGem(AzFramework::EntitySpawnTicket::Id gemTicketId);
  34. void RemoveGems();
  35. void HandleRPC_SpawnGem(
  36. AzNetworking::IConnection* invokingConnection, const Multiplayer::NetEntityId& playerEntity,
  37. const AZ::Vector3& spawnLocation, const AZStd::string& gemTag) override;
  38. void HandleRPC_SpawnGemWithValue(
  39. AzNetworking::IConnection* invokingConnection, const Multiplayer::NetEntityId& playerEntity,
  40. const AZ::Vector3& spawnLocation, const AZStd::string& gemTag, const uint16_t& gemValue) override;
  41. #endif
  42. private:
  43. #if AZ_TRAIT_SERVER
  44. AZStd::optional<const GemSpawnable> GetGemSpawnable(AZ::Crc32 gemTag) const;
  45. void SpawnGem(const AZ::Vector3& location, const AzFramework::SpawnableAsset& gemAsset, uint16_t gemValue);
  46. #endif
  47. AZStd::unordered_map<AzFramework::EntitySpawnTicket::Id, AZStd::shared_ptr<AzFramework::EntitySpawnTicket>> m_spawnedGems;
  48. struct GemSpawnEntry
  49. {
  50. AZ::Crc32 m_tag;
  51. float m_weight;
  52. bool m_entityHasTag;
  53. GemSpawnEntry(AZ::Crc32 tag, float weight, bool entityHasTag)
  54. : m_tag(tag), m_weight(weight), m_entityHasTag(entityHasTag)
  55. {
  56. }
  57. };
  58. //! Randomly choose a gem type from the given set of weights for the current round.
  59. AZ::Crc32 ChooseGemType(AZStd::vector<GemSpawnEntry>& gemSpawnList, const LmbrCentral::Tags& tags);
  60. };
  61. }