Skip to content

Commit 6a61818

Browse files
authored
Merge pull request #96 from ASM-Studios/dev
Dev
2 parents 35f242d + dfd9e42 commit 6a61818

File tree

8 files changed

+246
-112
lines changed

8 files changed

+246
-112
lines changed

docs/protocol.txt

Lines changed: 29 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,28 @@ RFC 9999 et des brouettes - RType Binary Data Transfer Procol (RBDTP)
1919
data (such as player movement or chat content).
2020

2121
To ensure no extra memory padding is added, all structures are packed.
22-
To ensure packet integrity, a CRC32 checksum is included in the header.
2322

24-
struct Header {
23+
class NonTypedQuery {
2524
RequestType requestType;
25+
std::array<char, MAX_PAYLOAD_SIZE> _fill;
2626
};
2727

2828
template <typename Payload>
29-
struct Query {
30-
Header header;
29+
class TypedQuery {
30+
RequestType requestType;
3131
Payload payload;
32+
std::array<char, MAX_PAYLOAD_SIZE - sizeof(Payload)> _fill;
3233
};
33-
3434

35-
template <typename Payload>
36-
struct Request {
37-
uint32_t checksum;
38-
Query<Payload> query;
35+
NonTypedQuery and TypedQuery are same elements except that TypedQuery has
36+
a typed payload.
37+
38+
class RawRequest {
39+
NonTypedQuery query;
3940
};
4041

42+
RawRequest is the class used by the socket to send queries.
43+
4144
4. Protocol
4245

4346
ID are to be defined.
@@ -46,110 +49,30 @@ RFC 9999 et des brouettes - RType Binary Data Transfer Procol (RBDTP)
4649

4750
4.1.1. Movement
4851

49-
Key pressed:
52+
Input:
5053
- ID: 0x0000
51-
- Request: Query<KeyPressed>
52-
53-
4.1.2. Chat
54-
55-
Send a message:
56-
- ID: 0x0000
57-
- Request: Query<SendMessage>
58-
59-
4.1.3. Ping
60-
61-
Ping:
62-
- ID: 0x0000
63-
- Request: Query<Empty>
54+
- Request: ecs::component::Input
6455

6556
4.2. Server to Client
6657

67-
4.2.1. Entity
68-
69-
Entity updated:
70-
- ID: 0x0000
71-
- Request: Query<EntityUpdatedRequest>
72-
73-
Entity destroyed:
74-
- ID: 0x0000
75-
- Request: Query<EntityDestroyedRequest>
76-
77-
4.2.2. Event
78-
79-
Projectile fired:
80-
- ID: 0x0000
81-
- Request: Query<ProjectileFiredRequest>
82-
83-
Collision:
84-
- ID: 0x0000
85-
- Request: Query<CollisionRequest>
86-
87-
Powerup spawned:
88-
- ID: 0x0000
89-
- Request: Query<PowerupSpawnedRequest>
90-
91-
Powerup collected:
92-
- ID: 0x0000
93-
- Request: Query<PowerupCollectedRequest>
94-
95-
Explosion:
96-
- ID: 0x0000
97-
- Request: Query<ExplosionRequest>
98-
99-
Receive a message:
100-
- ID: 0x0000
101-
- Request: Query<ReceiveMessageRequest>
102-
103-
5. Structures
104-
105-
struct Empty {};
106-
107-
enum StatusCode {
108-
OK,
109-
KO
110-
};
111-
112-
struct CreateLobbyRequest {
113-
char username[128];
114-
};
115-
116-
struct CreateLobbyResponse {
117-
uint32_t lobbyId;
118-
};
119-
120-
struct JoinLobbyRequest {
121-
char username[128];
122-
uint32_t lobbyId;
123-
};
124-
125-
struct KeyPressedRequest {
126-
char key;
127-
};
128-
129-
using KeyReleasedRequest = KeyPressedRequest;
58+
Update player:
59+
- ID: 0x0000
60+
- Request: UpdatePlayer
13061

131-
struct SendMessageRequest {
132-
char message[256];
133-
};
62+
Update team player:
63+
- ID: 0x0000
64+
- Request: UpdateTeamPlayer
13465

135-
struct PingResponse {
136-
uint64_t timestamp;
137-
};
66+
Create entity:
67+
- ID: 0x0000
68+
- Request: CreateEntity
13869

139-
struct EntityCreatedRequest {
140-
uint32_t entityId;
141-
uint32_t x;
142-
uint32_t y;
143-
};
70+
Update entity:
71+
- ID: 0x0000
72+
- Request: UpdateEntity
14473

145-
struct EntityDestroyedRequest {
146-
uint32_t entityId;
147-
};
74+
Destroy entity:
75+
- ID: 0x0000
76+
- Request: DestroyEntity
14877

149-
struct EntityUpdatedRequest {
150-
uint32_t entityId;
151-
uint32_t x;
152-
uint32_t y;
153-
};
15478

155-
// TO BE CONTINUED

shared/ECS/include/Registry.inl

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,18 @@
55
#include <unordered_map>
66

77
namespace ecs {
8+
/**
9+
* \brief Create an entity with a unique ID.
10+
*/
811
template <typename... Components>
912
Entity Registry::createEntity() {
1013
int id = this->_generateID();
1114
return this->createEntity(id);
1215
}
1316

17+
/**
18+
* \brief Create an entity with a specific ID.
19+
*/
1420
template <typename... Components>
1521
Entity Registry::createEntity(uint64_t id) {
1622
std::lock_guard<std::mutex> lock(this->_mutex);
@@ -25,6 +31,9 @@ namespace ecs {
2531
return entity;
2632
}
2733

34+
/**
35+
* \brief Get all entities with component.
36+
*/
2837
template <typename Component>
2938
std::unordered_map<Entity, Component> Registry::getEntities() {
3039
std::lock_guard<std::mutex> lock(this->_mutex);
@@ -35,34 +44,56 @@ namespace ecs {
3544
return castedMap;
3645
}
3746

47+
/**
48+
* \brief Add a component to an entity.
49+
*/
3850
template <typename Component>
3951
void Registry::addComponent(const Entity& entity) {
4052
std::lock_guard<std::mutex> lock(this->_mutex);
4153
_components[typeid(Component)][entity] = Component();
4254
}
4355

56+
/**
57+
* \brief Add multiple components to an entity.
58+
*/
4459
template <typename... Components>
4560
void Registry::addComponents(const Entity& entity) {
4661
(addComponent<Components>(entity), ...);
4762
}
4863

64+
/**
65+
* \brief Set a component to an entity.
66+
*
67+
* Set a component to an entity. If the entity already has a component of the same type, it will be replaced.
68+
* If the entity does not have a component of the same type, it will be added.
69+
*/
4970
template <typename Component>
5071
void Registry::setComponent(const Entity& entity, Component component) {
5172
std::lock_guard<std::mutex> lock(this->_mutex);
5273
_components[typeid(Component)][entity] = component;
5374
}
5475

76+
/**
77+
* \brief Check if an entity has a component.
78+
*/
5579
template <typename Component>
5680
bool Registry::contains(const Entity& entity) {
5781
std::lock_guard<std::mutex> lock(this->_mutex);
5882
return _components[typeid(Component)].contains(entity);
5983
}
6084

85+
/**
86+
* \brief Check if an entity has a component with a specific value.
87+
*/
6188
template <typename Component>
6289
bool Registry::contains(const Entity& entity, const Component& component) {
6390
return this->contains<Component>(entity) && getComponent<Component>(entity) == component;
6491
}
6592

93+
/**
94+
* \brief Get a component from an entity.
95+
* \throws ComponentNotFound if the entity does not have the component.
96+
*/
6697
template <typename Component>
6798
Component& Registry::getComponent(const Entity& entity) {
6899
if (!contains<Component>(entity)) {

shared/ECS/src/Registry.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ namespace ecs {
2020
return this->_message.c_str();
2121
}
2222

23+
/**
24+
* \brief Generate a unique ID for an entity.
25+
*/
2326
int Registry::_generateID() {
2427
std::lock_guard<std::mutex> lock(this->_mutex);
2528
int id = 1;
@@ -29,14 +32,23 @@ namespace ecs {
2932
return id;
3033
}
3134

35+
/**
36+
* \brief Create a registry with a unique ID.
37+
*/
3238
Registry::Registry(uint8_t id) :
3339
_id(id) {}
3440

41+
/**
42+
* \brief Get all entities of the registry.
43+
*/
3544
std::set<Entity> Registry::getEntities() {
3645
std::lock_guard<std::mutex> lock(this->_mutex);
3746
return _entities;
3847
}
3948

49+
/**
50+
* \brief Get number of components of an entity.
51+
*/
4052
std::size_t Registry::getNoComponent(const Entity& entity) {
4153
std::lock_guard<std::mutex> lock(this->_mutex);
4254
std::size_t noComponent = 0;
@@ -48,19 +60,28 @@ namespace ecs {
4860
return noComponent;
4961
}
5062

63+
/**
64+
* \brief Remove all components of an entity.
65+
*/
5166
void Registry::reset(const Entity& entity) {
5267
std::lock_guard<std::mutex> lock(this->_mutex);
5368
for (auto& component: this->_components) {
5469
component.second.erase(entity);
5570
}
5671
}
5772

73+
/**
74+
* \brief Remove all entities and components.
75+
*/
5876
void Registry::resetAll() {
5977
std::lock_guard<std::mutex> lock(this->_mutex);
6078
std::erase_if(this->_components, [this](const auto& item) { return !(item.first == typeid(ecs::component::Player)); });
6179
std::erase_if(this->_entities, [this](const auto& item) { return getNoComponent(item) == 0; });
6280
}
6381

82+
/**
83+
* \brief Remove an entity and its components.
84+
*/
6485
void Registry::removeEntity(const Entity& entity) {
6586
std::lock_guard<std::mutex> lock(this->_mutex);
6687
_entities.erase(entity);
@@ -69,6 +90,9 @@ namespace ecs {
6990
}
7091
}
7192

93+
/**
94+
* \brief Check if an entity is present in registry.
95+
*/
7296
bool Registry::isEntityValid(const Entity& entity) {
7397
std::lock_guard<std::mutex> lock(this->_mutex);
7498
return _entities.contains(entity);

shared/GameLogic/src/BehaviorFunc.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@
1212

1313
constexpr auto SPEED = 400;
1414

15+
/**
16+
* @brief Update the bullet position
17+
*
18+
* @param mode The game logic mode
19+
* @param bullet The bullet entity
20+
* @param timePerTick The time per tick
21+
*/
1522
void BehaviorFunc::updateBullet(GameLogicMode mode, const ecs::Entity& bullet, float timePerTick) {
1623
ecs::Registry& registry = ecs::RegistryManager::getInstance().getRegistry();
1724
int16_t speed = SPEED * 10 * timePerTick;
@@ -24,6 +31,13 @@ void BehaviorFunc::updateBullet(GameLogicMode mode, const ecs::Entity& bullet, f
2431
}
2532
}
2633

34+
/**
35+
* @brief Update the player sprite sheet from input
36+
*
37+
* @param mode The game logic mode
38+
* @param player The player entity
39+
* @param timePerTick The time per tick
40+
*/
2741
void BehaviorFunc::setSpriteSheetFromInput(GameLogicMode mode, const ecs::Entity& entity, float deltaTime) {
2842
ecs::Registry& registry = ecs::RegistryManager::getInstance().getRegistry();
2943
auto& input = registry.getComponent<ecs::component::Input>(entity);
@@ -66,6 +80,11 @@ void BehaviorFunc::setSpriteSheetFromInput(GameLogicMode mode, const ecs::Entity
6680
}
6781
}
6882

83+
/**
84+
* @brief Send a bullet to all clients
85+
*
86+
* @param entity The entity that shot the bullet
87+
*/
6988
static void sendBullet(const ecs::Entity& entity) {
7089
for (const auto& [destEntity, destClient]: ecs::RegistryManager::getInstance().getRegistry().getEntities<network::Client>()) {
7190
const ecs::Entity& bullet = EntitySchematic::createBullet(entity);
@@ -79,6 +98,13 @@ static void sendBullet(const ecs::Entity& entity) {
7998
}
8099
}
81100

101+
/**
102+
* @brief Handle the input of the player
103+
*
104+
* @param mode The game logic mode
105+
* @param entity The player entity
106+
* @param timePerTick The time per tick
107+
*/
82108
void BehaviorFunc::handleInput(GameLogicMode mode, const ecs::Entity& entity, float timePerTick) {
83109
ecs::Registry& registry = ecs::RegistryManager::getInstance().getRegistry();
84110
auto& input = registry.getComponent<ecs::component::Input>(entity);

0 commit comments

Comments
 (0)