Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
movement.hpp
Go to the documentation of this file.
1
4#pragma once
11
12#include <algorithm>
13#include <cmath>
14#include <numbers>
15
16namespace aegis2d::ecs {
17
23[[nodiscard]] inline Vec2 direction_of(const PlayerInput& input) noexcept {
24 Vec2 direction{};
25 if (input.holds(Action::Left)) {
26 direction.x -= 1.0F;
27 }
28 if (input.holds(Action::Right)) {
29 direction.x += 1.0F;
30 }
31 if (input.holds(Action::Up)) {
32 direction.y -= 1.0F;
33 }
34 if (input.holds(Action::Down)) {
35 direction.y += 1.0F;
36 }
37 if (direction.x != 0.0F && direction.y != 0.0F) {
38 direction /= std::numbers::sqrt2_v<float>;
39 }
40 return direction;
41}
42
49inline void apply_velocity(Registry& registry, const Tick& tick) {
50 const float delta = tick.delta();
51 registry.view<Transform, Velocity>().each(
52 [delta](Transform& transform, const Velocity& velocity) {
53 transform.position += velocity.value * delta;
54 });
55}
56
61[[nodiscard]] inline Vec2 sine_offset(const SineMotion& motion, float time) noexcept {
62 if (motion.period <= 0.0F) {
63 return Vec2{};
64 }
65 const float angle = 2.0F * std::numbers::pi_v<float> * time / motion.period;
66 return motion.amplitude * std::sin(angle);
67}
68
80inline void apply_sine_motion(Registry& registry, const Tick& tick) {
81 const float delta = tick.delta();
82 registry.view<SineMotion, Transform>().each([delta](SineMotion& motion, Transform& transform) {
83 const Vec2 before = sine_offset(motion, motion.elapsed);
84 motion.elapsed += delta;
85 transform.position += sine_offset(motion, motion.elapsed) - before;
86 if (motion.period > 0.0F) {
87 motion.elapsed = std::fmod(motion.elapsed, motion.period);
88 }
89 });
90}
91
98[[nodiscard]] inline Vec2 half_extent_of(const Registry& registry, Entity entity) {
99 if (const auto sprite = registry.try_get<Sprite>(entity)) {
100 return sprite->get().size * 0.5F;
101 }
102 if (const auto collider = registry.try_get<Collider>(entity)) {
103 return collider->get().size * 0.5F;
104 }
105 return Vec2{};
106}
107
115[[nodiscard]] inline Vec2 clamp_into(Vec2 position, Vec2 half, const Aabb& area) noexcept {
116 const auto axis = [](float value, float margin, float low, float high) {
117 const float first = low + margin;
118 const float last = high - margin;
119 if (first > last) {
120 return (low + high) * 0.5F;
121 }
122 return std::clamp(value, first, last);
123 };
124 return Vec2{axis(position.x, half.x, area.min.x, area.max.x),
125 axis(position.y, half.y, area.min.y, area.max.y)};
126}
127
128} // 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
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).
Entity-Component-System used by client and server.
Definition animation.hpp:13
void apply_velocity(Registry &registry, const Tick &tick)
Moves every entity by its velocity for one tick.
Definition movement.hpp:49
Vec2 sine_offset(const SineMotion &motion, float time) noexcept
Offset of a sine motion from its path at a time.
Definition movement.hpp:61
Vec2 direction_of(const PlayerInput &input) noexcept
Direction a player's input points to.
Definition movement.hpp:23
void apply_sine_motion(Registry &registry, const Tick &tick)
Moves every entity with a sine motion along its wave for one tick.
Definition movement.hpp:80
Vec2 half_extent_of(const Registry &registry, Entity entity)
Half size of what must stay visible of an entity.
Definition movement.hpp:98
@ Up
Move up (towards y = 0).
@ Right
Move right.
Vec2 clamp_into(Vec2 position, Vec2 half, const Aabb &area) noexcept
Keeps a position inside an area, with a margin on each side.
Definition movement.hpp:115
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
Axis-aligned box an entity collides with.
Definition components.hpp:101
Actions a player holds during the tick, as received from its client.
Definition components.hpp:184
Sine wave added to an entity's movement, like an enemy weaving up and down.
Definition components.hpp:230
float period
Seconds for one full wave; a period of zero or less does not move the entity.
Definition components.hpp:235
float elapsed
Seconds into the current wave, kept in [0, period). Starting above 0 shifts the phase,...
Definition components.hpp:238
What an entity looks like.
Definition components.hpp:34
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
How fast an entity moves.
Definition components.hpp:25
Vec2 value
Speed in logical pixels per second.
Definition components.hpp:27
Systems and the scheduler that updates them in order, once per tick.