2020-01-29 05:56:10 +01:00
|
|
|
# Contains multiple behaviors and returns only the result of the first with non-zero acceleration.
|
2019-12-21 03:32:21 +01:00
|
|
|
class_name GSTPriority
|
2020-01-16 23:14:50 +01:00
|
|
|
extends GSTSteeringBehavior
|
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
|
|
|
|
2020-01-27 19:24:05 +01:00
|
|
|
# The index in the behavior array of the last behavior that was selected.
|
2019-12-21 03:32:21 +01:00
|
|
|
var last_selected_index: int
|
2020-01-27 19:24:05 +01:00
|
|
|
# The amount of acceleration for a behavior to be considered to have effectively zero acceleration
|
2020-01-22 17:55:49 +01:00
|
|
|
var zero_threshold: float
|
2019-12-21 03:32:21 +01:00
|
|
|
|
|
|
|
|
2020-01-22 17:55:49 +01:00
|
|
|
func _init(agent: GSTSteeringAgent, zero_threshold := 0.001).(agent) -> void:
|
|
|
|
self.zero_threshold = zero_threshold
|
2019-12-21 03:32:21 +01:00
|
|
|
|
|
|
|
|
2020-01-27 19:24:05 +01:00
|
|
|
# Add a steering `behavior` to the pool of behaviors to consider
|
2019-12-21 03:32:21 +01:00
|
|
|
func add(behavior: GSTSteeringBehavior) -> void:
|
|
|
|
_behaviors.append(behavior)
|
|
|
|
|
|
|
|
|
2020-01-27 19:24:05 +01:00
|
|
|
# Returns the behavior at the position in the pool referred to by `index`.
|
|
|
|
# Returns `null` if none were found.
|
2019-12-21 03:32:21 +01:00
|
|
|
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-22 17:55:49 +01:00
|
|
|
var threshold_squared := zero_threshold * zero_threshold
|
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
|