NetworkWeaponsComponent.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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/NetworkWeaponsComponent.AutoComponent.h>
  9. #include <Source/Components/NetworkAiComponent.h>
  10. #include <Source/Weapons/IWeapon.h>
  11. #include <StartingPointInput/InputEventNotificationBus.h>
  12. namespace DebugDraw { class DebugDrawRequests; }
  13. namespace MultiplayerSample
  14. {
  15. // Input Event Ids for Player Controls
  16. const StartingPointInput::InputEventNotificationId DrawEventId("drawWeapon");
  17. const StartingPointInput::InputEventNotificationId FirePrimaryEventId("firePrimaryWeapon");
  18. const StartingPointInput::InputEventNotificationId FireSecondaryEventId("fireSecondaryWeapon");
  19. const WeaponIndex PrimaryWeaponIndex = WeaponIndex{ 0 };
  20. const WeaponIndex SecondaryWeaponIndex = WeaponIndex{ 1 };
  21. using OnWeaponActivateEvent = AZ::Event<const WeaponActivationInfo&>;
  22. using OnWeaponPredictHitEvent = AZ::Event<const WeaponHitInfo&>;
  23. using OnWeaponConfirmHitEvent = AZ::Event<const WeaponHitInfo&>;
  24. class NetworkWeaponsComponent
  25. : public NetworkWeaponsComponentBase
  26. , private WeaponListener
  27. {
  28. public:
  29. AZ_MULTIPLAYER_COMPONENT(MultiplayerSample::NetworkWeaponsComponent, s_networkWeaponsComponentConcreteUuid, MultiplayerSample::NetworkWeaponsComponentBase);
  30. static void Reflect(AZ::ReflectContext* context);
  31. NetworkWeaponsComponent();
  32. void OnInit() override;
  33. void OnActivate(Multiplayer::EntityIsMigrating entityIsMigrating) override;
  34. void OnDeactivate(Multiplayer::EntityIsMigrating entityIsMigrating) override;
  35. #if AZ_TRAIT_CLIENT
  36. void HandleSendConfirmHit(AzNetworking::IConnection* invokingConnection, const WeaponIndex& weaponIndex, const HitEvent& hitEvent) override;
  37. #endif
  38. void ActivateWeaponWithParams(WeaponIndex weaponIndex, WeaponState& weaponState, const FireParams& fireParams, bool validateActivations);
  39. IWeapon* GetWeapon(WeaponIndex weaponIndex) const;
  40. void AddOnWeaponActivateEventHandler(OnWeaponActivateEvent::Handler& handler);
  41. void AddOnWeaponPredictHitEventHandler(OnWeaponPredictHitEvent::Handler& handler);
  42. void AddOnWeaponConfirmHitEventHandler(OnWeaponConfirmHitEvent::Handler& handler);
  43. AZ::Vector3 GetCurrentShotStartPosition();
  44. private:
  45. //! WeaponListener interface
  46. //! @{
  47. void OnWeaponActivate(const WeaponActivationInfo& activationInfo) override;
  48. void OnWeaponHit(const WeaponHitInfo& hitInfo) override;
  49. //! @}
  50. void OnWeaponPredictHit(const WeaponHitInfo& hitInfo);
  51. void OnWeaponConfirmHit(const WeaponHitInfo& hitInfo);
  52. void OnUpdateActivationCounts(int32_t index, uint8_t value);
  53. void OnTickSimulatedWeapons(float seconds);
  54. using WeaponPointer = AZStd::unique_ptr<IWeapon>;
  55. AZStd::array<WeaponPointer, MaxWeaponsPerComponent> m_weapons;
  56. AZ::Event<int32_t, uint8_t>::Handler m_activationCountHandler;
  57. AZStd::array<WeaponState, MaxWeaponsPerComponent> m_simulatedWeaponStates;
  58. AZStd::array<int32_t, MaxWeaponsPerComponent> m_fireBoneJointIds;
  59. DebugDraw::DebugDrawRequests* m_debugDraw = nullptr;
  60. OnWeaponActivateEvent m_onWeaponActivateEvent;
  61. OnWeaponPredictHitEvent m_onWeaponPredictHitEvent;
  62. OnWeaponConfirmHitEvent m_onWeaponConfirmHitEvent;
  63. AZ::ScheduledEvent m_tickSimulatedWeapons{[this]()
  64. {
  65. OnTickSimulatedWeapons(AZ::TimeMsToSeconds(m_tickSimulatedWeapons.TimeInQueueMs()));
  66. }, AZ::Name("TickSimulatedWeapons")};
  67. };
  68. class NetworkWeaponsComponentController
  69. : public NetworkWeaponsComponentControllerBase
  70. , private StartingPointInput::InputEventNotificationBus::MultiHandler
  71. {
  72. public:
  73. NetworkWeaponsComponentController(NetworkWeaponsComponent& parent);
  74. void OnActivate(Multiplayer::EntityIsMigrating entityIsMigrating) override;
  75. void OnDeactivate(Multiplayer::EntityIsMigrating entityIsMigrating) override;
  76. void CreateInput(Multiplayer::NetworkInput& input, float deltaTime) override;
  77. void ProcessInput(Multiplayer::NetworkInput& input, float deltaTime) override;
  78. private:
  79. friend class NetworkAiComponentController;
  80. void UpdateAI();
  81. //! Update pump for player controlled weapons
  82. //! @param deltaTime the time in seconds since last tick
  83. void UpdateWeaponFiring(float deltaTime);
  84. //! Starts a weapon with the frame id from the client
  85. //! @return boolean true on activate, false if the weapon failed to activate
  86. virtual bool TryStartFire(WeaponIndex weaponIndex, const FireParams& fireParams);
  87. //! AZ::InputEventNotificationBus interface
  88. //! @{
  89. void OnPressed(float value) override;
  90. void OnReleased(float value) override;
  91. void OnHeld(float value) override;
  92. //! @}
  93. AZ::ScheduledEvent m_updateAI;
  94. NetworkAiComponentController* m_networkAiComponentController = nullptr;
  95. // Technically these values should never migrate hosts since they are maintained by the autonomous client
  96. // But due to how the stress test chaos monkey operates, it puppets these values on the server to mimick a client
  97. // This means these values can and will migrate between hosts (and lose any stored state)
  98. // We will need to consider moving these values to Authority to Server network properties if the design doesn't change
  99. bool m_aiEnabled = false;
  100. bool m_weaponDrawn = true;
  101. bool m_weaponDrawnChanged = false;
  102. WeaponActivationBitset m_weaponFiring;
  103. };
  104. }