spearman.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #include "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. Spearman::Spearman(Engine::Core::World &world)
  26. : Unit(world, TroopType::Spearman) {}
  27. auto Spearman::Create(Engine::Core::World &world,
  28. const SpawnParams &params) -> std::unique_ptr<Spearman> {
  29. auto unit = std::unique_ptr<Spearman>(new Spearman(world));
  30. unit->init(params);
  31. return unit;
  32. }
  33. void Spearman::init(const SpawnParams &params) {
  34. auto *e = m_world->createEntity();
  35. m_id = e->getId();
  36. const std::string nation_id = resolve_nation_id(params);
  37. auto profile = Game::Systems::TroopProfileService::instance().get_profile(
  38. nation_id, TroopType::Spearman);
  39. m_t = e->addComponent<Engine::Core::TransformComponent>();
  40. m_t->position = {params.position.x(), params.position.y(),
  41. params.position.z()};
  42. float const scale = profile.visuals.render_scale;
  43. m_t->scale = {scale, scale, scale};
  44. m_r = e->addComponent<Engine::Core::RenderableComponent>("", "");
  45. m_r->visible = true;
  46. m_r->rendererId = profile.visuals.renderer_id;
  47. m_u = e->addComponent<Engine::Core::UnitComponent>();
  48. m_u->spawn_type = params.spawn_type;
  49. m_u->health = profile.combat.health;
  50. m_u->max_health = profile.combat.max_health;
  51. m_u->speed = profile.combat.speed;
  52. m_u->owner_id = params.player_id;
  53. m_u->vision_range = profile.combat.vision_range;
  54. m_u->nation_id = nation_id;
  55. if (params.aiControlled) {
  56. e->addComponent<Engine::Core::AIControlledComponent>();
  57. } else {
  58. }
  59. QVector3D const tc = team_color(m_u->owner_id);
  60. m_r->color[0] = tc.x();
  61. m_r->color[1] = tc.y();
  62. m_r->color[2] = tc.z();
  63. m_mv = e->addComponent<Engine::Core::MovementComponent>();
  64. if (m_mv != nullptr) {
  65. m_mv->goalX = params.position.x();
  66. m_mv->goalY = params.position.z();
  67. m_mv->target_x = params.position.x();
  68. m_mv->target_y = params.position.z();
  69. }
  70. m_atk = e->addComponent<Engine::Core::AttackComponent>();
  71. m_atk->range = profile.combat.ranged_range;
  72. m_atk->damage = profile.combat.ranged_damage;
  73. m_atk->cooldown = profile.combat.ranged_cooldown;
  74. m_atk->meleeRange = profile.combat.melee_range;
  75. m_atk->meleeDamage = profile.combat.melee_damage;
  76. m_atk->meleeCooldown = profile.combat.melee_cooldown;
  77. m_atk->preferredMode = profile.combat.can_ranged
  78. ? Engine::Core::AttackComponent::CombatMode::Auto
  79. : Engine::Core::AttackComponent::CombatMode::Melee;
  80. m_atk->currentMode = profile.combat.can_ranged
  81. ? Engine::Core::AttackComponent::CombatMode::Ranged
  82. : Engine::Core::AttackComponent::CombatMode::Melee;
  83. m_atk->canRanged = profile.combat.can_ranged;
  84. m_atk->canMelee = profile.combat.can_melee;
  85. m_atk->max_heightDifference = 2.0F;
  86. Engine::Core::EventManager::instance().publish(
  87. Engine::Core::UnitSpawnedEvent(m_id, m_u->owner_id, m_u->spawn_type));
  88. }
  89. } // namespace Game::Units