Add 'Step 0: ECS Architecture'

master
sadpmpk 2022-12-30 03:31:40 -05:00
parent 0bec989682
commit 2a45f4fd45
1 changed files with 59 additions and 0 deletions

@ -0,0 +1,59 @@
The first step to the project is to implement the ECS architecture. Everything is adapted from the lecture series.
# Summary: ECS
To put it simply, ECS is an architecture to organise data. In this implementation, a component is just a data store; an entity is a collection of components; a system will describe the interaction with the components and entities. A better explanation and illustration can be found in the lecture series. This is how I understand it.
## Implementation
An entity is implemented as such in `entity.h`:
```
typedef struct Entity
{
unsigned long m_id; // Unique id for the entity
EntityTag_t m_tag; // Entity tag for quick reference
bool m_alive;
struct sc_map_64 components; // Component map
}Entity_t
```
It is expected that an entity can only have at most one of any given components. Thus, a map is used to keep track of it. Each component is given a tag (implemented as enum) to identify it. One commonly used component is the Bounding Box component (will refer to it as 'bbox'). This is implemented in `components.h`.
```
#define N_COMPONENTS 1
enum ComponentEnum
{
CBBOX_COMP_T,
};
typedef enum ComponentEnum ComponentEnum_t;
typedef struct _CBBox_t
{
int x;
int y;
int width;
int height;
}CBBox_t;
```
The components header file will be updated as more components are needed.
# Entity Manager
Following the lecture series, the entity manager is implemented. As C is used, typical OO-style calls cannot be used. There is also no concept of public/private/friend/ members as in C++.
The entity manager struct is implemented in `entManager.h`:
```
typedef struct EntityManager
{
// All fields are Read-Only
struct sc_map_64v entities; // id : entity
struct sc_map_64v entities_map[N_TAGS]; // [{id: ent}]
struct sc_map_64v component_map[N_COMPONENTS]; // [{id: comp}, ...]
struct sc_queue_uint to_add;
struct sc_queue_uint to_remove;
}EntityManager_t;
```
The first three maps are to quickly accesses the respective entities and components.
- First map is general map from entity id to the entity pointer.
- Second map is a tag-specific map from entity id to the entity pointer. This is used to quickly select a group of related entities
- Third map is a component-specific map from component id to the component pointer. This is used to quickly select a group of components, independent of the entity having them.
Due to the implementation of the data structure, it can only handle void pointer. Although it is possible to implement a map to a struct pointer, I didn't really bother. Therefore, the pointer obtained has to be casted according. For safer development, I should look into implementing a map to struct instead.
The `to_add` and `to_remove` fields are to handle _iterator invalidation_. This is explained in the lecture series. This arises when the data structure is modified _while_ iterating through it. When adding or removing an entity, they are not immediately added to the map but is added to the respective fields. After that, the entity manager will be updated to properly include the newly added entity and drop the removed entity. Due to this design, this update procedure has to be explicitly called.
# Memory Pool