R-Type  2
Doom but in better
Loading...
Searching...
No Matches
WeaponSystem.cpp
Go to the documentation of this file.
1#include "WeaponSystem.hpp"
2#include "IndexedZipper.hpp"
3#include "SpawnSystem.hpp"
4#include "Time.hpp"
5
7{
8 for (auto &&[idx, weapon, position, type] : IndexedZipper(weapons, positions, types)) {
9 if (!weapon || !position || !type) continue;
10
11 weapon->cooldown -= Time::deltaTime;
12 if (type->type == type_enum::MONSTER && !weapon->shot) weapon->shot = true;
13
14 if (weapon->shot && weapon->cooldown <= 0) {
15 spawn_missile(r, position->X, position->Y, type->type);
16 r.dispatcher->notify({P_SHOOT, idx, {0, 0, 0, "", {position->X, position->Y}}});
17 weapon->cooldown = 1.0f / weapon->fire_rate;
18 weapon->shot = false;
19 }
20 }
21}
22
23void make_shot(Registry &r, std::size_t id)
24{
25 auto &weapons = r.get_components<Weapon>();
26 if (weapons.size() <= id) return;
27
28 if (weapons[id])
29 weapons[id]->shot = true;
30}
@ P_SHOOT
Represents a player shooting event.
void spawn_missile(Registry &r, const float pos_x, const float pos_y, const type_enum owner)
Spawns a missile at the specified position.
Registry * r
Definition Tests.cpp:4
@ MONSTER
Definition Type.hpp:13
void weapon_system(Registry &r, ComponentContainer< Weapon > &weapons, ComponentContainer< Position > &positions, ComponentContainer< Type > &types)
Handles the firing of weapons and updates their states.
void make_shot(Registry &r, std::size_t id)
Changes the state of the weapon.
Manages a collection of components associated with entities in an ECS (Entity-Component-System) archi...
void notify(const GameMessage &event)
Notifies the subscribed listener of an event.
Combines multiple containers into a single iterable unit, iterating over corresponding elements from ...
Manages entities and their associated components, enabling the creation, deletion,...
Definition Registry.hpp:23
EventDispatcher * dispatcher
Definition Registry.hpp:191
ComponentContainer< Component > & get_components()
Retrieves the component container for a specific type.
Definition Registry.hpp:121
float deltaTime
The time difference between the current frame and the last frame.
Definition Time.cpp:4
Represents a weapon and its attributes in the game.
Definition Weapon.hpp:11
bool shot
Definition Weapon.hpp:15