Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
weapons.hpp
Go to the documentation of this file.
1
4#pragma once
12
13#include <span>
14#include <vector>
15
16namespace aegis2d::shmup {
17
27inline bool pull_trigger(Weapon& weapon, float delta, bool pulled) noexcept {
28 weapon.reload -= delta;
29 const bool fires = pulled && weapon.reload <= 0.0F;
30 if (fires) {
31 weapon.reload += weapon.cooldown;
32 }
33 if (weapon.reload < 0.0F) {
34 weapon.reload = 0.0F;
35 }
36 return fires;
37}
38
48inline void trigger_weapons(ecs::Registry& registry, const ecs::Tick& tick,
49 std::vector<ecs::Entity>& shooters) {
50 shooters.clear();
51 const float delta = tick.delta();
52 registry.view<Weapon, ecs::PlayerInput>().each(
53 [delta, &shooters](ecs::Entity entity, Weapon& weapon, const ecs::PlayerInput& input) {
54 if (pull_trigger(weapon, delta, input.holds(ecs::Action::Fire))) {
55 shooters.push_back(entity);
56 }
57 });
58}
59
72inline void trigger_auto_weapons(ecs::Registry& registry, const ecs::Tick& tick,
73 std::vector<ecs::Entity>& shooters) {
74 shooters.clear();
75 const float delta = tick.delta();
76 registry.view<Weapon, AutoTrigger>().each(
77 [&registry, delta, &shooters](ecs::Entity entity, Weapon& weapon, const AutoTrigger&) {
78 if (!registry.has<ecs::PlayerInput>(entity) && pull_trigger(weapon, delta, true)) {
79 shooters.push_back(entity);
80 }
81 });
82}
83
85struct ShotFired {
91 bool player = false;
92
94 friend bool operator==(const ShotFired&, const ShotFired&) = default;
95};
96
105inline void publish_shots(core::EventBus& events, const ecs::Registry& registry,
106 std::span<const ecs::Entity> shooters) {
107 for (const ecs::Entity shooter : shooters) {
108 const auto transform = registry.try_get<ecs::Transform>(shooter);
109 events.publish(ShotFired{.shooter = shooter,
110 .position = transform ? transform->get().position : Vec2{},
111 .player = registry.has<ecs::PlayerInput>(shooter)});
112 }
113}
114
115} // namespace aegis2d::shmup
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
bool has(Entity entity) const
Tells whether an entity has every listed component.
Definition registry.hpp:114
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).
Shoot'em up building blocks on top of the ECS: player ships, their controls, weapons and their shots....
Definition components.hpp:5
bool pull_trigger(Weapon &weapon, float delta, bool pulled) noexcept
Counts a weapon's reload down by one tick and fires it if the trigger is pulled.
Definition weapons.hpp:27
void trigger_auto_weapons(ecs::Registry &registry, const ecs::Tick &tick, std::vector< ecs::Entity > &shooters)
Finds the entities that fire on their own during this tick and reloads them.
Definition weapons.hpp:72
void trigger_weapons(ecs::Registry &registry, const ecs::Tick &tick, std::vector< ecs::Entity > &shooters)
Finds the ships that fire during this tick and reloads their weapons.
Definition weapons.hpp:48
void publish_shots(core::EventBus &events, const ecs::Registry &registry, std::span< const ecs::Entity > shooters)
Publishes a ShotFired for each entity that fires during the tick.
Definition weapons.hpp:105
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.
Actions a player holds during the tick, as received from its client.
Definition components.hpp:184
constexpr bool holds(Action action) const noexcept
Tells whether an action is held.
Definition components.hpp:191
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
Weapon that fires on its own, as soon as it is reloaded (enemies).
Definition components.hpp:31
An entity fired: one shot leaves it during the tick.
Definition weapons.hpp:85
bool player
true for a player's ship (it has a PlayerInput), false for an enemy.
Definition weapons.hpp:91
friend bool operator==(const ShotFired &, const ShotFired &)=default
Compares the events.
ecs::Entity shooter
Entity that fired.
Definition weapons.hpp:87
Vec2 position
Where it was (its Transform), the origin if it has none.
Definition weapons.hpp:89
Gun of a ship: how often it can fire.
Definition components.hpp:18
Systems and the scheduler that updates them in order, once per tick.