barracks.cpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #include "barracks.h"
  2. #include "../core/component.h"
  3. #include "../core/event_manager.h"
  4. #include "../core/ownership_constants.h"
  5. #include "../core/world.h"
  6. #include "../systems/building_collision_registry.h"
  7. #include "../systems/troop_profile_service.h"
  8. #include "../visuals/team_colors.h"
  9. #include "troop_config.h"
  10. #include "units/troop_type.h"
  11. #include "units/unit.h"
  12. #include <memory>
  13. namespace Game::Units {
  14. Barracks::Barracks(Engine::Core::World &world) : Unit(world, "barracks") {}
  15. auto Barracks::Create(Engine::Core::World &world,
  16. const SpawnParams &params) -> std::unique_ptr<Barracks> {
  17. auto unit = std::unique_ptr<Barracks>(new Barracks(world));
  18. unit->init(params);
  19. return unit;
  20. }
  21. void Barracks::init(const SpawnParams &params) {
  22. auto *e = m_world->create_entity();
  23. m_id = e->get_id();
  24. const auto nation_id = resolve_nation_id(params);
  25. m_t = e->add_component<Engine::Core::TransformComponent>();
  26. m_t->position = {params.position.x(), params.position.y(),
  27. params.position.z()};
  28. m_t->scale = {1.8F, 1.2F, 1.8F};
  29. m_r = e->add_component<Engine::Core::RenderableComponent>("", "");
  30. m_r->visible = true;
  31. m_r->mesh = Engine::Core::RenderableComponent::MeshKind::Cube;
  32. m_u = e->add_component<Engine::Core::UnitComponent>();
  33. m_u->spawn_type = params.spawn_type;
  34. m_u->health = 2000;
  35. m_u->max_health = 2000;
  36. m_u->speed = 0.0F;
  37. m_u->owner_id = params.player_id;
  38. m_u->vision_range = 22.0F;
  39. m_u->nation_id = nation_id;
  40. if (params.ai_controlled) {
  41. e->add_component<Engine::Core::AIControlledComponent>();
  42. } else {
  43. }
  44. QVector3D const tc = Game::Visuals::team_colorForOwner(m_u->owner_id);
  45. m_r->color[0] = tc.x();
  46. m_r->color[1] = tc.y();
  47. m_r->color[2] = tc.z();
  48. auto *building = e->add_component<Engine::Core::BuildingComponent>();
  49. if (building != nullptr) {
  50. building->original_nation_id = nation_id;
  51. }
  52. Game::Systems::BuildingCollisionRegistry::instance().register_building(
  53. m_id, m_type_string, m_t->position.x, m_t->position.z, m_u->owner_id);
  54. if (!Game::Core::isNeutralOwner(m_u->owner_id)) {
  55. if (auto *prod = e->add_component<Engine::Core::ProductionComponent>()) {
  56. prod->product_type = TroopType::Archer;
  57. prod->build_time = 10.0F;
  58. prod->max_units = params.max_population;
  59. prod->in_progress = false;
  60. prod->time_remaining = 0.0F;
  61. prod->produced_count = 0;
  62. prod->rally_x = m_t->position.x + 4.0F;
  63. prod->rally_z = m_t->position.z + 2.0F;
  64. prod->rally_set = true;
  65. const auto profile =
  66. Game::Systems::TroopProfileService::instance().get_profile(
  67. nation_id, prod->product_type);
  68. prod->build_time = profile.production.build_time;
  69. prod->villager_cost = profile.production.cost;
  70. }
  71. }
  72. Engine::Core::EventManager::instance().publish(Engine::Core::UnitSpawnedEvent(
  73. m_id, m_u->owner_id, m_u->spawn_type, params.is_initial_spawn));
  74. }
  75. } // namespace Game::Units