Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
ships.hpp
Go to the documentation of this file.
1
3#pragma once
12
13namespace aegis2d::shmup {
14
23inline void steer_player_ships(ecs::Registry& registry, [[maybe_unused]] const ecs::Tick& tick) {
25 [](const PlayerShip& ship, const ecs::PlayerInput& input, ecs::Velocity& velocity) {
26 velocity.value = ecs::direction_of(input) * ship.speed;
27 });
28}
29
40inline void clamp_player_ships(ecs::Registry& registry, const ecs::Aabb& area) {
41 registry.view<PlayerShip, ecs::Transform>().each(
42 [&registry, &area](ecs::Entity entity, const PlayerShip&, ecs::Transform& transform) {
43 transform.position =
44 ecs::clamp_into(transform.position, ecs::half_extent_of(registry, entity), area);
45 });
46}
47
57 float speed = PlayerShip{}.speed) {
58 const ecs::Entity ship = registry.create();
59 registry.emplace<ecs::Transform>(ship, position);
60 registry.emplace<ecs::Velocity>(ship);
61 registry.emplace<ecs::PlayerInput>(ship);
62 registry.emplace<PlayerShip>(ship, speed);
63 return ship;
64}
65
66} // namespace aegis2d::shmup
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
Entity create()
Creates an entity without components.
Component & emplace(Entity entity, Args &&... args)
Gives an entity a component built in place.
Definition registry.hpp:76
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...
Vec2 direction_of(const PlayerInput &input) noexcept
Direction a player's input points to.
Definition movement.hpp:23
Vec2 half_extent_of(const Registry &registry, Entity entity)
Half size of what must stay visible of an entity.
Definition movement.hpp:98
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
Shoot'em up building blocks on top of the ECS: player ships, their controls, weapons and their shots....
Definition components.hpp:5
void clamp_player_ships(ecs::Registry &registry, const ecs::Aabb &area)
Keeps every player ship inside the play area.
Definition ships.hpp:40
void steer_player_ships(ecs::Registry &registry, const ecs::Tick &tick)
Sets the velocity of every player ship from its input.
Definition ships.hpp:23
ecs::Entity spawn_player_ship(ecs::Registry &registry, Vec2 position, float speed=PlayerShip{}.speed)
Creates a player ship ready to be steered.
Definition ships.hpp:56
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.
Components of the shoot'em up module: player ships and weapons.
Axis-aligned box given by its two corners, in logical pixels.
Definition collision.hpp:20
Actions a player holds during the tick, as received from its client.
Definition components.hpp:184
One simulation step, as the systems see it.
Definition system.hpp:23
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
Ship steered by a player's input (ecs::PlayerInput).
Definition components.hpp:8
float speed
Speed in logical pixels per second, the same in every direction.
Definition components.hpp:10
Systems and the scheduler that updates them in order, once per tick.