Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
despawn.hpp
Go to the documentation of this file.
1
3#pragma once
10
11#include <vector>
12
13namespace aegis2d::ecs {
14
25[[nodiscard]] inline bool left_for_good(Vec2 position, Vec2 half, Vec2 velocity,
26 const Aabb& area) noexcept {
27 const auto axis = [](float center, float extent, float speed, float low, float high) {
28 const bool beyond_high = center - extent >= high && speed >= 0.0F;
29 const bool beyond_low = center + extent <= low && speed <= 0.0F;
30 return beyond_high || beyond_low;
31 };
32 return axis(position.x, half.x, velocity.x, area.min.x, area.max.x) ||
33 axis(position.y, half.y, velocity.y, area.min.y, area.max.y);
34}
35
48inline void despawn_outside(Registry& registry, const Aabb& area, std::vector<Entity>& despawned) {
49 despawned.clear();
50 registry.view<OffscreenDespawn, Transform>().each([&registry, &area,
51 &despawned](Entity entity,
52 const OffscreenDespawn& despawn,
53 const Transform& transform) {
54 const auto velocity = registry.try_get<Velocity>(entity);
55 const Vec2 speed = velocity ? velocity->get().value : Vec2{};
56 const Vec2 margin{despawn.margin, despawn.margin};
57 const Aabb bounds{.min = area.min - margin, .max = area.max + margin};
58 if (left_for_good(transform.position, half_extent_of(registry, entity), speed, bounds)) {
59 despawned.push_back(entity);
60 }
61 });
62 for (const Entity entity : despawned) {
63 registry.destroy(entity);
64 }
65}
66
67} // namespace aegis2d::ecs
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 destroy(Entity entity)
Destroys an entity and removes all its components.
View< Components... > view()
View on the entities that own all the listed components.
Definition registry.hpp:222
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).
Movement: the direction of a player's input, velocities applied each tick, sine motions on top of the...
Entity-Component-System used by client and server.
Definition animation.hpp:13
Vec2 half_extent_of(const Registry &registry, Entity entity)
Half size of what must stay visible of an entity.
Definition movement.hpp:98
bool left_for_good(Vec2 position, Vec2 half, Vec2 velocity, const Aabb &area) noexcept
Tells whether a moving box has left an area and will not come back.
Definition despawn.hpp:25
void despawn_outside(Registry &registry, const Aabb &area, std::vector< Entity > &despawned)
Destroys the entities that have left the play area for good.
Definition despawn.hpp:48
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.
Axis-aligned box given by its two corners, in logical pixels.
Definition collision.hpp:20
Vec2 max
Bottom-right corner (largest x and y).
Definition collision.hpp:24
Vec2 min
Top-left corner (smallest x and y).
Definition collision.hpp:22
Entity destroyed once it has left the play area for good.
Definition components.hpp:217
float margin
Distance beyond each edge of the area, in logical pixels, before the entity counts as gone.
Definition components.hpp:220
Where an entity is.
Definition components.hpp:17
Vec2 position
Center of the entity.
Definition components.hpp:19
How fast an entity moves.
Definition components.hpp:25
Vec2 value
Speed in logical pixels per second.
Definition components.hpp:27