52[[nodiscard]]
inline float path_length(std::span<const Vec2> waypoints)
noexcept {
53 if (waypoints.empty()) {
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);
70[[nodiscard]]
inline Vec2 point_along(std::span<const Vec2> waypoints,
float distance)
noexcept {
71 if (waypoints.empty()) {
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);
80 return from + (step * (left / length));
85 return waypoints.back();
92 return std::max(
path_length(follower.waypoints) - follower.progress, 0.0F);
107 const float delta = tick.
delta();
115 std::min(follower.
progress + (std::max(follower.
speed, 0.0F) * delta), length);
119 arrived.push_back(entity);
131 std::span<const Entity> arrived) {
132 for (
const Entity entity : arrived) {
135 .position = follower && !follower->get().waypoints.empty()
136 ? follower->get().waypoints.back()
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 ®istry, 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 ®istry, 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
Systems and the scheduler that updates them in order, once per tick.