Skip to content

Instantly share code, notes, and snippets.

@aganm
Last active March 25, 2024 19:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aganm/db0c4911c1f3ee3c14383442376c8b70 to your computer and use it in GitHub Desktop.
Save aganm/db0c4911c1f3ee3c14383442376c8b70 to your computer and use it in GitHub Desktop.
Zero Lines ECS
// A component is a plain old datatype
typedef struct Position {
float x;
float y;
} Position;
// An entity can have 0 .. N components
typedef struct Archetype1 {
int count;
Position position[1024];
} Archetype1 ;
void MoveXSystem(
Position *e_position,
int entity_count,
float move_x)
{
for (int e = 0; e < entity_count; e++) {
e_position[e].x += move_x;
}
}
int main() {
Archetype1 archetype1 = { 0 };
int e = archetype1.count++; // An entity is a unique identifier
archetype1.position[e].x = 100.f;
archetype1.position[e].y = 50.f;
// A system is logic matched with entities based on their components
MoveXSystem(archetype1.position, archetype1.count, 10.f);
}
// A component is a plain old datatype
typedef struct Position {
float x;
float y;
} Position;
// An entity can have 0 .. N components
typedef struct SparseStorage {
int count;
Position position[1024];
int position_slot[1024];
int position_count;
} SparseStorage;
void MoveXSystem(
Position *position,
int *e_position,
int *entity,
int entity_count,
float move_x)
{
for (int ee = 0; ee < entity_count; ee++) {
int e = entity[ee];
position[e_position[e]].x += move_x;
}
}
int main() {
SparseStorage sparse = { 0 };
int e = sparse.count++; // An entity is a unique identifier
sparse.position_slot[e] = sparse.position_count++;
sparse.position[sparse.position_slot[e]].x = 100.f;
sparse.position[sparse.position_slot[e]].y = 50.f;
// A system is logic matched with entities based on their components
MoveXSystem(sparse.position, sparse.position_slot, (int[]){ e }, 1, 10.f);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment