NetworkTeleportCompatibleComponent.cpp 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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
  3. * this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <Source/Components/NetworkTeleportCompatibleComponent.h>
  9. #include <Multiplayer/Components/NetworkTransformComponent.h>
  10. #include <AzCore/Component/TransformBus.h>
  11. #include <AzCore/Serialization/SerializeContext.h>
  12. #include <AzFramework/Physics/Components/SimulatedBodyComponentBus.h>
  13. namespace MultiplayerSample
  14. {
  15. void NetworkTeleportCompatibleComponent::Reflect(AZ::ReflectContext* context)
  16. {
  17. AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context);
  18. if (serializeContext)
  19. {
  20. serializeContext->Class<NetworkTeleportCompatibleComponent, NetworkTeleportCompatibleComponentBase>()
  21. ->Version(1);
  22. }
  23. NetworkTeleportCompatibleComponentBase::Reflect(context);
  24. }
  25. void NetworkTeleportCompatibleComponent::OnActivate([[maybe_unused]] Multiplayer::EntityIsMigrating entityIsMigrating)
  26. {
  27. #if AZ_TRAIT_CLIENT
  28. m_effect = GetTeleportEffect();
  29. m_effect.Initialize();
  30. #endif
  31. }
  32. void NetworkTeleportCompatibleComponent::OnDeactivate([[maybe_unused]] Multiplayer::EntityIsMigrating entityIsMigrating)
  33. {
  34. #if AZ_TRAIT_CLIENT
  35. // Clean up the teleport effect emitter.
  36. m_effect = {};
  37. #endif
  38. }
  39. #if AZ_TRAIT_CLIENT
  40. void NetworkTeleportCompatibleComponent::HandleNotifyTeleport([[maybe_unused]] AzNetworking::IConnection* invokingConnection, const AZ::Vector3& teleportedLocation)
  41. {
  42. const AZ::Transform transform = AZ::Transform::CreateFromQuaternionAndTranslation(AZ::Quaternion::CreateIdentity(), teleportedLocation);
  43. m_effect.TriggerEffect(transform);
  44. }
  45. #endif
  46. // Controller
  47. NetworkTeleportCompatibleComponentController::NetworkTeleportCompatibleComponentController(NetworkTeleportCompatibleComponent& parent)
  48. : NetworkTeleportCompatibleComponentControllerBase(parent)
  49. {
  50. }
  51. #if AZ_TRAIT_SERVER
  52. void NetworkTeleportCompatibleComponentController::HandleTeleport(
  53. [[maybe_unused]] AzNetworking::IConnection* invokingConnection, [[maybe_unused]] const AZ::Vector3& teleportedLocation)
  54. {
  55. AZ::Entity* self = GetEntity();
  56. AZ_TracePrintf("TeleportCompatibleComponent", "Teleporting entity %s to (%f,%f)\n",
  57. self->GetName().c_str(),
  58. teleportedLocation.GetX(),
  59. teleportedLocation.GetY());
  60. AZ::EntityId selfId = self->GetId();
  61. // disable physics (needed to move rigid bodies)
  62. // see: https://github.com/o3de/o3de/issues/2541
  63. AzPhysics::SimulatedBodyComponentRequestsBus::Event(selfId, &AzPhysics::SimulatedBodyComponentRequestsBus::Events::DisablePhysics);
  64. // move self and increment resetCount to prevent transform interpolation
  65. AZ::TransformBus::Event(selfId,
  66. &AZ::TransformBus::Events::SetWorldTranslation, teleportedLocation);
  67. Multiplayer::NetworkTransformComponentController* netTransform = GetNetworkTransformComponentController();
  68. netTransform->SetResetCount(netTransform->GetResetCount() + 1);
  69. // re-enable physics
  70. AzPhysics::SimulatedBodyComponentRequestsBus::Event(selfId, &AzPhysics::SimulatedBodyComponentRequestsBus::Events::EnablePhysics);
  71. NotifyTeleport(teleportedLocation);
  72. }
  73. #endif
  74. } // namespace MultiplayerSample