Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
path_following.hpp
Go to the documentation of this file.
1
4#pragma once
11
12#include <algorithm>
13#include <cmath>
14#include <span>
15#include <vector>
16
17namespace aegis2d::ecs {
18
29 std::vector<Vec2> waypoints{};
31 float speed = 0.0F;
33 float progress = 0.0F;
35 bool arrived = false;
36};
37
39struct PathEnded {
44
46 friend bool operator==(const PathEnded&, const PathEnded&) = default;
47};
48
52[[nodiscard]] inline float path_length(std::span<const Vec2> waypoints) noexcept {
53 if (waypoints.empty()) {
54 return 0.0F;
55 }
56 float length = 0.0F;
57 Vec2 from = waypoints.front();
58 for (const Vec2 to : waypoints.subspan(1)) {
59 const Vec2 step = to - from;
60 length += std::hypot(step.x, step.y);
61 from = to;
62 }
63 return length;
64}
65
70[[nodiscard]] inline Vec2 point_along(std::span<const Vec2> waypoints, float distance) noexcept {
71 if (waypoints.empty()) {
72 return {};
73 }
74 Vec2 from = waypoints.front();
75 float left = std::max(distance, 0.0F);
76 for (const Vec2 to : waypoints.subspan(1)) {
77 const Vec2 step = to - from;
78 const float length = std::hypot(step.x, step.y);
79 if (left < length) {
80 return from + (step * (left / length));
81 }
82 left -= length;
83 from = to;
84 }
85 return waypoints.back();
86}
87
91[[nodiscard]] inline float remaining(const PathFollower& follower) noexcept {
92 return std::max(path_length(follower.waypoints) - follower.progress, 0.0F);
93}
94
105inline void follow_paths(Registry& registry, const Tick& tick, std::vector<Entity>& arrived) {
106 arrived.clear();
107 const float delta = tick.delta();
108 registry.view<PathFollower, Transform>().each(
109 [delta, &arrived](Entity entity, PathFollower& follower, Transform& transform) {
110 if (follower.arrived || follower.waypoints.empty()) {
111 return;
112 }
113 const float length = path_length(follower.waypoints);
114 follower.progress =
115 std::min(follower.progress + (std::max(follower.speed, 0.0F) * delta), length);
116 transform.position = point_along(follower.waypoints, follower.progress);
117 if (follower.progress >= length) {
118 follower.arrived = true;
119 arrived.push_back(entity);
120 }
121 });
122}
123
130inline void publish_path_ends(core::EventBus& events, const Registry& registry,
131 std::span<const Entity> arrived) {
132 for (const Entity entity : arrived) {
133 const auto follower = registry.try_get<PathFollower>(entity);
134 events.publish(PathEnded{.entity = entity,
135 .position = follower && !follower->get().waypoints.empty()
136 ? follower->get().waypoints.back()
137 : Vec2{}});
138 }
139}
140
141} // namespace aegis2d::ecs
Lets systems react to events without knowing who sends them.
Definition event_bus.hpp:31
void publish(const Event &event)
Calls every handler of type Event with event, before returning.
Definition event_bus.hpp:66
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
View< Components... > view()
View on the entities that own all the listed components.
Definition registry.hpp:222
Components shared by the client and the server.
Generational entity handle: an index into the component pools plus a generation.
Typed publish/subscribe event bus with immediate dispatch.
Vector and matrix types of the engine (GLM behind engine names).
Entity-Component-System used by client and server.
Definition animation.hpp:13
Vec2 point_along(std::span< const Vec2 > waypoints, float distance) noexcept
Point at a distance along a path.
Definition path_following.hpp:70
void follow_paths(Registry &registry, const Tick &tick, std::vector< Entity > &arrived)
Moves every PathFollower along its path for one tick and lists those that arrived.
Definition path_following.hpp:105
float remaining(const PathFollower &follower) noexcept
Distance a follower still has to walk.
Definition path_following.hpp:91
float path_length(std::span< const Vec2 > waypoints) noexcept
Length of a path.
Definition path_following.hpp:52
void publish_path_ends(core::EventBus &events, const Registry &registry, std::span< const Entity > arrived)
Publishes a PathEnded for each entity that arrived this tick.
Definition path_following.hpp:130
glm::vec2 Vec2
2D vector of floats: positions, velocities, sizes (glm::vec2).
Definition math.hpp:19
Registry: owns the entities and one component pool per component type.
An entity that reached the last waypoint of its path.
Definition path_following.hpp:39
Entity entity
Entity that arrived.
Definition path_following.hpp:41
friend bool operator==(const PathEnded &, const PathEnded &)=default
Compares the events.
Vec2 position
Last waypoint of its path.
Definition path_following.hpp:43
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
float progress
Distance walked from the first waypoint, in logical pixels.
Definition path_following.hpp:33
std::vector< Vec2 > waypoints
Points to walk through, in order, in logical pixels; the entity starts on the first one.
Definition path_following.hpp:29
float speed
Speed along the path, in logical pixels per second.
Definition path_following.hpp:31
bool arrived
true once the last waypoint is reached; the entity stops there.
Definition path_following.hpp:35
One simulation step, as the systems see it.
Definition system.hpp:23
constexpr float delta() const noexcept
Duration of the step in seconds, to scale values given per second.
Definition system.hpp:32
Where an entity is.
Definition components.hpp:17
Vec2 position
Center of the entity.
Definition components.hpp:19
Systems and the scheduler that updates them in order, once per tick.