NetworkTeleportCompatibleComponent.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. #if AZ_TRAIT_CLIENT
  33. void NetworkTeleportCompatibleComponent::HandleNotifyTeleport([[maybe_unused]] AzNetworking::IConnection* invokingConnection, const AZ::Vector3& teleportedLocation)
  34. {
  35. const AZ::Transform transform = AZ::Transform::CreateFromQuaternionAndTranslation(AZ::Quaternion::CreateIdentity(), teleportedLocation);
  36. m_effect.TriggerEffect(transform);
  37. }
  38. #endif
  39. // Controller
  40. NetworkTeleportCompatibleComponentController::NetworkTeleportCompatibleComponentController(NetworkTeleportCompatibleComponent& parent)
  41. : NetworkTeleportCompatibleComponentControllerBase(parent)
  42. {
  43. }
  44. #if AZ_TRAIT_SERVER
  45. void NetworkTeleportCompatibleComponentController::HandleTeleport(
  46. [[maybe_unused]] AzNetworking::IConnection* invokingConnection, [[maybe_unused]] const AZ::Vector3& teleportedLocation)
  47. {
  48. AZ::Entity* self = GetEntity();
  49. AZ_TracePrintf("TeleportCompatibleComponent", "Teleporting entity %s to (%f,%f)\n",
  50. self->GetName().c_str(),
  51. teleportedLocation.GetX(),
  52. teleportedLocation.GetY());
  53. AZ::EntityId selfId = self->GetId();
  54. // disable physics (needed to move rigid bodies)
  55. // see: https://github.com/o3de/o3de/issues/2541
  56. AzPhysics::SimulatedBodyComponentRequestsBus::Event(selfId, &AzPhysics::SimulatedBodyComponentRequestsBus::Events::DisablePhysics);
  57. // move self and increment resetCount to prevent transform interpolation
  58. AZ::TransformBus::Event(selfId,
  59. &AZ::TransformBus::Events::SetWorldTranslation, teleportedLocation);
  60. Multiplayer::NetworkTransformComponentController* netTransform = GetNetworkTransformComponentController();
  61. netTransform->SetResetCount(netTransform->GetResetCount() + 1);
  62. // re-enable physics
  63. AzPhysics::SimulatedBodyComponentRequestsBus::Event(selfId, &AzPhysics::SimulatedBodyComponentRequestsBus::Events::EnablePhysics);
  64. NotifyTeleport(teleportedLocation);
  65. }
  66. #endif
  67. } // namespace MultiplayerSample