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

SPAWN_SWORDSMAN.c (1448B)


      1 #include "stb_ds.h"
      2 #include "core/command.h"
      3 #include "core/ecs/entity.h"
      4 #include "actions/attack_melee.h"
      5 #include "actions/attack_ranged.h"
      6 #include "actions/movement.h"
      7 
      8 static void spawn_swordsman(World *w, int id, int x, int y, int hp, int str) {
      9     //puts("spawn swordsman");
     10     Entity swordsman = {0};
     11     swordsman.id = id;
     12     Entity_Add_Component(&swordsman, (Component){
     13         .type = COMPONENT_HEALTH,
     14         .health = { .hp=hp }
     15     });
     16     Entity_Add_Component(&swordsman, (Component){
     17         .type = COMPONENT_MELEE,
     18         .melee = { .strength=str }
     19     });
     20     Entity_Add_Component(&swordsman, (Component){
     21         .type = COMPONENT_POSITION,
     22         .position = { .x=x, .y=y }
     23     });
     24     Actions actions = {0};
     25     arrput(actions.items, (struct Actions_Item){ .run = Action_Attack_Melee });
     26     Entity_Add_Component(&swordsman, (Component){
     27         .type = COMPONENT_ACTIONS,
     28         .actions = actions
     29     });
     30     World_Add_Entity(w, &swordsman);
     31     //printf("unit has %ld components:\n", arrlen(swordsman.components));
     32     //printf("%d: %dhp\n", swordsman.components[0].type, swordsman.components[0].health.hp);
     33     printf("[%d] UNIT_SPAWNED unitId=%d unitType=Swordsman x=%d y=%d\n", w->turn, id, x, y);
     34 }
     35 
     36 bool cmd_spawn_swordsman(World *w, int argc, char **argv) {
     37     if (argc!=5) return false;
     38     spawn_swordsman(w, atoi(argv[0]), atoi(argv[1]), atoi(argv[2]), atoi(argv[3]), atoi(argv[4]));
     39     return true;
     40 }