2019-12-21 03:32:21 +01:00
|
|
|
class_name GSTPriority
|
2020-01-16 23:14:50 +01:00
|
|
|
extends GSTSteeringBehavior
|
2020-01-02 23:42:41 +01:00
|
|
|
# Contains multiple steering behaviors and returns only the result of the first that has a non-zero
|
|
|
|
# acceleration.
|
2019-12-21 03:32:21 +01:00
|
|
|
|
|
|
|
|
2020-01-16 09:44:44 +01:00
|
|
|
var _behaviors := []
|
2019-12-21 03:32:21 +01:00
|
|
|
|
|
|
|
var last_selected_index: int
|
|
|
|
var threshold_for_zero: float
|
|
|
|
|
|
|
|
|
2020-01-16 09:44:44 +01:00
|
|
|
func _init(agent: GSTSteeringAgent, threshold_for_zero := 0.001).(agent) -> void:
|
2019-12-21 03:32:21 +01:00
|
|
|
self.threshold_for_zero = threshold_for_zero
|
|
|
|
|
|
|
|
|
|
|
|
func add(behavior: GSTSteeringBehavior) -> void:
|
|
|
|
_behaviors.append(behavior)
|
|
|
|
|
|
|
|
|
|
|
|
func get_behavior_at(index: int) -> GSTSteeringBehavior:
|
|
|
|
if _behaviors.size() > index:
|
|
|
|
return _behaviors[index]
|
|
|
|
printerr("Tried to get index " + str(index) + " in array of size " + str(_behaviors.size()))
|
|
|
|
return null
|
|
|
|
|
|
|
|
|
|
|
|
func _calculate_steering(accel: GSTTargetAcceleration) -> GSTTargetAcceleration:
|
2020-01-16 09:44:44 +01:00
|
|
|
var threshold_squared := threshold_for_zero * threshold_for_zero
|
2019-12-21 03:32:21 +01:00
|
|
|
|
|
|
|
last_selected_index = -1
|
|
|
|
|
2020-01-16 09:44:44 +01:00
|
|
|
var size := _behaviors.size()
|
2019-12-21 03:32:21 +01:00
|
|
|
|
|
|
|
if size > 0:
|
|
|
|
for i in range(size):
|
|
|
|
last_selected_index = i
|
|
|
|
var behavior: GSTSteeringBehavior = _behaviors[i]
|
|
|
|
behavior.calculate_steering(accel)
|
|
|
|
|
2020-01-10 20:38:12 +01:00
|
|
|
if accel.get_magnitude_squared() > threshold_squared:
|
2019-12-21 03:32:21 +01:00
|
|
|
break
|
|
|
|
else:
|
|
|
|
accel.set_zero()
|
|
|
|
|
|
|
|
return accel
|