diff --git a/prototype/unit/behavior/advance.gd b/prototype/unit/behavior/advance.gd index 27281f1b..c219e830 100644 --- a/prototype/unit/behavior/advance.gd +++ b/prototype/unit/behavior/advance.gd @@ -19,15 +19,15 @@ func start(unit, objective, smart_move = false): # move_and_attack var enemies = unit.get_units_on_sight({"team": unit.oponent_team()}) var at_objective = (unit.global_position.distance_to(unit.objective) < game.map.half_tile_size) var has_path = (unit.current_path.size() > 0) - if not enemies and not at_objective: move(unit, unit.objective, smart_move) - if not enemies and at_objective: - if has_path: game.unit.follow.start(unit, unit.current_path, "advance") - if enemies: + if not enemies: + if not at_objective: move(unit, unit.objective, smart_move) + elif has_path: game.unit.follow.start(unit, unit.current_path, "advance") + else: var target = game.unit.orders.select_target(unit, enemies) - if not target and not at_objective: move(unit, unit.objective, smart_move) - if not target and at_objective: - if has_path: game.unit.follow.start(unit, unit.current_path, "advance") - if target: + if not target: + if not at_objective: move(unit, unit.objective, smart_move) + elif has_path: game.unit.follow.start(unit, unit.current_path, "advance") + else: game.unit.attack.set_target(unit, target) var target_position = target.global_position + target.collision_position if game.unit.attack.in_range(unit, target): diff --git a/prototype/unit/behavior/attack.gd b/prototype/unit/behavior/attack.gd index 5e698df4..bbb832b2 100644 --- a/prototype/unit/behavior/attack.gd +++ b/prototype/unit/behavior/attack.gd @@ -16,10 +16,8 @@ func start(unit, point): var neighbors = game.map.blocks.get_units_in_radius(point, 1) if neighbors: var target = closest_enemy_unit(unit, neighbors) - if target: - var target_position = target.global_position + target.collision_position - if target_position.distance_to(point) < target.collision_radius: - game.unit.attack.set_target(unit, target) + if can_hit(unit, target) and in_range(unit, target): + game.unit.attack.set_target(unit, target) if unit.target or game.test.unit: unit.aim_point = point @@ -44,15 +42,14 @@ func set_target(unit, target): func closest_enemy_unit(unit, enemies): - var sorted = game.utils.sort_by_distance(unit, enemies) var filtered = [] - for enemy in sorted: - if (enemy.unit.team == game.enemy_team and - not enemy.unit.dead and - not enemy.unit.immune): filtered.append(enemy.unit) + for enemy in enemies: + if can_hit(unit, enemy): filtered.append(enemy) - if filtered: return filtered[0] + var sorted = game.utils.sort_by_distance(unit, filtered) + + if sorted: return sorted[0].unit func hit(unit1): @@ -92,7 +89,7 @@ func in_range(attacker, target): var att_rad = game.unit.modifiers.get_value(attacker, "attack_range") var tar_pos = target.global_position + target.collision_position var tar_rad = target.collision_radius - return game.utils.circle_collision(att_pos, att_rad, tar_pos, tar_rad * 0.9) + return game.utils.circle_collision(att_pos, att_rad, tar_pos, tar_rad) func take_hit(attacker, target, projectile = null, modifiers = {}): diff --git a/prototype/unit/behavior/orders.gd b/prototype/unit/behavior/orders.gd index c56206bc..4bada512 100644 --- a/prototype/unit/behavior/orders.gd +++ b/prototype/unit/behavior/orders.gd @@ -207,13 +207,18 @@ func set_leader_priority(priority): func select_target(unit, enemies): - var n = enemies.size() + var filtered = [] + + for enemy in enemies: + if game.unit.attack.can_hit(unit, enemy): filtered.append(enemy) + + var n = filtered.size() if n == 0: return if n == 1: - return enemies[0] + return filtered[0] - var sorted = game.utils.sort_by_distance(unit, enemies) + var sorted = game.utils.sort_by_distance(unit, filtered) var closest_unit = sorted[0].unit if n == 2: