horse_spearman.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #include "horse_spearman.h"
  2. #include "../core/component.h"
  3. #include "../core/event_manager.h"
  4. #include "../core/world.h"
  5. #include "../systems/troop_profile_service.h"
  6. #include "units/troop_type.h"
  7. #include "units/unit.h"
  8. #include <memory>
  9. #include <qvectornd.h>
  10. static inline auto team_color(int owner_id) -> QVector3D {
  11. switch (owner_id) {
  12. case 1:
  13. return {0.20F, 0.55F, 1.00F};
  14. case 2:
  15. return {1.00F, 0.30F, 0.30F};
  16. case 3:
  17. return {0.20F, 0.80F, 0.40F};
  18. case 4:
  19. return {1.00F, 0.80F, 0.20F};
  20. default:
  21. return {0.8F, 0.8F, 0.8F};
  22. }
  23. }
  24. namespace Game::Units {
  25. HorseSpearman::HorseSpearman(Engine::Core::World &world)
  26. : Unit(world, TroopType::HorseSpearman) {}
  27. auto HorseSpearman::Create(Engine::Core::World &world,
  28. const SpawnParams &params)
  29. -> std::unique_ptr<HorseSpearman> {
  30. auto unit = std::unique_ptr<HorseSpearman>(new HorseSpearman(world));
  31. unit->init(params);
  32. return unit;
  33. }
  34. void HorseSpearman::init(const SpawnParams &params) {
  35. auto *e = m_world->create_entity();
  36. m_id = e->get_id();
  37. const auto nation_id = resolve_nation_id(params);
  38. auto profile = Game::Systems::TroopProfileService::instance().get_profile(
  39. nation_id, TroopType::HorseSpearman);
  40. m_t = e->add_component<Engine::Core::TransformComponent>();
  41. m_t->position = {params.position.x(), params.position.y(),
  42. params.position.z()};
  43. float const scale = profile.visuals.render_scale;
  44. m_t->scale = {scale, scale, scale};
  45. m_r = e->add_component<Engine::Core::RenderableComponent>("", "");
  46. m_r->visible = true;
  47. m_r->renderer_id = profile.visuals.renderer_id;
  48. m_u = e->add_component<Engine::Core::UnitComponent>();
  49. m_u->spawn_type = params.spawn_type;
  50. m_u->health = profile.combat.health;
  51. m_u->max_health = profile.combat.max_health;
  52. m_u->speed = profile.combat.speed;
  53. m_u->owner_id = params.player_id;
  54. m_u->vision_range = profile.combat.vision_range;
  55. m_u->nation_id = nation_id;
  56. if (params.ai_controlled) {
  57. e->add_component<Engine::Core::AIControlledComponent>();
  58. } else {
  59. }
  60. QVector3D const tc = team_color(m_u->owner_id);
  61. m_r->color[0] = tc.x();
  62. m_r->color[1] = tc.y();
  63. m_r->color[2] = tc.z();
  64. m_mv = e->add_component<Engine::Core::MovementComponent>();
  65. if (m_mv != nullptr) {
  66. m_mv->goal_x = params.position.x();
  67. m_mv->goal_y = params.position.z();
  68. m_mv->target_x = params.position.x();
  69. m_mv->target_y = params.position.z();
  70. }
  71. m_atk = e->add_component<Engine::Core::AttackComponent>();
  72. m_atk->range = profile.combat.ranged_range;
  73. m_atk->damage = profile.combat.ranged_damage;
  74. m_atk->cooldown = profile.combat.ranged_cooldown;
  75. m_atk->melee_range = profile.combat.melee_range;
  76. m_atk->melee_damage = profile.combat.melee_damage;
  77. m_atk->melee_cooldown = profile.combat.melee_cooldown;
  78. m_atk->preferred_mode =
  79. profile.combat.can_ranged
  80. ? Engine::Core::AttackComponent::CombatMode::Auto
  81. : Engine::Core::AttackComponent::CombatMode::Melee;
  82. m_atk->current_mode = profile.combat.can_ranged
  83. ? Engine::Core::AttackComponent::CombatMode::Ranged
  84. : Engine::Core::AttackComponent::CombatMode::Melee;
  85. m_atk->can_ranged = profile.combat.can_ranged;
  86. m_atk->can_melee = profile.combat.can_melee;
  87. m_atk->max_height_difference = 2.0F;
  88. Engine::Core::EventManager::instance().publish(Engine::Core::UnitSpawnedEvent(
  89. m_id, m_u->owner_id, m_u->spawn_type, params.is_initial_spawn));
  90. }
  91. } // namespace Game::Units