2020-01-29 17:04:04 +01:00
|
|
|
# Container for multiple behaviors that returns the result of the first child
|
|
|
|
# behavior with non-zero acceleration.
|
2020-04-03 02:31:59 +02:00
|
|
|
# @category - Combination behaviors
|
2020-02-11 18:33:25 +01:00
|
|
|
class_name GSAIPriority
|
|
|
|
extends GSAISteeringBehavior
|
2019-12-21 03:32:21 +01:00
|
|
|
|
2023-01-13 13:09:18 +01:00
|
|
|
var _behaviors : Array = Array()
|
2019-12-21 03:32:21 +01:00
|
|
|
|
2020-01-29 17:04:04 +01:00
|
|
|
# The index of the last behavior the container prioritized.
|
2023-01-13 13:09:18 +01:00
|
|
|
var last_selected_index : int = 0
|
2020-01-29 17:04:04 +01:00
|
|
|
# If a behavior's acceleration is lower than this threshold, the container
|
|
|
|
# considers it has an acceleration of zero.
|
2023-01-13 13:09:18 +01:00
|
|
|
var zero_threshold : float = 0.0
|
2019-12-21 03:32:21 +01:00
|
|
|
|
|
|
|
|
2023-01-13 13:09:18 +01:00
|
|
|
func _init(agent: GSAISteeringAgent, _zero_threshold : float = 0.001).(agent) -> void:
|
2020-02-11 20:36:06 +01:00
|
|
|
self.zero_threshold = _zero_threshold
|
2019-12-21 03:32:21 +01:00
|
|
|
|
|
|
|
|
2020-01-29 17:04:04 +01:00
|
|
|
# Appends a steering behavior as a child of this container.
|
2020-02-11 18:33:25 +01:00
|
|
|
func add(behavior: GSAISteeringBehavior) -> void:
|
2019-12-21 03:32:21 +01:00
|
|
|
_behaviors.append(behavior)
|
|
|
|
|
|
|
|
|
2020-01-29 17:04:04 +01:00
|
|
|
# Returns the behavior at the position in the pool referred to by `index`, or
|
|
|
|
# `null` if no behavior was found.
|
2023-01-13 13:09:18 +01:00
|
|
|
func get_behavior_at(index : int) -> GSAISteeringBehavior:
|
2019-12-21 03:32:21 +01:00
|
|
|
if _behaviors.size() > index:
|
|
|
|
return _behaviors[index]
|
2023-01-13 13:09:18 +01:00
|
|
|
|
2019-12-21 03:32:21 +01:00
|
|
|
printerr("Tried to get index " + str(index) + " in array of size " + str(_behaviors.size()))
|
2023-01-13 13:09:18 +01:00
|
|
|
|
2019-12-21 03:32:21 +01:00
|
|
|
return null
|
|
|
|
|
|
|
|
|
2023-01-13 13:09:18 +01:00
|
|
|
func _calculate_steering(accel : GSAITargetAcceleration) -> void:
|
|
|
|
var threshold_squared : float = zero_threshold * zero_threshold
|
2020-01-29 17:04:04 +01:00
|
|
|
|
2019-12-21 03:32:21 +01:00
|
|
|
last_selected_index = -1
|
2020-01-29 17:04:04 +01:00
|
|
|
|
2023-01-13 13:09:18 +01:00
|
|
|
var size : int = _behaviors.size()
|
2020-01-29 17:04:04 +01:00
|
|
|
|
2019-12-21 03:32:21 +01:00
|
|
|
if size > 0:
|
|
|
|
for i in range(size):
|
|
|
|
last_selected_index = i
|
2020-02-11 18:33:25 +01:00
|
|
|
var behavior: GSAISteeringBehavior = _behaviors[i]
|
2019-12-21 03:32:21 +01:00
|
|
|
behavior.calculate_steering(accel)
|
2020-01-29 17:04:04 +01:00
|
|
|
|
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()
|