|
34 | 34 |
|
35 | 35 | namespace openage::gamestate::system { |
36 | 36 |
|
| 37 | +// helper type for the visitor processing the move_target |
| 38 | +template <class... Ts> |
| 39 | +struct overloaded : Ts... { |
| 40 | + using Ts::operator()...; |
| 41 | +}; |
| 42 | + |
37 | 43 |
|
38 | 44 | std::vector<coord::phys3> find_path(const std::shared_ptr<path::Pathfinder> &pathfinder, |
39 | 45 | path::grid_id_t grid_id, |
@@ -104,24 +110,26 @@ const time::time_t Move::move_target(const std::shared_ptr<gamestate::GameEntity |
104 | 110 | return time::time_t::from_int(0); |
105 | 111 | } |
106 | 112 |
|
107 | | - if (std::holds_alternative<gamestate::entity_id_t>(target.value())) { |
108 | | - auto target_id = std::get<gamestate::entity_id_t>(target.value()); |
109 | | - auto target_entity = state->get_game_entity(target_id); |
110 | | - |
111 | | - auto position = std::dynamic_pointer_cast<component::Position>( |
112 | | - target_entity->get_component(component::component_t::POSITION)); |
113 | | - |
114 | | - auto target_pos = position->get_positions().get(start_time); |
115 | | - |
116 | | - return Move::move_default(entity, state, target_pos, start_time); |
117 | | - } |
118 | | - else if (std::holds_alternative<coord::phys3>(target.value())) { |
119 | | - auto target_pos = std::get<coord::phys3>(target.value()); |
120 | | - return Move::move_default(entity, state, target_pos, start_time); |
121 | | - } |
122 | | - |
123 | | - log::log(WARN << "Entity " << entity->get_id() << " has an invalid target at time " << start_time); |
124 | | - return time::time_t::from_int(0); |
| 113 | + return std::visit( |
| 114 | + overloaded{ |
| 115 | + [&](const gamestate::entity_id_t &target_id) { |
| 116 | + auto target_entity = state->get_game_entity(target_id); |
| 117 | + |
| 118 | + auto position = std::dynamic_pointer_cast<component::Position>( |
| 119 | + target_entity->get_component(component::component_t::POSITION)); |
| 120 | + |
| 121 | + auto target_pos = position->get_positions().get(start_time); |
| 122 | + |
| 123 | + return Move::move_default(entity, state, target_pos, start_time); |
| 124 | + }, |
| 125 | + [&](const coord::phys3 &target_pos) { |
| 126 | + return Move::move_default(entity, state, target_pos, start_time); |
| 127 | + }, |
| 128 | + [&](const auto &target) { |
| 129 | + log::log(WARN << "Entity " << entity->get_id() << " has an invalid target type: " |
| 130 | + << typeid(target).name() << " at time " << start_time); |
| 131 | + return time::time_t::from_int(0); }}, |
| 132 | + target.value()); |
125 | 133 | } |
126 | 134 |
|
127 | 135 |
|
|
0 commit comments