home.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #include "home.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 "../visuals/team_colors.h"
  8. #include "units/unit.h"
  9. #include <memory>
  10. namespace Game::Units {
  11. Home::Home(Engine::Core::World &world) : Unit(world, "home") {}
  12. auto Home::Create(Engine::Core::World &world,
  13. const SpawnParams &params) -> std::unique_ptr<Home> {
  14. auto unit = std::unique_ptr<Home>(new Home(world));
  15. unit->init(params);
  16. return unit;
  17. }
  18. void Home::init(const SpawnParams &params) {
  19. auto *e = m_world->create_entity();
  20. m_id = e->get_id();
  21. const auto nation_id = resolve_nation_id(params);
  22. m_t = e->add_component<Engine::Core::TransformComponent>();
  23. m_t->position = {params.position.x(), params.position.y(),
  24. params.position.z()};
  25. m_t->scale = {1.2F, 1.0F, 1.2F};
  26. m_r = e->add_component<Engine::Core::RenderableComponent>("", "");
  27. m_r->visible = true;
  28. m_r->mesh = Engine::Core::RenderableComponent::MeshKind::Cube;
  29. m_u = e->add_component<Engine::Core::UnitComponent>();
  30. m_u->spawn_type = params.spawn_type;
  31. m_u->health = 1000;
  32. m_u->max_health = 1000;
  33. m_u->speed = 0.0F;
  34. m_u->owner_id = params.player_id;
  35. m_u->vision_range = 15.0F;
  36. m_u->nation_id = nation_id;
  37. if (params.ai_controlled) {
  38. e->add_component<Engine::Core::AIControlledComponent>();
  39. }
  40. QVector3D const tc = Game::Visuals::team_colorForOwner(m_u->owner_id);
  41. m_r->color[0] = tc.x();
  42. m_r->color[1] = tc.y();
  43. m_r->color[2] = tc.z();
  44. auto *building = e->add_component<Engine::Core::BuildingComponent>();
  45. if (building != nullptr) {
  46. building->original_nation_id = nation_id;
  47. }
  48. auto *home_comp = e->add_component<Engine::Core::HomeComponent>();
  49. if (home_comp != nullptr) {
  50. home_comp->population_contribution = 50;
  51. home_comp->update_cooldown = 0.0F;
  52. }
  53. Game::Systems::BuildingCollisionRegistry::instance().register_building(
  54. m_id, m_type_string, m_t->position.x, m_t->position.z, m_u->owner_id);
  55. Engine::Core::EventManager::instance().publish(Engine::Core::UnitSpawnedEvent(
  56. m_id, m_u->owner_id, m_u->spawn_type, params.is_initial_spawn));
  57. }
  58. } // namespace Game::Units