2019-12-21 03:32:21 +01:00
|
|
|
class_name GSTBlend
|
2020-01-16 23:14:50 +01:00
|
|
|
extends GSTSteeringBehavior
|
2020-01-02 23:42:41 +01:00
|
|
|
# Blends multiple steering behaviors into one, and returns acceleration combining all of them.
|
2019-12-21 03:32:21 +01:00
|
|
|
|
2020-01-02 23:42:41 +01:00
|
|
|
# # Each behavior is associated with a weight - a modifier by which the result will be multiplied by,
|
|
|
|
# then added to a total acceleration.
|
2019-12-23 17:38:27 +01:00
|
|
|
|
2020-01-02 23:42:41 +01:00
|
|
|
# # Each behavior is stored internally as a `Dictionary` with a `behavior` key with a value of type
|
|
|
|
# `GSTSteeringBehavior` and a `weight` key with a value of type float.
|
2019-12-21 03:32:21 +01:00
|
|
|
|
|
|
|
|
2020-01-16 09:44:44 +01:00
|
|
|
var _behaviors := []
|
|
|
|
var _accel := GSTTargetAcceleration.new()
|
2019-12-21 03:32:21 +01:00
|
|
|
|
|
|
|
|
|
|
|
func _init(agent: GSTSteeringAgent).(agent) -> void:
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
func add(behavior: GSTSteeringBehavior, weight: float) -> void:
|
|
|
|
behavior.agent = agent
|
2019-12-23 17:38:27 +01:00
|
|
|
_behaviors.append({behavior = behavior, weight = weight})
|
2019-12-21 03:32:21 +01:00
|
|
|
|
|
|
|
|
2019-12-23 17:38:27 +01:00
|
|
|
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()))
|
2019-12-23 17:38:27 +01:00
|
|
|
return {}
|
2019-12-21 03:32:21 +01:00
|
|
|
|
|
|
|
|
|
|
|
func _calculate_steering(blended_accel: GSTTargetAcceleration) -> GSTTargetAcceleration:
|
|
|
|
blended_accel.set_zero()
|
|
|
|
|
|
|
|
for i in range(_behaviors.size()):
|
2019-12-23 17:38:27 +01:00
|
|
|
var bw: Dictionary = _behaviors[i]
|
2019-12-21 03:32:21 +01:00
|
|
|
bw.behavior.calculate_steering(_accel)
|
|
|
|
|
|
|
|
blended_accel.add_scaled_accel(_accel, bw.weight)
|
|
|
|
|
2020-01-10 18:15:50 +01:00
|
|
|
blended_accel.linear = GSTUtils.clampedv3(blended_accel.linear, agent.max_linear_acceleration)
|
2019-12-23 17:38:27 +01:00
|
|
|
blended_accel.angular = min(blended_accel.angular, agent.max_angular_acceleration)
|
2019-12-21 03:32:21 +01:00
|
|
|
|
|
|
|
return blended_accel
|