Skip to content

Commit

Permalink
ranged fix
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelcastrocouto committed Sep 8, 2022
1 parent 1418aab commit e50e56e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 22 deletions.
16 changes: 8 additions & 8 deletions prototype/unit/behavior/advance.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
19 changes: 8 additions & 11 deletions prototype/unit/behavior/attack.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand Down Expand Up @@ -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 = {}):
Expand Down
11 changes: 8 additions & 3 deletions prototype/unit/behavior/orders.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit e50e56e

Please sign in to comment.