diff --git a/scripts/Contents/AIPresets/Entity.gd b/scripts/Contents/AIPresets/Entity.gd index 4733ca7..d459c6a 100644 --- a/scripts/Contents/AIPresets/Entity.gd +++ b/scripts/Contents/AIPresets/Entity.gd @@ -8,3 +8,14 @@ static func follow(entity: EntityBase, target: EntityBase, minDistance: float = entity.move(-delta) else: entity.move(delta) +static func distanceAttack(entity: EntityBase, target: EntityBase, minDistance: float, maxDistance: float, index: int = 0): + var distance = (entity.position - target.position).length() + if minDistance <= distance and distance <= maxDistance: + entity.tryAttack(index) +static func distanceAction(entity: EntityBase, target: EntityBase, minDistance: float, maxDistance: float, action: Callable): + var distance = (entity.position - target.position).length() + if minDistance <= distance and distance <= maxDistance: + action.call() +static func weightAttack(entity: EntityBase, indexes: Array[int], weight: Array[int], chargeUp: Callable): + var method = MathTool.randc_from_weights(indexes, weight) + entity.tryAttack(method, chargeUp.call(method)) diff --git a/scripts/Contents/Characters/Chick.gd b/scripts/Contents/Characters/Chick.gd index d25036d..02082e4 100644 --- a/scripts/Contents/Characters/Chick.gd +++ b/scripts/Contents/Characters/Chick.gd @@ -18,13 +18,12 @@ func spawn(): func ai(): PresetEntityAI.follow(self, currentFocusedBoss, 0) - if currentFocusedBoss.position.distance_to(position) < 200: - tryAttack(2) - elif currentFocusedBoss.position.distance_to(position) < 700: - tryAttack(1) - else: - var method = MathTool.randc_from([0, 0, 0, 0, 3]) - tryAttack(method, method == 3) + PresetEntityAI.distanceAttack(self, currentFocusedBoss, 0, 200, 2) + PresetEntityAI.distanceAttack(self, currentFocusedBoss, 200, 700, 1) + PresetEntityAI.distanceAction(self, currentFocusedBoss, 700, INF, + func(): + PresetEntityAI.weightAttack(self, [0, 3], [5, 1], func(index): return index == 3) + ) func attack(type): if type == 0: var weaponPos = findWeaponAnchor("normal") diff --git a/scripts/Tools/MathTool.gd b/scripts/Tools/MathTool.gd index ecdc5ab..490271e 100644 --- a/scripts/Tools/MathTool.gd +++ b/scripts/Tools/MathTool.gd @@ -9,6 +9,12 @@ static func randv2_range(offset: float): ) static func randc_from(array: Array): return array[randi() % array.size()] +static func randc_from_weights(indexes: Array, weights: Array[int]): + var totals: Array = [] + for i in indexes: + for j in range(weights[i]): + totals.append(i) + return randc_from(totals) static func signBeforeStr(value: float): return ("+" if value > 0 else "-" if value < 0 else "") + str(abs(value)) static func percent(value: float):