commit e24168ead002e3729dc9a51d11f9b33434e017ef
parent 5f57321a32038b330ad5f44d291ad88df7b14300
Author: bsandro <email@bsandro.tech>
Date: Mon, 7 Sep 2026 00:43:28 +0300
Basic systems workflow; basic actions component with its own small functions call subsystem
Diffstat:
12 files changed, 104 insertions(+), 15 deletions(-)
diff --git a/actions/attack.c b/actions/attack.c
@@ -0,0 +1,10 @@
+#include <stdbool.h>
+#include <stdio.h>
+#include "actions/attack.h"
+#include "core/ecs/entity.h"
+#include "core/world.h"
+
+bool Action_Attack(struct Entity *e, struct World *w) {
+ puts("action attack");
+ return true;
+}
diff --git a/actions/attack.h b/actions/attack.h
@@ -0,0 +1,6 @@
+#pragma once
+
+struct Entity; // fwd
+struct World; // fwd
+
+bool Action_Attack(struct Entity *e, struct World *w);
diff --git a/commands/SPAWN_SWORDSMAN.c b/commands/SPAWN_SWORDSMAN.c
@@ -1,19 +1,26 @@
+#include "stb_ds.h"
#include "core/command.h"
#include "core/ecs/entity.h"
-
-#include "stb_ds.h"
+#include "actions/attack.h"
void cmd_spawn_swordsman(World *w, ...) {
puts("spawn swordsman");
- Entity entity = {0};
- Entity_Add_Component(&entity, (Component){
+ Entity swordsman = {0};
+ Entity_Add_Component(&swordsman, (Component){
.type = COMPONENT_HEALTH,
.health = { .hp=30 }
});
- Entity_Add_Component(&entity, (Component){
+ Entity_Add_Component(&swordsman, (Component){
.type = COMPONENT_MELEE,
.melee = { .strength=30 }
});
- printf("entity has %ld components:\n", arrlen(entity.components));
- printf("%d: %dhp\n", entity.components[0].type, entity.components[0].health.hp);
+ Actions actions = {0};
+ arrput(actions.items, (struct Actions_Item){ .run = Action_Attack });
+ Entity_Add_Component(&swordsman, (Component){
+ .type = COMPONENT_ACTIONS,
+ .actions = actions
+ });
+ World_addEntity(w, &swordsman);
+ printf("entity has %ld components:\n", arrlen(swordsman.components));
+ printf("%d: %dhp\n", swordsman.components[0].type, swordsman.components[0].health.hp);
}
diff --git a/components/actions.h b/components/actions.h
@@ -0,0 +1,14 @@
+#pragma once
+
+struct Entity; // fwd
+struct World; // fwd
+
+typedef bool (Actions_Run)(struct Entity *e, struct World *w);
+
+struct Actions_Item {
+ Actions_Run *run;
+};
+
+typedef struct Actions {
+ struct Actions_Item *items;
+} Actions;
diff --git a/core/ecs/entity.c b/core/ecs/entity.c
@@ -18,7 +18,3 @@ bool Entity_Add_Component(Entity *e, Component c) {
arrput(e->components, c);
return true;
}
-
-bool Entity_Add_System(Entity *e, System *s) {
- return true;
-}
diff --git a/core/ecs/entity.h b/core/ecs/entity.h
@@ -2,7 +2,6 @@
#include <stdbool.h>
#include "core/ecs/component.h"
-#include "core/ecs/system.h"
typedef struct Entity {
int id;
diff --git a/core/ecs/system.h b/core/ecs/system.h
@@ -1,11 +1,11 @@
#pragma once
#include <stdbool.h>
-#include "core/world.h"
-typedef bool (System_Run)(World *w);
+struct World; // fwd
+
+typedef bool (System_Run)(struct World *w);
typedef struct System {
System_Run *run;
- struct System *next;
} System;
diff --git a/core/world.c b/core/world.c
@@ -1,6 +1,21 @@
#include <stddef.h>
+#include "stb_ds.h"
#include "world.h"
World * World_init() {
return NULL;
}
+
+void World_addEntity(World *w, Entity *e) {
+ arrput(w->entities, *e);
+}
+
+void World_addSystem(World *w, System *s) {
+ arrput(w->systems, *s);
+}
+
+void World_runSystems(World *w) {
+ for (int i=0;i<arrlen(w->systems);++i) {
+ w->systems[i].run(w);
+ }
+}
diff --git a/core/world.h b/core/world.h
@@ -1,9 +1,16 @@
#pragma once
#include "map.h"
+#include "core/ecs/entity.h"
+#include "core/ecs/system.h"
typedef struct World {
Map map;
+ Entity *entities;
+ System *systems;
} World;
World * World_init();
+void World_addEntity(World *w, Entity *e);
+void World_addSystem(World *w, System *s);
+void World_runSystems(World *w);
diff --git a/main.c b/main.c
@@ -3,9 +3,16 @@
#include "core/world.h"
#include "commands/_registry.h"
+#include "systems/actions.h"
+
int main(int argc, char **argv) {
World w;
Map_init(&w.map, 3, 4);
+ puts("map init ok");
+
+ System actions = {0};
+ actions.run = actions_run;
+ World_addSystem(&w, &actions);
puts("world init ok");
if (argc>1) {
@@ -19,6 +26,10 @@ int main(int argc, char **argv) {
puts("unknown command");
return 1;
fin:
+
+ World_runSystems(&w);
+ puts("world systems run ok");
+
puts("ok");
return 0;
}
diff --git a/systems/actions.c b/systems/actions.c
@@ -0,0 +1,19 @@
+#include <stdbool.h>
+#include <stdio.h>
+#include "stb_ds.h"
+#include "systems/actions.h"
+#include "core/world.h"
+
+bool actions_run(World *w) {
+ printf("actions_run world %p\n", (void *)w);
+ for (int i=0;i<arrlen(w->entities);++i) {
+ for (int j=0;j<arrlen(w->entities[i].components);++j) {
+ if (w->entities[i].components[j].type==COMPONENT_ACTIONS) {
+ for (int k=0;k<arrlen(w->entities[i].components[j].actions.items);++k) {
+ w->entities[i].components[j].actions.items[k].run(&w->entities[i], w);
+ }
+ }
+ }
+ }
+ return true;
+}
diff --git a/systems/actions.h b/systems/actions.h
@@ -0,0 +1,5 @@
+#pragma once
+
+struct World; // fwd
+
+bool actions_run(struct World *w);