Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
targeting.hpp
Go to the documentation of this file.
1
4#pragma once
11
12#include <algorithm>
13#include <cstdint>
14#include <limits>
15#include <tuple>
16#include <vector>
17
18namespace aegis2d::ecs {
19
21enum class TargetOrder : std::uint8_t {
23 Nearest,
27};
28
52template <typename Component>
53void find_targets(const Registry& registry, const CollisionGrid& grid, Vec2 center, float radius,
54 TargetOrder order, std::vector<Entity>& out) {
55 out.clear();
56 if (!(radius >= 0.0F)) {
57 return;
58 }
59 grid.query(Aabb{.min = center - Vec2{radius, radius}, .max = center + Vec2{radius, radius}},
60 out);
61
62 // First key, second key, entity: compared in that order.
63 std::vector<std::tuple<float, float, Entity>> ranked;
64 ranked.reserve(out.size());
65 for (const Entity entity : out) {
66 if (!registry.alive(entity) || !registry.has<Component>(entity)) {
67 continue;
68 }
69 const auto transform = registry.try_get<Transform>(entity);
70 if (!transform) {
71 continue;
72 }
73 const Vec2 offset = transform->get().position - center;
74 const float distance = (offset.x * offset.x) + (offset.y * offset.y);
75 if (!(distance <= radius * radius)) {
76 continue;
77 }
78 if (order == TargetOrder::Nearest) {
79 ranked.emplace_back(distance, 0.0F, entity);
80 } else {
81 const auto follower = registry.try_get<PathFollower>(entity);
82 ranked.emplace_back(follower ? remaining(follower->get())
83 : std::numeric_limits<float>::infinity(),
84 distance, entity);
85 }
86 }
87 std::ranges::sort(ranked);
88 out.clear();
89 for (const auto& [first, second, entity] : ranked) {
90 out.push_back(entity);
91 }
92}
93
94} // namespace aegis2d::ecs
Broad phase of collision detection: a uniform grid over the play area.
Definition collision.hpp:120
void query(const Aabb &area, std::vector< Entity > &out) const
Entities recorded in the cells an area touches.
Handle to an entity: 32 bits made of a 24-bit index and an 8-bit generation.
Definition entity.hpp:23
Entry point of the ECS: creates entities and stores their components.
Definition registry.hpp:34
std::optional< std::reference_wrapper< Component > > try_get(Entity entity)
Component of an entity, if it has one.
Definition registry.hpp:147
bool has(Entity entity) const
Tells whether an entity has every listed component.
Definition registry.hpp:114
bool alive(Entity entity) const noexcept
Tells whether a handle designates a living entity.
Collision detection: a uniform grid finds the entities close enough to collide (broad phase),...
Components shared by the client and the server.
Generational entity handle: an index into the component pools plus a generation.
Vector and matrix types of the engine (GLM behind engine names).
Entity-Component-System used by client and server.
Definition animation.hpp:13
float remaining(const PathFollower &follower) noexcept
Distance a follower still has to walk.
Definition path_following.hpp:91
void find_targets(const Registry &registry, const CollisionGrid &grid, Vec2 center, float radius, TargetOrder order, std::vector< Entity > &out)
Lists the entities with a component whose center is within a radius of a point, in the order asked.
Definition targeting.hpp:53
TargetOrder
Which targets come first in find_targets().
Definition targeting.hpp:21
@ Nearest
The closest to the point first.
@ MostAdvanced
The furthest along its path first: the least PathFollower remaining(), then the closest; entities tha...
glm::vec2 Vec2
2D vector of floats: positions, velocities, sizes (glm::vec2).
Definition math.hpp:19
Path following: entities that walk along waypoints at a steady speed, and tell when they reach the en...
Registry: owns the entities and one component pool per component type.
Axis-aligned box given by its two corners, in logical pixels.
Definition collision.hpp:20
Vec2 min
Top-left corner (smallest x and y).
Definition collision.hpp:22
An entity that walks along waypoints, such as an enemy of a tower defense on the path found by NavGri...
Definition path_following.hpp:26
Where an entity is.
Definition components.hpp:17
Vec2 position
Center of the entity.
Definition components.hpp:19