GemComponent.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 <AzFramework/Physics/RigidBodyBus.h>
  9. #include <Source/AutoGen/GemComponent.AutoComponent.h>
  10. #if AZ_TRAIT_SERVER
  11. #include <AzFramework/Spawnable/SpawnableEntitiesInterface.h>
  12. #include <Source/Components/Multiplayer/GemSpawnerComponent.h>
  13. #endif
  14. namespace MultiplayerSample
  15. {
  16. //! @brief Gems have a physical static body on the server with a trigger volume
  17. //! On clients, gems do not have physics and have a local bouncing effect.
  18. class GemComponent
  19. : public GemComponentBase
  20. , public Physics::RigidBodyNotificationBus::Handler
  21. {
  22. public:
  23. AZ_MULTIPLAYER_COMPONENT(MultiplayerSample::GemComponent, s_gemComponentConcreteUuid, MultiplayerSample::GemComponentBase);
  24. static void Reflect(AZ::ReflectContext* context);
  25. void OnActivate(Multiplayer::EntityIsMigrating entityIsMigrating) override;
  26. void OnDeactivate(Multiplayer::EntityIsMigrating entityIsMigrating) override;
  27. //! RigidBodyNotificationBus overrides ...
  28. //! @{
  29. void OnPhysicsEnabled(const AZ::EntityId& entityId) override;
  30. //! }@
  31. private:
  32. // Animate the gem on clients without spending network traffic. (The gem will not spin on the authority server.)
  33. void ClientAnimationTick();
  34. AZ::ScheduledEvent m_clientAnimationEvent{ [this]()
  35. {
  36. ClientAnimationTick();
  37. }, AZ::Name("GemComponent") };
  38. void OnNetworkLocationChanged(const AZ::Vector3& location);
  39. AZ::Event<AZ::Vector3>::Handler m_networkLocationHandler{ [this](const AZ::Vector3& location)
  40. {
  41. OnNetworkLocationChanged(location);
  42. } };
  43. AZ::Vector3 m_rootLocation = AZ::Vector3::CreateZero();
  44. AZ::TimeMs m_lifetime = AZ::Time::ZeroTimeMs;
  45. };
  46. class GemComponentController
  47. : public GemComponentControllerBase
  48. {
  49. public:
  50. explicit GemComponentController(GemComponent& parent);
  51. void OnActivate(Multiplayer::EntityIsMigrating entityIsMigrating) override;
  52. void OnDeactivate(Multiplayer::EntityIsMigrating entityIsMigrating) override;
  53. #if AZ_TRAIT_SERVER
  54. void SetGemSpawnerController(GemSpawnerComponentController* controller);
  55. void HandleRPC_CollectedByPlayer(AzNetworking::IConnection* invokingConnection) override;
  56. private:
  57. GemSpawnerComponentController* m_controller = nullptr;
  58. #endif
  59. };
  60. }