ambient_state_manager.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #include "ambient_state_manager.h"
  2. #include "game/core/component.h"
  3. #include "game/core/world.h"
  4. #include "game_engine.h"
  5. #include <QDebug>
  6. AmbientStateManager::AmbientStateManager() = default;
  7. void AmbientStateManager::update(float dt, Engine::Core::World *world,
  8. int local_owner_id,
  9. const EntityCache &entity_cache,
  10. const QString &victory_state) {
  11. m_ambient_check_timer += dt;
  12. const float check_interval = 2.0F;
  13. if (m_ambient_check_timer < check_interval) {
  14. return;
  15. }
  16. m_ambient_check_timer = 0.0F;
  17. Engine::Core::AmbientState new_state = Engine::Core::AmbientState::PEACEFUL;
  18. if (!victory_state.isEmpty()) {
  19. if (victory_state == "victory") {
  20. new_state = Engine::Core::AmbientState::VICTORY;
  21. } else if (victory_state == "defeat") {
  22. new_state = Engine::Core::AmbientState::DEFEAT;
  23. }
  24. } else if (is_player_in_combat(world, local_owner_id)) {
  25. new_state = Engine::Core::AmbientState::COMBAT;
  26. } else if (entity_cache.enemy_barracks_alive &&
  27. entity_cache.player_barracks_alive) {
  28. new_state = Engine::Core::AmbientState::TENSE;
  29. }
  30. if (new_state != m_current_ambient_state) {
  31. Engine::Core::AmbientState const previous_state = m_current_ambient_state;
  32. m_current_ambient_state = new_state;
  33. Engine::Core::EventManager::instance().publish(
  34. Engine::Core::AmbientStateChangedEvent(new_state, previous_state));
  35. qInfo() << "Ambient state changed from" << static_cast<int>(previous_state)
  36. << "to" << static_cast<int>(new_state);
  37. }
  38. }
  39. auto AmbientStateManager::is_player_in_combat(
  40. Engine::Core::World *world, int local_owner_id) const -> bool {
  41. if (!world) {
  42. return false;
  43. }
  44. auto units = world->get_entities_with<Engine::Core::UnitComponent>();
  45. const float combat_check_radius = 15.0F;
  46. for (auto *entity : units) {
  47. auto *unit = entity->get_component<Engine::Core::UnitComponent>();
  48. if ((unit == nullptr) || unit->owner_id != local_owner_id ||
  49. unit->health <= 0) {
  50. continue;
  51. }
  52. if (entity->has_component<Engine::Core::AttackTargetComponent>()) {
  53. return true;
  54. }
  55. auto *transform = entity->get_component<Engine::Core::TransformComponent>();
  56. if (transform == nullptr) {
  57. continue;
  58. }
  59. for (auto *other_entity : units) {
  60. auto *other_unit =
  61. other_entity->get_component<Engine::Core::UnitComponent>();
  62. if ((other_unit == nullptr) || other_unit->owner_id == local_owner_id ||
  63. other_unit->health <= 0) {
  64. continue;
  65. }
  66. auto *other_transform =
  67. other_entity->get_component<Engine::Core::TransformComponent>();
  68. if (other_transform == nullptr) {
  69. continue;
  70. }
  71. float const dx = transform->position.x - other_transform->position.x;
  72. float const dz = transform->position.z - other_transform->position.z;
  73. float const dist_sq = dx * dx + dz * dz;
  74. if (dist_sq < combat_check_radius * combat_check_radius) {
  75. return true;
  76. }
  77. }
  78. }
  79. return false;
  80. }