CharacterComponent.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 <Source/Components/CharacterComponent.h>
  8. #include <AzFramework/Physics/CharacterBus.h>
  9. #include <AzFramework/Physics/Character.h>
  10. #include <Multiplayer/Components/NetworkTransformComponent.h>
  11. #include <Multiplayer/NetworkTime/INetworkTime.h>
  12. namespace MultiplayerSample
  13. {
  14. void CharacterComponent::CharacterComponent::Reflect(AZ::ReflectContext* context)
  15. {
  16. AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context);
  17. if (serializeContext)
  18. {
  19. serializeContext->Class<CharacterComponent, CharacterComponentBase>()
  20. ->Version(1);
  21. }
  22. CharacterComponentBase::Reflect(context);
  23. }
  24. CharacterComponent::CharacterComponent()
  25. : m_translationEventHandler([this](const AZ::Vector3& translation) { OnTranslationChangedEvent(translation); })
  26. {
  27. ;
  28. }
  29. void CharacterComponent::OnInit()
  30. {
  31. ;
  32. }
  33. void CharacterComponent::OnActivate([[maybe_unused]] Multiplayer::EntityIsMigrating entityIsMigrating)
  34. {
  35. Physics::CharacterRequests* characterRequests = Physics::CharacterRequestBus::FindFirstHandler(GetEntityId());
  36. m_physicsCharacter = (characterRequests != nullptr) ? characterRequests->GetCharacter() : nullptr;
  37. GetNetBindComponent()->AddEntitySyncRewindEventHandler(m_syncRewindHandler);
  38. if (!HasController())
  39. {
  40. GetNetworkTransformComponent()->TranslationAddEvent(m_translationEventHandler);
  41. }
  42. }
  43. void CharacterComponent::OnDeactivate([[maybe_unused]] Multiplayer::EntityIsMigrating entityIsMigrating)
  44. {
  45. ;
  46. }
  47. void CharacterComponent::OnTranslationChangedEvent([[maybe_unused]] const AZ::Vector3& translation)
  48. {
  49. OnSyncRewind();
  50. }
  51. void CharacterComponent::OnSyncRewind()
  52. {
  53. if (m_physicsCharacter == nullptr)
  54. {
  55. return;
  56. }
  57. const AZ::Vector3 currPosition = m_physicsCharacter->GetBasePosition();
  58. if (!currPosition.IsClose(GetNetworkTransformComponent()->GetTranslation()))
  59. {
  60. uint32_t frameId = static_cast<uint32_t>(Multiplayer::GetNetworkTime()->GetHostFrameId());
  61. m_physicsCharacter->SetFrameId(frameId);
  62. //m_physicsCharacter->SetBasePosition(GetNetworkTransformComponent()->GetTranslation());
  63. }
  64. }
  65. CharacterComponentController::CharacterComponentController(CharacterComponent& parent)
  66. : CharacterComponentControllerBase(parent)
  67. {
  68. ;
  69. }
  70. void CharacterComponentController::OnActivate([[maybe_unused]] Multiplayer::EntityIsMigrating entityIsMigrating)
  71. {
  72. ;
  73. }
  74. void CharacterComponentController::OnDeactivate([[maybe_unused]] Multiplayer::EntityIsMigrating entityIsMigrating)
  75. {
  76. ;
  77. }
  78. AZ::Vector3 CharacterComponentController::TryMoveWithVelocity(const AZ::Vector3& velocity, float deltaTime)
  79. {
  80. if ((GetParent().m_physicsCharacter == nullptr) || (velocity.GetLengthSq() <= 0.0f))
  81. {
  82. return GetEntity()->GetTransform()->GetWorldTranslation();
  83. }
  84. GetParent().m_physicsCharacter->AddVelocity(velocity);
  85. GetParent().m_physicsCharacter->ApplyRequestedVelocity(deltaTime);
  86. GetEntity()->GetTransform()->SetWorldTranslation(GetParent().m_physicsCharacter->GetBasePosition());
  87. AZLOG
  88. (
  89. NET_Movement,
  90. "Moved to position %f x %f x %f",
  91. GetParent().m_physicsCharacter->GetBasePosition().GetX(),
  92. GetParent().m_physicsCharacter->GetBasePosition().GetY(),
  93. GetParent().m_physicsCharacter->GetBasePosition().GetZ()
  94. );
  95. return GetEntity()->GetTransform()->GetWorldTranslation();
  96. }
  97. }