commit 3ebd6b7e876f34e2eaabbba687e91d73f823f641
parent 6ac4021fa425b7e10320dd1a80f53c78205091fa
Author: bsandro <email@bsandro.tech>
Date: Tue, 8 Sep 2026 00:10:53 +0300
basic death system
Diffstat:
12 files changed, 75 insertions(+), 27 deletions(-)
diff --git a/actions/attack.c b/actions/attack.c
@@ -1,10 +0,0 @@
-#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
@@ -1,6 +0,0 @@
-#pragma once
-
-struct Entity; // fwd
-struct World; // fwd
-
-bool Action_Attack(struct Entity *e, struct World *w);
diff --git a/actions/attack_melee.c b/actions/attack_melee.c
@@ -0,0 +1,11 @@
+#include <stdbool.h>
+#include <stdio.h>
+#include "actions/attack_melee.h"
+#include "core/ecs/entity.h"
+#include "core/world.h"
+
+bool Attack_Melee(Entity *e, World *w) {
+ int strength = Entity_Find_Component(e, COMPONENT_MELEE)->melee.strength; //@todo NULL check
+ printf("action melee attack with strength %d\n", strength);
+ return true;
+}
diff --git a/actions/attack_melee.h b/actions/attack_melee.h
@@ -0,0 +1,6 @@
+#pragma once
+
+struct Entity; // fwd
+struct World; // fwd
+
+bool Attack_Melee(struct Entity *e, struct World *w);
diff --git a/commands/SPAWN_SWORDSMAN.c b/commands/SPAWN_SWORDSMAN.c
@@ -1,11 +1,13 @@
#include "stb_ds.h"
#include "core/command.h"
#include "core/ecs/entity.h"
-#include "actions/attack.h"
+#include "actions/attack_melee.h"
void cmd_spawn_swordsman(World *w, ...) {
+ //@todo parse va_args
puts("spawn swordsman");
Entity swordsman = {0};
+ swordsman.id = 1;
Entity_Add_Component(&swordsman, (Component){
.type = COMPONENT_HEALTH,
.health = { .hp=30 }
@@ -14,8 +16,16 @@ void cmd_spawn_swordsman(World *w, ...) {
.type = COMPONENT_MELEE,
.melee = { .strength=30 }
});
+ Entity_Add_Component(&swordsman, (Component){
+ .type = COMPONENT_POSITION,
+ .position = { .x=1, .y=1 }
+ });
+ Entity_Add_Component(&swordsman, (Component){
+ .type = COMPONENT_MOVEMENT_TARGET,
+ .movement_target = { .x=5, .y=5 }
+ });
Actions actions = {0};
- arrput(actions.items, (struct Actions_Item){ .run = Action_Attack });
+ arrput(actions.items, (struct Actions_Item){ .run = Attack_Melee });
Entity_Add_Component(&swordsman, (Component){
.type = COMPONENT_ACTIONS,
.actions = actions
diff --git a/components/movement_target.h b/components/movement_target.h
@@ -0,0 +1,6 @@
+#pragma once
+
+typedef struct MovementTarget {
+ int x;
+ int y;
+} MovementTarget;
diff --git a/components/position.h b/components/position.h
@@ -0,0 +1,6 @@
+#pragma once
+
+typedef struct Position {
+ int x;
+ int y;
+} Position;
diff --git a/core/world.c b/core/world.c
@@ -15,9 +15,9 @@ void World_addSystem(World *w, System *s) {
}
bool World_runSystems(World *w) {
- bool ret = true;
+ bool ret = false;
for (int i=0;i<arrlen(w->systems);++i) {
- ret &= w->systems[i].run(w);
+ ret |= w->systems[i].run(w);
}
return ret;
}
diff --git a/main.c b/main.c
@@ -4,15 +4,15 @@
#include "commands/_registry.h"
#include "systems/actions.h"
+#include "systems/death.h"
int main(int argc, char **argv) {
World w;
Map_init(&w.map, 3, 4);
puts("map init ok");
int turn = 0;
- System actions = {0};
- actions.run = actions_run;
- World_addSystem(&w, &actions);
+ World_addSystem(&w, &(System){ .run = actions_run });
+ World_addSystem(&w, &(System){ .run = death_run });
puts("world init ok");
if (argc>1) {
diff --git a/systems/actions.c b/systems/actions.c
@@ -9,10 +9,10 @@ bool actions_run(World *w) {
printf("actions_run world %p\n", (void *)w);
bool ret = false;
for (int i=0;i<arrlen(w->entities);++i) {
- Component *comp = Entity_Find_Component(&w->entities[i], COMPONENT_ACTIONS);
- if (comp==NULL) continue;
- for (int j=0;j<arrlen(comp->actions.items);++j) {
- if (comp->actions.items[j].run(&w->entities[i], w)) {
+ Component *c = Entity_Find_Component(&w->entities[i], COMPONENT_ACTIONS);
+ if (c==NULL) continue;
+ for (int j=0;j<arrlen(c->actions.items);++j) {
+ if (c->actions.items[j].run(&w->entities[i], w)) {
ret = true;
break;
}
diff --git a/systems/death.c b/systems/death.c
@@ -0,0 +1,20 @@
+#include <stdbool.h>
+#include <stdio.h>
+#include "stb_ds.h"
+#include "core/ecs/entity.h"
+#include "systems/actions.h"
+#include "core/world.h"
+
+bool death_run(World *w) {
+ printf("death_run world %p\n", (void *)w);
+ for (int i=0;i<arrlen(w->entities);++i) {
+ Component *c = Entity_Find_Component(&w->entities[i], COMPONENT_HEALTH);
+ if (c==NULL) continue;
+ if (c->health.hp<=0) {
+ printf("entity %d died\n", w->entities[i].id);
+ arrdel(w->entities, i);
+ i--; // wtf
+ }
+ }
+ return true;
+}
diff --git a/systems/death.h b/systems/death.h
@@ -0,0 +1,5 @@
+#pragma once
+
+struct World; // fwd
+
+bool death_run(struct World *w);