NetworkWeaponsComponent.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 ${SanitizedCppName}
  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. class NetworkWeaponsComponent
  22. : public NetworkWeaponsComponentBase
  23. , private WeaponListener
  24. {
  25. public:
  26. AZ_MULTIPLAYER_COMPONENT(${SanitizedCppName}::NetworkWeaponsComponent, s_networkWeaponsComponentConcreteUuid, ${SanitizedCppName}::NetworkWeaponsComponentBase);
  27. static void Reflect(AZ::ReflectContext* context);
  28. NetworkWeaponsComponent();
  29. void OnInit() override;
  30. void OnActivate(Multiplayer::EntityIsMigrating entityIsMigrating) override;
  31. void OnDeactivate(Multiplayer::EntityIsMigrating entityIsMigrating) override;
  32. #if AZ_TRAIT_CLIENT
  33. void HandleSendConfirmHit(AzNetworking::IConnection* invokingConnection, const WeaponIndex& weaponIndex, const HitEvent& hitEvent) override;
  34. #endif
  35. void ActivateWeaponWithParams(WeaponIndex weaponIndex, WeaponState& weaponState, const FireParams& fireParams, bool validateActivations);
  36. IWeapon* GetWeapon(WeaponIndex weaponIndex) const;
  37. private:
  38. //! WeaponListener interface
  39. //! @{
  40. void OnWeaponActivate(const WeaponActivationInfo& activationInfo) override;
  41. void OnWeaponHit(const WeaponHitInfo& hitInfo) override;
  42. //! @}
  43. void OnWeaponPredictHit(const WeaponHitInfo& hitInfo);
  44. void OnWeaponConfirmHit(const WeaponHitInfo& hitInfo);
  45. void OnUpdateActivationCounts(int32_t index, uint8_t value);
  46. using WeaponPointer = AZStd::unique_ptr<IWeapon>;
  47. AZStd::array<WeaponPointer, MaxWeaponsPerComponent> m_weapons;
  48. AZ::Event<int32_t, uint8_t>::Handler m_activationCountHandler;
  49. AZStd::array<WeaponState, MaxWeaponsPerComponent> m_simulatedWeaponStates;
  50. AZStd::array<int32_t, MaxWeaponsPerComponent> m_fireBoneJointIds;
  51. DebugDraw::DebugDrawRequests* m_debugDraw = nullptr;
  52. };
  53. class NetworkWeaponsComponentController
  54. : public NetworkWeaponsComponentControllerBase
  55. , private StartingPointInput::InputEventNotificationBus::MultiHandler
  56. {
  57. public:
  58. NetworkWeaponsComponentController(NetworkWeaponsComponent& parent);
  59. void OnActivate(Multiplayer::EntityIsMigrating entityIsMigrating) override;
  60. void OnDeactivate(Multiplayer::EntityIsMigrating entityIsMigrating) override;
  61. void CreateInput(Multiplayer::NetworkInput& input, float deltaTime) override;
  62. void ProcessInput(Multiplayer::NetworkInput& input, float deltaTime) override;
  63. private:
  64. friend class NetworkAiComponentController;
  65. void UpdateAI();
  66. //! Update pump for player controlled weapons
  67. //! @param deltaTime the time in seconds since last tick
  68. void UpdateWeaponFiring(float deltaTime);
  69. //! Starts a weapon with the frame id from the client
  70. //! @return boolean true on activate, false if the weapon failed to activate
  71. virtual bool TryStartFire(WeaponIndex weaponIndex, const FireParams& fireParams);
  72. //! AZ::InputEventNotificationBus interface
  73. //! @{
  74. void OnPressed(float value) override;
  75. void OnReleased(float value) override;
  76. void OnHeld(float value) override;
  77. //! @}
  78. AZ::ScheduledEvent m_updateAI;
  79. NetworkAiComponentController* m_networkAiComponentController = nullptr;
  80. // Technically these values should never migrate hosts since they are maintained by the autonomous client
  81. // But due to how the stress test chaos monkey operates, it puppets these values on the server to mimick a client
  82. // This means these values can and will migrate between hosts (and lose any stored state)
  83. // We will need to consider moving these values to Authority to Server network properties if the design doesn't change
  84. bool m_aiEnabled = false;
  85. bool m_weaponDrawn = false;
  86. WeaponActivationBitset m_weaponFiring;
  87. };
  88. }