NetworkPlayerMovementComponent.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 <Source/AutoGen/NetworkPlayerMovementComponent.AutoComponent.h>
  9. #include <Source/Components/NetworkAiComponent.h>
  10. #include <StartingPointInput/InputEventNotificationBus.h>
  11. #include <AzFramework/Physics/CharacterBus.h>
  12. namespace MultiplayerSample
  13. {
  14. // Input Event Ids for Player Controls
  15. const StartingPointInput::InputEventNotificationId MoveFwdEventId("move_fwd");
  16. const StartingPointInput::InputEventNotificationId MoveBackEventId("move_back");
  17. const StartingPointInput::InputEventNotificationId MoveLeftEventId("move_left");
  18. const StartingPointInput::InputEventNotificationId MoveRightEventId("move_right");
  19. const StartingPointInput::InputEventNotificationId SprintEventId("sprint");
  20. const StartingPointInput::InputEventNotificationId JumpEventId("jump");
  21. const StartingPointInput::InputEventNotificationId CrouchEventId("crouch");
  22. const StartingPointInput::InputEventNotificationId LookLeftRightEventId("lookLeftRight");
  23. const StartingPointInput::InputEventNotificationId LookUpDownEventId("lookUpDown");
  24. const StartingPointInput::InputEventNotificationId ZoomInEventId("zoomIn");
  25. const StartingPointInput::InputEventNotificationId ZoomOutEventId("zoomOut");
  26. class NetworkPlayerMovementComponentController
  27. : public NetworkPlayerMovementComponentControllerBase
  28. , private StartingPointInput::InputEventNotificationBus::MultiHandler
  29. , protected Physics::CharacterNotificationBus::Handler
  30. {
  31. public:
  32. NetworkPlayerMovementComponentController(NetworkPlayerMovementComponent& parent);
  33. //! NetworkPlayerMovementComponentControllerBase
  34. //! @{
  35. void OnActivate(Multiplayer::EntityIsMigrating entityIsMigrating) override;
  36. void OnDeactivate(Multiplayer::EntityIsMigrating entityIsMigrating) override;
  37. void CreateInput(Multiplayer::NetworkInput& input, float deltaTime) override;
  38. void ProcessInput(Multiplayer::NetworkInput& input, float deltaTime) override;
  39. #if AZ_TRAIT_SERVER
  40. void HandleApplyImpulse(AzNetworking::IConnection* connection, const AZ::Vector3& impulse, const bool& external) override;
  41. void HandleSetVelocity(AzNetworking::IConnection* connection, const AZ::Vector3& velocity, const bool& external) override;
  42. #endif
  43. //! @}
  44. protected:
  45. void OnCharacterActivated(const AZ::EntityId& entityId) override;
  46. private:
  47. friend class NetworkAiComponentController;
  48. void UpdateVelocity(const NetworkPlayerMovementComponentNetworkInput& playerInput, float deltaTime, bool& jumpTriggered, bool& movingDownward);
  49. float NormalizeHeading(float heading) const;
  50. AZ::Vector3 GetSlopeHeading(float targetHeading) const;
  51. //! AZ::InputEventNotificationBus interface
  52. //! @{
  53. void OnPressed(float value) override;
  54. void OnReleased(float value) override;
  55. void OnHeld(float value) override;
  56. //! @}
  57. #if AZ_TRAIT_SERVER
  58. void UpdateAI();
  59. AZ::ScheduledEvent m_updateAI;
  60. NetworkAiComponentController* m_networkAiComponentController = nullptr;
  61. #endif
  62. #if AZ_TRAIT_CLIENT
  63. void UpdateLocalBot();
  64. AZ::ScheduledEvent m_updateLocalBot;
  65. AZ::SimpleLcgRandom m_botLcg;
  66. float m_botRemainingTime;
  67. float m_botTurnRate;
  68. float m_botTargetYawDelta;
  69. float m_botTargetPitchDelta;
  70. Action m_botAction;
  71. bool m_botStrafingRight;
  72. #endif
  73. // Technically these values should never migrate hosts since they are maintained by the autonomous client
  74. // But due to how the stress test chaos monkey operates, it puppets these values on the server to mimic a client
  75. // This means these values can and will migrate between hosts (and lose any stored state)
  76. // We will need to consider moving these values to Authority to Server network properties if the design doesn't change
  77. float m_forwardWeight = 0.0f;
  78. float m_leftWeight = 0.0f;
  79. float m_backwardWeight = 0.0f;
  80. float m_rightWeight = 0.0f;
  81. float m_viewYaw = 0.0f;
  82. float m_viewPitch = 0.0f;
  83. bool m_toggleSprint = false;
  84. bool m_forwardDown = false;
  85. bool m_leftDown = false;
  86. bool m_backwardDown = false;
  87. bool m_rightDown = false;
  88. bool m_sprinting = false;
  89. bool m_jumping = false;
  90. bool m_crouching = false;
  91. bool m_aiEnabled = false;
  92. float m_gravity = -9.81f;
  93. float m_gravityMultiplier = 1.0f;
  94. float m_stepHeight = 0.1f;
  95. float m_radius = 0.3f;
  96. };
  97. }