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_HUNTER.c (1673B)


      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_hunter(World *w, int id, int x, int y, int hp, int agi, int str, int range) {
      9     //puts("spawn hunter");
     10     Entity hunter = {0};
     11     hunter.id = id;
     12     Entity_Add_Component(&hunter, (Component){
     13         .type = COMPONENT_HEALTH,
     14         .health = { .hp=hp }
     15     });
     16     Entity_Add_Component(&hunter, (Component){
     17         .type = COMPONENT_MELEE,
     18         .melee = { .strength=str }
     19     });
     20     Entity_Add_Component(&hunter, (Component){
     21         .type = COMPONENT_POSITION,
     22         .position = { .x=x, .y=y }
     23     });
     24     Entity_Add_Component(&hunter, (Component){
     25         .type = COMPONENT_RANGED,
     26         .ranged = { .agility=agi, .range=range }
     27     });
     28     Actions actions = {0};
     29     arrput(actions.items, (struct Actions_Item){ .run = Action_Attack_Melee });
     30     arrput(actions.items, (struct Actions_Item){ .run = Action_Attack_Ranged });
     31     Entity_Add_Component(&hunter, (Component){
     32         .type = COMPONENT_ACTIONS,
     33         .actions = actions
     34     });
     35     World_Add_Entity(w, &hunter);
     36     //printf("unit has %ld components:\n", arrlen(hunter.components));
     37     //printf("%d: %dhp\n", hunter.components[0].type, hunter.components[0].health.hp);
     38     printf("[%d] UNIT_SPAWNED unitId=%d unitType=Hunter x=%d y=%d\n", w->turn, id, x, y);
     39 }
     40 
     41 
     42 bool cmd_spawn_hunter(World *w, int argc, char **argv) {
     43     if (argc!=7) return false;
     44     spawn_hunter(w, atoi(argv[0]), atoi(argv[1]), atoi(argv[2]), atoi(argv[3]), atoi(argv[4]), atoi(argv[5]), atoi(argv[6]));
     45     return true;
     46 }