NetworkAiComponent.cpp 5.0 KB

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