| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311 |
- #include "unit.h"
- #include "../core/component.h"
- #include "../core/world.h"
- #include "../systems/nation_id.h"
- #include "../systems/nation_registry.h"
- #include "../systems/troop_profile_service.h"
- #include "units/troop_type.h"
- #include <qvectornd.h>
- #include <string>
- #include <utility>
- namespace Game::Units {
- Unit::Unit(Engine::Core::World &world, TroopType type)
- : m_world(&world), m_type_string(troop_typeToString(type)) {}
- Unit::Unit(Engine::Core::World &world, std::string type)
- : m_world(&world), m_type_string(std::move(type)) {}
- auto Unit::entity() const -> Engine::Core::Entity * {
- return (m_world != nullptr) ? m_world->get_entity(m_id) : nullptr;
- }
- auto Unit::resolve_nation_id(const SpawnParams ¶ms)
- -> Game::Systems::NationID {
- return params.nation_id;
- }
- void Unit::ensureCoreComponents() {
- if (m_world == nullptr) {
- return;
- }
- if (auto *e = entity()) {
- if (m_t == nullptr) {
- m_t = e->get_component<Engine::Core::TransformComponent>();
- }
- if (m_r == nullptr) {
- m_r = e->get_component<Engine::Core::RenderableComponent>();
- }
- if (m_u == nullptr) {
- m_u = e->get_component<Engine::Core::UnitComponent>();
- }
- if (m_mv == nullptr) {
- m_mv = e->get_component<Engine::Core::MovementComponent>();
- }
- if (m_atk == nullptr) {
- m_atk = e->get_component<Engine::Core::AttackComponent>();
- }
- }
- }
- void Unit::move_to(float x, float z) {
- ensureCoreComponents();
- if (m_mv == nullptr) {
- if (auto *e = entity()) {
- m_mv = e->add_component<Engine::Core::MovementComponent>();
- }
- }
- if (m_mv != nullptr) {
- m_mv->target_x = x;
- m_mv->target_y = z;
- m_mv->has_target = true;
- m_mv->goal_x = x;
- m_mv->goal_y = z;
- m_mv->clear_path();
- m_mv->path_pending = false;
- m_mv->pending_request_id = 0;
- }
- if (auto *e = entity()) {
- auto *hold_comp = e->get_component<Engine::Core::HoldModeComponent>();
- if (hold_comp != nullptr) {
- hold_comp->active = false;
- }
- auto *guard_comp = e->get_component<Engine::Core::GuardModeComponent>();
- if (guard_comp != nullptr) {
- guard_comp->active = false;
- }
- }
- }
- auto Unit::is_alive() const -> bool {
- if (auto *e = entity()) {
- if (auto *u = e->get_component<Engine::Core::UnitComponent>()) {
- return u->health > 0;
- }
- }
- return false;
- }
- auto Unit::position() const -> QVector3D {
- if (auto *e = entity()) {
- if (auto *t = e->get_component<Engine::Core::TransformComponent>()) {
- return {t->position.x, t->position.y, t->position.z};
- }
- }
- return {};
- }
- void Unit::set_hold_mode(bool enabled) {
- auto *e = entity();
- if (e == nullptr) {
- return;
- }
- auto *hold_comp = e->get_component<Engine::Core::HoldModeComponent>();
- if (enabled) {
- if (hold_comp == nullptr) {
- hold_comp = e->add_component<Engine::Core::HoldModeComponent>();
- }
- hold_comp->active = true;
- hold_comp->exit_cooldown = 0.0F;
- auto *guard_comp = e->get_component<Engine::Core::GuardModeComponent>();
- if (guard_comp != nullptr) {
- guard_comp->active = false;
- }
- auto *mv = e->get_component<Engine::Core::MovementComponent>();
- if (mv != nullptr) {
- mv->has_target = false;
- mv->clear_path();
- mv->path_pending = false;
- }
- } else {
- if (hold_comp != nullptr) {
- hold_comp->active = false;
- hold_comp->exit_cooldown = hold_comp->stand_up_duration;
- }
- }
- }
- auto Unit::is_in_hold_mode() const -> bool {
- auto *e = entity();
- if (e == nullptr) {
- return false;
- }
- auto *hold_comp = e->get_component<Engine::Core::HoldModeComponent>();
- return (hold_comp != nullptr) && hold_comp->active;
- }
- void Unit::set_guard_mode(bool enabled) {
- auto *e = entity();
- if (e == nullptr) {
- return;
- }
- auto *guard_comp = e->get_component<Engine::Core::GuardModeComponent>();
- if (enabled) {
- if (guard_comp == nullptr) {
- guard_comp = e->add_component<Engine::Core::GuardModeComponent>();
- }
- guard_comp->active = true;
- guard_comp->returning_to_guard_position = false;
- auto *hold_comp = e->get_component<Engine::Core::HoldModeComponent>();
- if (hold_comp != nullptr) {
- hold_comp->active = false;
- }
- if (!guard_comp->has_guard_target) {
- auto *transform = e->get_component<Engine::Core::TransformComponent>();
- if (transform != nullptr) {
- guard_comp->guard_position_x = transform->position.x;
- guard_comp->guard_position_z = transform->position.z;
- guard_comp->has_guard_target = true;
- }
- }
- } else {
- if (guard_comp != nullptr) {
- guard_comp->active = false;
- }
- }
- }
- void Unit::set_guard_target(Engine::Core::EntityID target_id) {
- auto *e = entity();
- if (e == nullptr) {
- return;
- }
- auto *guard_comp = e->get_component<Engine::Core::GuardModeComponent>();
- if (guard_comp == nullptr) {
- guard_comp = e->add_component<Engine::Core::GuardModeComponent>();
- }
- guard_comp->guarded_entity_id = target_id;
- guard_comp->guard_position_x = 0.0F;
- guard_comp->guard_position_z = 0.0F;
- guard_comp->active = true;
- guard_comp->returning_to_guard_position = false;
- guard_comp->has_guard_target = true;
- auto *hold_comp = e->get_component<Engine::Core::HoldModeComponent>();
- if (hold_comp != nullptr) {
- hold_comp->active = false;
- }
- }
- void Unit::set_guard_position(float x, float z) {
- auto *e = entity();
- if (e == nullptr) {
- return;
- }
- auto *guard_comp = e->get_component<Engine::Core::GuardModeComponent>();
- if (guard_comp == nullptr) {
- guard_comp = e->add_component<Engine::Core::GuardModeComponent>();
- }
- guard_comp->guarded_entity_id = 0;
- guard_comp->guard_position_x = x;
- guard_comp->guard_position_z = z;
- guard_comp->active = true;
- guard_comp->returning_to_guard_position = false;
- guard_comp->has_guard_target = true;
- auto *hold_comp = e->get_component<Engine::Core::HoldModeComponent>();
- if (hold_comp != nullptr) {
- hold_comp->active = false;
- }
- }
- auto Unit::is_in_guard_mode() const -> bool {
- auto *e = entity();
- if (e == nullptr) {
- return false;
- }
- auto *guard_comp = e->get_component<Engine::Core::GuardModeComponent>();
- return (guard_comp != nullptr) && guard_comp->active;
- }
- void Unit::clear_guard_mode() {
- auto *e = entity();
- if (e == nullptr) {
- return;
- }
- auto *guard_comp = e->get_component<Engine::Core::GuardModeComponent>();
- if (guard_comp != nullptr) {
- guard_comp->active = false;
- guard_comp->guarded_entity_id = 0;
- guard_comp->guard_position_x = 0.0F;
- guard_comp->guard_position_z = 0.0F;
- guard_comp->returning_to_guard_position = false;
- guard_comp->has_guard_target = false;
- }
- }
- void Unit::set_run_mode(bool enabled) {
- auto *e = entity();
- if (e == nullptr) {
- return;
- }
- const auto *unit_comp = e->get_component<Engine::Core::UnitComponent>();
- if (unit_comp == nullptr ||
- !Game::Units::can_use_run_mode(unit_comp->spawn_type)) {
- return;
- }
- auto *stamina_comp = e->get_component<Engine::Core::StaminaComponent>();
- if (stamina_comp == nullptr) {
- if (!enabled) {
- return;
- }
- stamina_comp = e->add_component<Engine::Core::StaminaComponent>();
- const auto troop_type =
- Game::Units::spawn_typeToTroopType(unit_comp->spawn_type);
- if (troop_type.has_value()) {
- const auto profile =
- Game::Systems::TroopProfileService::instance().get_profile(
- unit_comp->nation_id, *troop_type);
- stamina_comp->initialize_from_stats(
- profile.combat.max_stamina, profile.combat.stamina_regen_rate,
- profile.combat.stamina_depletion_rate);
- }
- }
- stamina_comp->run_requested = enabled;
- }
- auto Unit::is_running() const -> bool {
- const auto *e = entity();
- if (e == nullptr) {
- return false;
- }
- const auto *stamina_comp = e->get_component<Engine::Core::StaminaComponent>();
- return stamina_comp != nullptr && stamina_comp->is_running;
- }
- auto Unit::can_run() const -> bool {
- const auto *e = entity();
- if (e == nullptr) {
- return false;
- }
- const auto *unit_comp = e->get_component<Engine::Core::UnitComponent>();
- return unit_comp != nullptr &&
- Game::Units::can_use_run_mode(unit_comp->spawn_type);
- }
- } // namespace Game::Units
|