NetworkAiComponent.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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/NetworkAiComponent.h>
  8. #include <Source/Components/NetworkPlayerMovementComponent.h>
  9. #include <Source/Components/NetworkRandomComponent.h>
  10. #include <Source/Components/NetworkWeaponsComponent.h>
  11. #include <Multiplayer/Components/NetBindComponent.h>
  12. #include <Multiplayer/Components/LocalPredictionPlayerInputComponent.h>
  13. #include <AzCore/Serialization/EditContext.h>
  14. #include <AzCore/Serialization/SerializeContext.h>
  15. #include <AzCore/Time/ITime.h>
  16. namespace MultiplayerSample
  17. {
  18. NetworkAiComponentController::NetworkAiComponentController(NetworkAiComponent& parent)
  19. : NetworkAiComponentControllerBase(parent)
  20. {
  21. }
  22. #if AZ_TRAIT_SERVER
  23. constexpr static float SecondsToMs = 1000.f;
  24. void NetworkAiComponentController::TickMovement(NetworkPlayerMovementComponentController& movementController, float deltaTime)
  25. {
  26. // TODO: Execute this tick only if this component is owned by this endpoint (currently ticks on server only)
  27. float deltaTimeMs = deltaTime * SecondsToMs;
  28. ModifyRemainingTimeMs() -= deltaTimeMs;
  29. if (GetRemainingTimeMs() <= 0)
  30. {
  31. // Determine a new directive after 500 to 9500 ms
  32. SetRemainingTimeMs(GetNetworkRandomComponentController()->GetRandomFloat() * (GetActionIntervalMaxMs() - GetActionIntervalMinMs()) + GetActionIntervalMinMs());
  33. SetTurnRate(1.f / GetRemainingTimeMs());
  34. // Randomize new target yaw and pitch and compute the delta from the current yaw and pitch respectively
  35. SetTargetYawDelta(-movementController.m_viewYaw + (GetNetworkRandomComponentController()->GetRandomFloat() * 2.f - 1.f));
  36. SetTargetPitchDelta(-movementController.m_viewPitch + (GetNetworkRandomComponentController()->GetRandomFloat() - 0.5f));
  37. // Randomize the action and strafe direction (used only if we decide to strafe)
  38. SetAction(static_cast<Action>(GetNetworkRandomComponentController()->GetRandomInt() % static_cast<int>(Action::COUNT)));
  39. SetStrafingRight(static_cast<bool>(GetNetworkRandomComponentController()->GetRandomInt() % 2));
  40. }
  41. // Translate desired motion into inputs
  42. // Interpolate the current view yaw and pitch values towards the desired values
  43. movementController.m_viewYaw += GetTurnRate() * deltaTimeMs * GetTargetYawDelta();
  44. movementController.m_viewPitch += GetTurnRate() * deltaTimeMs * GetTargetPitchDelta();
  45. // Reset keyboard movement inputs decided on the previous frame
  46. movementController.m_forwardDown = false;
  47. movementController.m_backwardDown = false;
  48. movementController.m_leftDown = false;
  49. movementController.m_rightDown = false;
  50. movementController.m_sprinting = false;
  51. movementController.m_jumping = false;
  52. movementController.m_crouching = false;
  53. switch (GetAction())
  54. {
  55. case Action::Default:
  56. movementController.m_forwardDown = true;
  57. break;
  58. case Action::Sprinting:
  59. movementController.m_forwardDown = true;
  60. movementController.m_sprinting = true;
  61. break;
  62. case Action::Jumping:
  63. movementController.m_forwardDown = true;
  64. movementController.m_jumping = true;
  65. break;
  66. case Action::Crouching:
  67. movementController.m_forwardDown = true;
  68. movementController.m_crouching = true;
  69. break;
  70. case Action::Strafing:
  71. if (GetStrafingRight())
  72. {
  73. movementController.m_rightDown = true;
  74. }
  75. else
  76. {
  77. movementController.m_leftDown = true;
  78. }
  79. break;
  80. default:
  81. break;
  82. }
  83. }
  84. void NetworkAiComponentController::TickWeapons(NetworkWeaponsComponentController& weaponsController, float deltaTime)
  85. {
  86. // TODO: Execute this tick only if this component is owned by this endpoint (currently ticks on server only)
  87. ModifyTimeToNextShot() -= deltaTime * SecondsToMs;
  88. if (GetTimeToNextShot() <= 0)
  89. {
  90. if (GetShotFired())
  91. {
  92. // Fire weapon between 100 and 10000 ms from now
  93. SetTimeToNextShot(GetNetworkRandomComponentController()->GetRandomFloat() * (GetFireIntervalMaxMs() - GetFireIntervalMinMs()) + GetFireIntervalMinMs());
  94. SetShotFired(false);
  95. weaponsController.m_weaponFiring = false;
  96. }
  97. else
  98. {
  99. weaponsController.m_weaponFiring = true;
  100. SetShotFired(true);
  101. }
  102. }
  103. }
  104. void NetworkAiComponentController::ConfigureAi(
  105. float fireIntervalMinMs, float fireIntervalMaxMs, float actionIntervalMinMs, float actionIntervalMaxMs)
  106. {
  107. SetFireIntervalMinMs(fireIntervalMinMs);
  108. SetFireIntervalMaxMs(fireIntervalMaxMs);
  109. SetActionIntervalMinMs(actionIntervalMinMs);
  110. SetActionIntervalMaxMs(actionIntervalMaxMs);
  111. }
  112. #endif
  113. }