GemComponent.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. #include <AzCore/Serialization/SerializeContext.h>
  8. #include <Components/Multiplayer/GemSpawnerComponent.h>
  9. #include <Multiplayer/Components/NetworkTransformComponent.h>
  10. #include <Source/Components/Multiplayer/GemComponent.h>
  11. namespace MultiplayerSample
  12. {
  13. void GemComponent::Reflect(AZ::ReflectContext* context)
  14. {
  15. AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context);
  16. if (serializeContext)
  17. {
  18. serializeContext->Class<GemComponent, GemComponentBase>()
  19. ->Version(1);
  20. }
  21. GemComponentBase::Reflect(context);
  22. }
  23. void GemComponent::OnActivate([[maybe_unused]] Multiplayer::EntityIsMigrating entityIsMigrating)
  24. {
  25. if (IsNetEntityRoleClient())
  26. {
  27. m_rootLocation = GetEntity()->GetTransform()->GetWorldTranslation();
  28. GetNetworkTransformComponent()->TranslationAddEvent(m_networkLocationHandler);
  29. // Tick on every frame.
  30. m_clientAnimationEvent.Enqueue(AZ::Time::ZeroTimeMs, true);
  31. // Physical bodies take time to enable after entity activation, so sign up for physics activation and disable it
  32. Physics::RigidBodyNotificationBus::Handler::BusConnect(GetEntityId());
  33. }
  34. }
  35. void GemComponent::OnDeactivate([[maybe_unused]] Multiplayer::EntityIsMigrating entityIsMigrating)
  36. {
  37. Physics::RigidBodyNotificationBus::Handler::BusDisconnect();
  38. m_clientAnimationEvent.RemoveFromQueue();
  39. m_networkLocationHandler.Disconnect();
  40. }
  41. void GemComponent::OnPhysicsEnabled([[maybe_unused]] const AZ::EntityId& entityId)
  42. {
  43. // @entityId should be this entity since we signed up for it when calling
  44. // Physics::RigidBodyNotificationBus::Handler::BusConnect
  45. Physics::RigidBodyNotificationBus::Handler::BusDisconnect();
  46. Physics::RigidBodyRequestBus::Event(GetEntityId(), &Physics::RigidBodyRequestBus::Events::DisablePhysics);
  47. }
  48. void GemComponent::ClientAnimationTick()
  49. {
  50. m_lifetime += m_clientAnimationEvent.TimeInQueueMs();
  51. auto updatedLocation = m_rootLocation;
  52. updatedLocation.SetZ(updatedLocation.GetZ() + 0.5f * GetVerticalAmplitude() * AZStd::sin(
  53. AZ::TimeMsToSeconds(m_lifetime + AZ::TimeMs{ GetRandomPeriodOffset() }) * AZ::Constants::TwoPi / GetVerticalBouncePeriod()));
  54. GetEntity()->GetTransform()->SetWorldTranslation(updatedLocation);
  55. const AZ::Quaternion rotation = AZ::Quaternion::CreateRotationZ(AZ::TimeMsToSeconds(
  56. m_lifetime + AZ::TimeMs{ GetRandomPeriodOffset() }) * GetAngularTurnSpeed());
  57. GetEntity()->GetTransform()->SetWorldRotationQuaternion(rotation);
  58. }
  59. void GemComponent::OnNetworkLocationChanged(const AZ::Vector3& location)
  60. {
  61. m_rootLocation = location;
  62. }
  63. GemComponentController::GemComponentController(GemComponent& parent)
  64. : GemComponentControllerBase(parent)
  65. {
  66. }
  67. void GemComponentController::OnActivate([[maybe_unused]] Multiplayer::EntityIsMigrating entityIsMigrating)
  68. {
  69. }
  70. void GemComponentController::OnDeactivate([[maybe_unused]] Multiplayer::EntityIsMigrating entityIsMigrating)
  71. {
  72. }
  73. #if AZ_TRAIT_SERVER
  74. void GemComponentController::HandleRPC_CollectedByPlayer([[maybe_unused]] AzNetworking::IConnection* invokingConnection)
  75. {
  76. if (m_controller)
  77. {
  78. m_controller->RemoveGem(GetEntity()->GetEntitySpawnTicketId());
  79. m_controller = nullptr;
  80. }
  81. }
  82. void GemComponentController::SetGemSpawnerController(GemSpawnerComponentController* controller)
  83. {
  84. m_controller = controller;
  85. }
  86. #endif
  87. }