R-Type  2
Doom but in better
Loading...
Searching...
No Matches
CollisionSystem.cpp
Go to the documentation of this file.
1#include "CollisionSystem.hpp"
2
3#include "IndexedZipper.hpp"
4#include "Components.hpp"
5#include "SpawnSystem.hpp"
6#include "WinSystem.hpp"
7
8void collision_player_missile(Registry &r, std::size_t &entity1, std::size_t &entity2)
9{
10 std::cout << "Collision Player Missile" << std::endl;
11 auto &&[healths, teams] = r.get_component_array<Health, Team>();
12 if (teams[entity2]->team == team_enum::ENEMY) {
13
14 healths[entity1]->current -= 1;
15 r.dispatcher->notify({messageType::P_DAMAGE, entity1, {0, 0, 0, "", {0, 0}}});
16
17 if (healths[entity1]->current == 0) {
18 r.kill_entity(Entity(entity1));
19 r.dispatcher->notify({messageType::P_KILL, entity1, {0, 0, 0, "", {0, 0}}});
20 }
21
22 r.kill_entity(Entity(entity2));
23 r.dispatcher->notify({messageType::P_KILL, entity2, {0, 0, 0, "", {0, 0}}});
24 }
25}
26
27void collision_monster_missile(Registry &r, std::size_t &entity1, std::size_t &entity2)
28{
29 std::cout << "Collision Monster Missile" << std::endl;
30 auto &&[healths, teams, loot_drops, positions] = r.get_component_array<Health, Team, LootDrop, Position>();
31 if (teams[entity2]->team == team_enum::ALLY) {
32 healths[entity1]->current -= 1;
33 r.dispatcher->notify({messageType::P_DAMAGE, entity1, {0, 0, 0, "", {0, 0}}});
34
35 if (healths[entity1]->current == 0) {
36 // auto loot = loot_drops[entity1];
37 // spawn_powerup(r, positions[entity1]->X, positions[entity1]->Y, loot->loot);
38
40 r.kill_entity(Entity(entity1));
41 r.dispatcher->notify({messageType::P_KILL, entity1, {0, 0, 0, "", {0, 0}}});
42 }
43
44 r.kill_entity(Entity(entity2));
45 r.dispatcher->notify({messageType::P_KILL, entity2, {0, 0, 0, "", {0, 0}}});
46 }
47}
48
49void collision_player_obstacle(Registry &r, std::size_t &entity1, std::size_t &entity2)
50{
51 std::cout << "Collision Player Obstacle" << std::endl;
52 auto &healths = r.get_components<Health>();
53 healths[entity1]->current -= 1;
54 r.dispatcher->notify({messageType::P_DAMAGE, entity1, {0, 0, 0, "", {0, 0}}});
55}
56
57void collision_monster_obstacle(Registry &r, std::size_t &entity1, std::size_t &entity2)
58{
59 //idk
60}
61
62void collision_player_powerup(Registry &r, std::size_t &entity1, std::size_t &entity2)
63{
64 std::cout << "Collision Player Powerup" << std::endl;
65 auto &&[healths, loots] = r.get_component_array<Health, LootDrop>();
66 if (loots[entity2]->loot == loot_enum::HEALTH_DROP) {
67 healths[entity1]->current = healths[entity1]->max;
68 r.dispatcher->notify({messageType::P_HEAL, entity1, {0, 0, 0, "", {0, 0}}});
69 }
70 else if (loots[entity2]->loot == loot_enum::POWERUP_DROP) {
71 auto &powerups = r.get_components<PowerUp>();
72 powerups[entity1]->enabled = true;
73 r.dispatcher->notify({messageType::P_BUFF, entity1, {0, 0, 0, "", {0, 0}}});
74 }
75}
76
77void collision_obstacle_missile(Registry &r, std::size_t &entity1, std::size_t &entity2)
78{
79 std::cout << "Collision Obstacle Missile" << std::endl;
80 r.kill_entity(Entity(entity2));
81 r.dispatcher->notify({messageType::P_KILL, entity2, {0, 0, 0, "", {0, 0}}});
82}
83
84namespace std {
85 template <>
86 struct hash<std::pair<type_enum, type_enum>> {
87 std::size_t operator()(const std::pair<type_enum, type_enum>& p) const {
88 return (hash<type_enum>()(p.first) ^ (hash<type_enum>()(p.second) << 1));
89 }
90 };
91}
92
93const std::unordered_map<std::pair<type_enum, type_enum>, std::function<void(Registry &, std::size_t &, std::size_t &)>> collisions = {
100};
101
103{
104 for (auto &&[idx1, pos1, col1, type1] : IndexedZipper(positions, colliders, types)) {
105 if (!pos1 || !col1 || !type1) continue;
106
107 for (auto &&[idx2, pos2, col2, type2] : IndexedZipper(positions, colliders, types)) {
108 if (!pos2 || !col2 || !type2 || idx1 == idx2) continue;
109
110 auto key = std::make_pair(type1->type, type2->type);
111 if (collisions.find(key) != collisions.end()) {
112 float dx = (pos1->X - pos2->X);
113 float dy = (pos1->Y - pos2->Y);
114 float distance_squared = dx * dx + dy * dy;
115 float radius_sum = col1->radius + col2->radius;
116 if (distance_squared <= radius_sum * radius_sum)
117 collisions.at(key)(r, idx1, idx2);
118 }
119 }
120 }
121}
void collision_system(Registry &r, ComponentContainer< Position > &positions, ComponentContainer< Collider > &colliders, ComponentContainer< Type > &types)
Collision detection and resolution system.
void collision_monster_obstacle(Registry &r, std::size_t &entity1, std::size_t &entity2)
const std::unordered_map< std::pair< type_enum, type_enum >, std::function< void(Registry &, std::size_t &, std::size_t &)> > collisions
void collision_obstacle_missile(Registry &r, std::size_t &entity1, std::size_t &entity2)
void collision_player_powerup(Registry &r, std::size_t &entity1, std::size_t &entity2)
void collision_player_missile(Registry &r, std::size_t &entity1, std::size_t &entity2)
void collision_monster_missile(Registry &r, std::size_t &entity1, std::size_t &entity2)
void collision_player_obstacle(Registry &r, std::size_t &entity1, std::size_t &entity2)
@ P_BUFF
Represents a buff event.
@ P_HEAL
Represents a healing event.
@ P_DAMAGE
Represents a damage event.
@ P_KILL
Represents a kill event.
@ HEALTH_DROP
Definition LootDrop.hpp:13
@ POWERUP_DROP
Definition LootDrop.hpp:12
@ ALLY
Definition Team.hpp:11
@ ENEMY
Definition Team.hpp:12
Registry * r
Definition Tests.cpp:4
@ OBSTACLE
Definition Type.hpp:15
@ POWERUP
Definition Type.hpp:16
@ MISSILE
Definition Type.hpp:14
@ PLAYER
Definition Type.hpp:12
@ MONSTER
Definition Type.hpp:13
Manages a collection of components associated with entities in an ECS (Entity-Component-System) archi...
Represents an entity in an Entity-Component-System (ECS) architecture.
Definition Entity.hpp:12
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
void kill_entity(const Entity &entity)
Deletes an entity and removes all its components.
Definition Registry.cpp:35
EventDispatcher * dispatcher
Definition Registry.hpp:191
std::tuple< ComponentContainer< Components > &... > get_component_array()
Retrieves component containers for multiple component types.
Definition Registry.hpp:110
ComponentContainer< Component > & get_components()
Retrieves the component container for a specific type.
Definition Registry.hpp:121
int enemies_left
Definition WinSystem.cpp:6
Represents the health of an entity, including current and maximum health values.
Definition Health.hpp:10
int current
Definition Health.hpp:11
Represents a loot drop with a specific loot type.
Definition LootDrop.hpp:23
Represents the position of an entity in a 2D space.
Definition Position.hpp:10
Represents a power-up item and its enabled state.
Definition PowerUp.hpp:11
bool enabled
Definition PowerUp.hpp:12
Represents the team affiliation of an entity.
Definition Team.hpp:22
std::size_t operator()(const std::pair< type_enum, type_enum > &p) const