MultiplayerController.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #pragma once
  9. #include <Multiplayer/NetworkEntity/NetworkEntityHandle.h>
  10. #include <AzCore/Math/Aabb.h>
  11. namespace Multiplayer
  12. {
  13. class NetworkInput;
  14. class NetBindComponent;
  15. class MultiplayerComponent;
  16. //! @class MultiplayerController
  17. //! @brief A base class for all multiplayer component controllers responsible for running local prediction logic.
  18. class MultiplayerController
  19. {
  20. public:
  21. enum class InputPriorityOrder
  22. {
  23. First = 0,
  24. Default = 1000,
  25. SubEntities = 90000,
  26. Last = 100000
  27. };
  28. MultiplayerController(MultiplayerComponent& owner);
  29. virtual ~MultiplayerController() = default;
  30. //! Activates the controller.
  31. virtual void Activate(EntityIsMigrating entityIsMigrating) = 0;
  32. //! Deactivates the controller.
  33. virtual void Deactivate(EntityIsMigrating entityIsMigrating) = 0;
  34. //! Returns the networkId for the entity that owns this controller.
  35. //! @return the networkId for the entity that owns this controller
  36. NetEntityId GetNetEntityId() const;
  37. //! Returns true if this controller has authority.
  38. //! @return boolean true if this controller has authority
  39. bool IsNetEntityRoleAuthority() const;
  40. //! Returns true if this controller has autonomy (can locally predict).
  41. //! @return boolean true if this controller has autonomy
  42. bool IsNetEntityRoleAutonomous() const;
  43. //! Returns the raw AZ::Entity pointer for the entity that owns this controller.
  44. //! @return the raw AZ::Entity pointer for the entity that owns this controller
  45. AZ::Entity* GetEntity() const;
  46. //! @return the AZ::EntityId of the entity that owns this controller
  47. AZ::EntityId GetEntityId() const;
  48. //! Returns the network entity handle for the entity that owns this controller.
  49. //! @return the network entity handle for the entity that owns this controller
  50. ConstNetworkEntityHandle GetEntityHandle() const;
  51. //! Returns the network entity handle for the entity that owns this controller.
  52. //! @return the network entity handle for the entity that owns this controller
  53. NetworkEntityHandle GetEntityHandle();
  54. protected:
  55. //! Returns the NetBindComponent responsible for net binding for this controller
  56. //! @{
  57. const NetBindComponent* GetNetBindComponent() const;
  58. NetBindComponent* GetNetBindComponent();
  59. //! @}
  60. //! Returns the MultiplayerComponent that owns this controller instance.
  61. //! @return the MultiplayerComponent that owns this controller instance
  62. //! @{
  63. const MultiplayerComponent& GetOwner() const;
  64. MultiplayerComponent& GetOwner();
  65. //! @}
  66. //! Returns true if the owning entity is currently inside ProcessInput scope.
  67. bool IsProcessingInput() const;
  68. //! Returns the input priority ordering for determining the order of ProcessInput or CreateInput functions.
  69. virtual InputPriorityOrder GetInputOrder() const = 0;
  70. //! Base execution for ProcessInput packet, do not call directly.
  71. //! @param networkInput input structure to process
  72. //! @param deltaTime amount of time to integrate the provided inputs over
  73. virtual void ProcessInput(NetworkInput& networkInput, float deltaTime) = 0;
  74. //! Similar to ProcessInput, do not call directly.
  75. //! This only needs to be overridden in components which allow NetworkInput to be processed by script.
  76. //! @param networkInput input structure to process
  77. //! @param deltaTime amount of time to integrate the provided inputs over
  78. virtual void ProcessInputFromScript([[maybe_unused]] NetworkInput& networkInput, [[maybe_unused]] float deltaTime){}
  79. //! Only valid on a client, should never be invoked on the server.
  80. //! @param networkInput input structure to process
  81. //! @param deltaTime amount of time to integrate the provided inputs over
  82. virtual void CreateInput(NetworkInput& networkInput, float deltaTime) = 0;
  83. //! Similar to CreateInput, should never be invoked on the server.
  84. //! This only needs to be overridden in components which allow NetworkInput creation to be handled by scripts.
  85. //! @param networkInput input structure to process
  86. //! @param deltaTime amount of time to integrate the provided inputs over
  87. virtual void CreateInputFromScript([[maybe_unused]]NetworkInput& networkInput, [[maybe_unused]] float deltaTime) {}
  88. template <typename ComponentType>
  89. const ComponentType* FindComponent() const;
  90. template <typename ComponentType>
  91. ComponentType* FindComponent();
  92. template <typename ControllerType>
  93. ControllerType* FindController(const NetworkEntityHandle& entityHandle) const;
  94. MultiplayerController* FindController(const AZ::Uuid& typeId, const NetworkEntityHandle& entityHandle) const;
  95. private:
  96. MultiplayerComponent& m_owner;
  97. friend class NetBindComponent; // For access to create and process input methods
  98. };
  99. template <typename ComponentType>
  100. inline const ComponentType* MultiplayerController::FindComponent() const
  101. {
  102. return GetEntity()->FindComponent<ComponentType>();
  103. }
  104. template <typename ComponentType>
  105. inline ComponentType* MultiplayerController::FindComponent()
  106. {
  107. return GetEntity()->FindComponent<ComponentType>();
  108. }
  109. template <typename ControllerType>
  110. inline ControllerType* MultiplayerController::FindController(const NetworkEntityHandle& entityHandle) const
  111. {
  112. return static_cast<ControllerType*>(FindController(typename ControllerType::ComponentType::RTTI_Type(), entityHandle));
  113. }
  114. }