Advertisement
Tkap1

Untitled

Oct 2nd, 2022
1,198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.41 KB | None | 0 0
  1.  
  2. func int get_closest_non_hit_enemy(
  3.     s_enemies* enemies,
  4.     s_cells cells,
  5.     s_v2 pos,
  6.     s_enemy_ref* already_hit,
  7.     int already_hit_count,
  8.     float range,
  9.     s_lin_arena* frame_arena
  10. )
  11. {
  12.  
  13.     m_timed_function();
  14.  
  15.     assert(already_hit);
  16.     assert(already_hit_count >= 0);
  17.  
  18.     int result = invalid_entity;
  19.  
  20.     la_push(frame_arena);
  21.     s_darray<int> indices = query_cells_circle(cells, enemies, pos, range, frame_arena);
  22.     float smallest_distance = (float)c_max_u32;
  23.  
  24.     for(int i = 0; i < indices.count; i++)
  25.     {
  26.         int entity = *arr_get(&indices, i);
  27.         assert(enemies->active[entity]);
  28.         if(!enemies->flags[entity][e_entity_flag_hittable]) { continue; }
  29.  
  30.         b8 has_been_hit = false;
  31.         for(int j = 0; j < already_hit_count; j++)
  32.         {
  33.             int already_hit_entity = already_hit[j].to_entity(enemies);
  34.             if(entity == already_hit_entity)
  35.             {
  36.                 has_been_hit = true;
  37.                 break;
  38.             }
  39.  
  40.         }
  41.         if(!has_been_hit)
  42.         {
  43.             float distance = v2_distance_squared(enemies->pos[entity].xy, pos);
  44.             if(distance < smallest_distance)
  45.             {
  46.                 smallest_distance = distance;
  47.                 result = entity;
  48.  
  49.                 // @Note(tkap, 26/06/2022): Breaking here to avoid potentially hundreds of unnecessary iterations. This is close enough that it wont be noticed.
  50.                 if(distance < 0.1f) { break; }
  51.             }
  52.         }
  53.     }
  54.     la_pop(frame_arena);
  55.  
  56. #ifdef DEBUG
  57.     if(result != invalid_entity)
  58.     {
  59.         assert(enemies->active[result]);
  60.     }
  61. #endif
  62.  
  63.     return result;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement