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

attack_melee.c (1211B)


      1 #include <stdlib.h>
      2 #include <stdbool.h>
      3 #include <stdio.h>
      4 #include "stb_ds.h"
      5 #include "actions/attack_melee.h"
      6 #include "core/ecs/entity.h"
      7 #include "core/world.h"
      8 
      9 bool Action_Attack_Melee(Entity *e, World *w) {
     10     //printf("unit %d try melee attack\n", e->id);
     11     int strength = Entity_Find_Component(e, COMPONENT_MELEE)->melee.strength; //@todo NULL check
     12     //int hp = Entity_Find_Component(e, COMPONENT_HEALTH)->health.hp; //@todo NULL check
     13     Position p = Entity_Find_Component(e, COMPONENT_POSITION)->position; //@todo NULL check
     14     for (int i=0;i<arrlen(w->entities);++i) {
     15         if (w->entities[i].id==e->id) continue;
     16         Position pe = Entity_Find_Component(&w->entities[i], COMPONENT_POSITION)->position;
     17         if (abs(pe.x-p.x)<=1 && abs(pe.y-p.y)<=1) {
     18             //printf("found neighbor enemy %d\n", w->entities[i].id);
     19             Component *target = Entity_Find_Component(&w->entities[i], COMPONENT_HEALTH);
     20             target->health.hp -= strength;
     21             printf("[%d] UNIT_ATTACKED attackerUnitId=%d targetUnitId=%d damage=%d targetHp=%d\n", w->turn, e->id, w->entities[i].id, strength, target->health.hp);
     22             return true;
     23         }
     24     }
     25     return false;
     26 }