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 afb767d3a8c9bbdfe1d53cdda9d41591d444fd34
parent 917e798edec11551ab00d4db014cd7bd70bd6c68
Author: bsandro <email@bsandro.tech>
Date:   Tue,  8 Sep 2026 00:48:57 +0300

basic melee attack

Diffstat:
Mactions/attack_melee.c | 18+++++++++++++++---
Mcommands/SPAWN_SWORDSMAN.c | 38++++++++++++++++++++++++++++++++++++--
Msystems/actions.c | 1+
Msystems/death.c | 2+-
4 files changed, 53 insertions(+), 6 deletions(-)

diff --git a/actions/attack_melee.c b/actions/attack_melee.c @@ -1,12 +1,24 @@ +#include <stdlib.h> #include <stdbool.h> #include <stdio.h> +#include "stb_ds.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; + int hp = Entity_Find_Component(e, COMPONENT_HEALTH)->health.hp; //@todo NULL check + Position p = Entity_Find_Component(e, COMPONENT_POSITION)->position; //@todo NULL check + printf("[%d with %d hp] action try melee attack with strength %d\n", e->id, hp, strength); + for (int i=0;i<arrlen(w->entities);++i) { + if (w->entities[i].id==e->id) continue; + Position pe = Entity_Find_Component(&w->entities[i], COMPONENT_POSITION)->position; + if (abs(pe.x-p.x)<=1 && abs(pe.y-p.y)<=1) { + printf("found neighbor enemy %d\n", w->entities[i].id); + Entity_Find_Component(&w->entities[i], COMPONENT_HEALTH)->health.hp -= strength; + return true; + } + } + return false; } diff --git a/commands/SPAWN_SWORDSMAN.c b/commands/SPAWN_SWORDSMAN.c @@ -3,6 +3,38 @@ #include "core/ecs/entity.h" #include "actions/attack_melee.h" +void spawn_swordsman2(World *w) { + //@todo parse va_args + puts("spawn swordsman 2"); + Entity swordsman = {0}; + swordsman.id = 2; + Entity_Add_Component(&swordsman, (Component){ + .type = COMPONENT_HEALTH, + .health = { .hp=30 } + }); + Entity_Add_Component(&swordsman, (Component){ + .type = COMPONENT_MELEE, + .melee = { .strength=1 } + }); + 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 = Attack_Melee }); + Entity_Add_Component(&swordsman, (Component){ + .type = COMPONENT_ACTIONS, + .actions = actions + }); + World_Add_Entity(w, &swordsman); + printf("entity has %ld components:\n", arrlen(swordsman.components)); + printf("%d: %dhp\n", swordsman.components[0].type, swordsman.components[0].health.hp); +} + void cmd_spawn_swordsman(World *w, ...) { //@todo parse va_args puts("spawn swordsman"); @@ -14,11 +46,11 @@ void cmd_spawn_swordsman(World *w, ...) { }); Entity_Add_Component(&swordsman, (Component){ .type = COMPONENT_MELEE, - .melee = { .strength=30 } + .melee = { .strength=2 } }); Entity_Add_Component(&swordsman, (Component){ .type = COMPONENT_POSITION, - .position = { .x=1, .y=1 } + .position = { .x=2, .y=2 } }); Entity_Add_Component(&swordsman, (Component){ .type = COMPONENT_MOVEMENT_TARGET, @@ -33,4 +65,6 @@ void cmd_spawn_swordsman(World *w, ...) { World_Add_Entity(w, &swordsman); printf("entity has %ld components:\n", arrlen(swordsman.components)); printf("%d: %dhp\n", swordsman.components[0].type, swordsman.components[0].health.hp); + + spawn_swordsman2(w); } diff --git a/systems/actions.c b/systems/actions.c @@ -13,6 +13,7 @@ bool actions_run(World *w) { if (c==NULL) continue; for (int j=0;j<arrlen(c->actions.items);++j) { if (c->actions.items[j].run(&w->entities[i], w)) { + puts("action ok"); ret = true; break; } diff --git a/systems/death.c b/systems/death.c @@ -16,5 +16,5 @@ bool death_run(World *w) { i--; // wtf } } - return true; + return false; }