sw-battle-test-c

Interesting test task in C instead of C++
git clone git://bsandro.tech/sw-battle-test-c
Log | Files | Refs | README

commit 5f57321a32038b330ad5f44d291ad88df7b14300
parent 2da61694a72f77885c8d1518362e674694d186f1
Author: bsandro <email@bsandro.tech>
Date:   Thu,  3 Sep 2026 01:26:35 +0300

sample populating couple of components into entity on SPAWN_SWORDSMAN command

Diffstat:
Mcommands/SPAWN_SWORDSMAN.c | 14++++++++++++++
Mcore/ecs/entity.c | 6+++---
Mcore/ecs/entity.h | 2+-
Mmain.c | 7+++++--
4 files changed, 23 insertions(+), 6 deletions(-)

diff --git a/commands/SPAWN_SWORDSMAN.c b/commands/SPAWN_SWORDSMAN.c @@ -1,5 +1,19 @@ #include "core/command.h" +#include "core/ecs/entity.h" + +#include "stb_ds.h" void cmd_spawn_swordsman(World *w, ...) { puts("spawn swordsman"); + Entity entity = {0}; + Entity_Add_Component(&entity, (Component){ + .type = COMPONENT_HEALTH, + .health = { .hp=30 } + }); + Entity_Add_Component(&entity, (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); } diff --git a/core/ecs/entity.c b/core/ecs/entity.c @@ -13,9 +13,9 @@ Entity * Entity_New(int id) { return e; } -bool Entity_Add_Component(Entity *e, Component *c) { - printf("%d add component %d\n", e->id, c->type); - arrput(e->components, *c); +bool Entity_Add_Component(Entity *e, Component c) { + printf("%d add component %d\n", e->id, c.type); + arrput(e->components, c); return true; } diff --git a/core/ecs/entity.h b/core/ecs/entity.h @@ -10,4 +10,4 @@ typedef struct Entity { } Entity; Entity * Entity_New(int id); -bool Entity_Add_Component(Entity *e, Component *c); +bool Entity_Add_Component(Entity *e, Component c); diff --git a/main.c b/main.c @@ -12,10 +12,13 @@ int main(int argc, char **argv) { for (int i=0;i<COMMANDS_COUNT;++i) { if (strcmp(COMMANDS[i].name, argv[1])==0) { COMMANDS[i].func(&w, "ehlo"); - break; + goto fin; } } - puts("unknown command"); } + puts("unknown command"); + return 1; +fin: + puts("ok"); return 0; }