godot-steering-ai-framework/godot/addons/com.gdquest.godot-steering-ai-framework/Behaviors/GSAIBlend.gd

55 lines
1.5 KiB
GDScript3
Raw Normal View History

2020-01-29 17:04:04 +01:00
# Blends multiple steering behaviors into one, and returns a weighted
# acceleration from their calculations.
#
# Stores the behaviors internally as dictionaries of the form
# {
# behavior : GSAISteeringBehavior,
2020-01-29 17:04:04 +01:00
# weight : float
# }
2020-04-03 02:31:59 +02:00
# @category - Combination behaviors
class_name GSAIBlend
extends GSAISteeringBehavior
2019-12-21 03:32:21 +01:00
var _behaviors := []
var _accel := GSAITargetAcceleration.new()
2019-12-21 03:32:21 +01:00
func _init(agent: GSAISteeringAgent).(agent) -> void:
2019-12-21 03:32:21 +01:00
pass
2020-01-29 17:04:04 +01:00
# Appends a behavior to the internal array along with its `weight`.
func add(behavior: GSAISteeringBehavior, weight: float) -> void:
2019-12-21 03:32:21 +01:00
behavior.agent = agent
2023-01-13 11:02:01 +01:00
var dict : Dictionary = Dictionary()
dict["behavior"] = behavior;
dict["weight"] = weight;
_behaviors.append(dict)
2019-12-21 03:32:21 +01:00
2020-01-29 17:04:04 +01:00
# Returns the behavior at the specified `index`, or an empty `Dictionary` if
# none was found.
func get_behavior_at(index: int) -> Dictionary:
2019-12-21 03:32:21 +01:00
if _behaviors.size() > index:
return _behaviors[index]
printerr("Tried to get index " + str(index) + " in array of size " + str(_behaviors.size()))
return {}
2019-12-21 03:32:21 +01:00
func _calculate_steering(blended_accel: GSAITargetAcceleration) -> void:
2019-12-21 03:32:21 +01:00
blended_accel.set_zero()
2020-01-29 17:04:04 +01:00
2019-12-21 03:32:21 +01:00
for i in range(_behaviors.size()):
var bw: Dictionary = _behaviors[i]
2019-12-21 03:32:21 +01:00
bw.behavior.calculate_steering(_accel)
2020-01-29 17:04:04 +01:00
2019-12-21 03:32:21 +01:00
blended_accel.add_scaled_accel(_accel, bw.weight)
2020-01-29 17:04:04 +01:00
blended_accel.linear = GSAIUtils.clampedv3(blended_accel.linear, agent.linear_acceleration_max)
blended_accel.angular = clamp(
blended_accel.angular, -agent.angular_acceleration_max, agent.angular_acceleration_max
)