Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- func int get_closest_non_hit_enemy(
- s_enemies* enemies,
- s_cells cells,
- s_v2 pos,
- s_enemy_ref* already_hit,
- int already_hit_count,
- float range,
- s_lin_arena* frame_arena
- )
- {
- m_timed_function();
- assert(already_hit);
- assert(already_hit_count >= 0);
- int result = invalid_entity;
- la_push(frame_arena);
- s_darray<int> indices = query_cells_circle(cells, enemies, pos, range, frame_arena);
- float smallest_distance = (float)c_max_u32;
- for(int i = 0; i < indices.count; i++)
- {
- int entity = *arr_get(&indices, i);
- assert(enemies->active[entity]);
- if(!enemies->flags[entity][e_entity_flag_hittable]) { continue; }
- b8 has_been_hit = false;
- for(int j = 0; j < already_hit_count; j++)
- {
- int already_hit_entity = already_hit[j].to_entity(enemies);
- if(entity == already_hit_entity)
- {
- has_been_hit = true;
- break;
- }
- }
- if(!has_been_hit)
- {
- float distance = v2_distance_squared(enemies->pos[entity].xy, pos);
- if(distance < smallest_distance)
- {
- smallest_distance = distance;
- result = entity;
- // @Note(tkap, 26/06/2022): Breaking here to avoid potentially hundreds of unnecessary iterations. This is close enough that it wont be noticed.
- if(distance < 0.1f) { break; }
- }
- }
- }
- la_pop(frame_arena);
- #ifdef DEBUG
- if(result != invalid_entity)
- {
- assert(enemies->active[result]);
- }
- #endif
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement