More api cleanups.

This commit is contained in:
Relintai 2023-01-13 20:30:27 +01:00
parent 07f03b04f8
commit 7b9561875a
18 changed files with 70 additions and 38 deletions

View File

@ -60,5 +60,5 @@ func setup(
target_node = _target target_node = _target
self.target.position = target_node.transform.origin self.target.position = target_node.transform.origin
blend.add(arrive, 1) blend.add_behavior(arrive, 1)
blend.add(face, 1) blend.add_behavior(face, 1)

View File

@ -80,8 +80,8 @@ func setup(
self.draw_proximity = _draw_proximity self.draw_proximity = _draw_proximity
priority.add(avoid) priority.add_behavior(avoid)
priority.add(seek) priority.add_behavior(seek)
func set_proximity_agents(agents: Array) -> void: func set_proximity_agents(agents: Array) -> void:

View File

@ -50,8 +50,8 @@ func setup(
cohesion.agent = agent cohesion.agent = agent
cohesion.proximity = proximity cohesion.proximity = proximity
blend.add(separation, separation_strength) blend.add_behavior(separation, separation_strength)
blend.add(cohesion, cohesion_strength) blend.add_behavior(cohesion, cohesion_strength)
func _draw() -> void: func _draw() -> void:

View File

@ -84,9 +84,9 @@ func set_separation_decay_coef(value: float) -> void:
func set_cohesion_strength(value: float) -> void: func set_cohesion_strength(value: float) -> void:
for child in get_children(): for child in get_children():
child.blend.get_behavior_at(1).weight = value child.blend.get_behavior(1).weight = value
func set_separation_strength(value: float) -> void: func set_separation_strength(value: float) -> void:
for child in get_children(): for child in get_children():
child.blend.get_behavior_at(0).weight = value child.blend.get_behavior(0).weight = value

View File

@ -69,8 +69,8 @@ func setup(predict_time: float, linear_speed_max: float, linear_accel_max: float
_blend = GSAIBlend.new() _blend = GSAIBlend.new()
_blend.agent = agent _blend.agent = agent
_blend.add(behavior, 1) _blend.add_behavior(behavior, 1)
_blend.add(orient_behavior, 1) _blend.add_behavior(orient_behavior, 1)
agent.angular_acceleration_max = deg2rad(1080) agent.angular_acceleration_max = deg2rad(1080)
agent.angular_speed_max = deg2rad(360) agent.angular_speed_max = deg2rad(360)

View File

@ -118,19 +118,19 @@ func _ready() -> void:
# Behaviors that are not enabled produce 0 acceleration. # Behaviors that are not enabled produce 0 acceleration.
# Adding our fleeing behaviors to a blend. The order does not matter. # Adding our fleeing behaviors to a blend. The order does not matter.
flee_blend.is_enabled = false flee_blend.is_enabled = false
flee_blend.add(look, 1) flee_blend.add_behavior(look, 1)
flee_blend.add(flee, 1) flee_blend.add_behavior(flee, 1)
# Adding our pursuit behaviors to a blend. The order does not matter. # Adding our pursuit behaviors to a blend. The order does not matter.
pursue_blend.add(face, 1) pursue_blend.add_behavior(face, 1)
pursue_blend.add(pursue, 1) pursue_blend.add_behavior(pursue, 1)
# Adding our final behaviors to the main priority behavior. The order does matter here. # Adding our final behaviors to the main priority behavior. The order does matter here.
# We want to avoid collision with the player first, flee from the player second when enabled, # We want to avoid collision with the player first, flee from the player second when enabled,
# and pursue the player last when enabled. # and pursue the player last when enabled.
priority.add(avoid) priority.add_behavior(avoid)
priority.add(flee_blend) priority.add_behavior(flee_blend)
priority.add(pursue_blend) priority.add_behavior(pursue_blend)
func _physics_process(delta: float) -> void: func _physics_process(delta: float) -> void:

View File

@ -14,6 +14,8 @@ var deceleration_radius : float = 0.0
# Represents the time it takes to change acceleration. # Represents the time it takes to change acceleration.
var time_to_reach : float = 0.1 var time_to_reach : float = 0.1
func arrive(acceleration : GSAITargetAcceleration, target_position : Vector3) -> void:
call("_arrive", acceleration, target_position)
func _arrive(acceleration : GSAITargetAcceleration, target_position : Vector3) -> void: func _arrive(acceleration : GSAITargetAcceleration, target_position : Vector3) -> void:
var to_target : Vector3 = target_position - agent.position var to_target : Vector3 = target_position - agent.position
@ -34,6 +36,5 @@ func _arrive(acceleration : GSAITargetAcceleration, target_position : Vector3) -
acceleration.linear = GSAIUtils.clampedv3(desired_velocity, agent.linear_acceleration_max) acceleration.linear = GSAIUtils.clampedv3(desired_velocity, agent.linear_acceleration_max)
acceleration.angular = 0 acceleration.angular = 0
func _calculate_steering(acceleration: GSAITargetAcceleration) -> void: func _calculate_steering(acceleration: GSAITargetAcceleration) -> void:
_arrive(acceleration, target.position) arrive(acceleration, target.position)

View File

@ -11,7 +11,6 @@ var _first_distance : float = 0.0
var _first_relative_position : Vector3 var _first_relative_position : Vector3
var _first_relative_velocity : Vector3 var _first_relative_velocity : Vector3
func _calculate_steering(acceleration: GSAITargetAcceleration) -> void: func _calculate_steering(acceleration: GSAITargetAcceleration) -> void:
_shortest_time = INF _shortest_time = INF
_first_neighbor = null _first_neighbor = null

View File

@ -11,11 +11,10 @@ class_name GSAIBlend
extends GSAISteeringBehavior extends GSAISteeringBehavior
var _behaviors : Array = Array() var _behaviors : Array = Array()
#note only add a getter
var _accel : GSAITargetAcceleration = GSAITargetAcceleration.new() var _accel : GSAITargetAcceleration = GSAITargetAcceleration.new()
# Appends a behavior to the internal array along with its `weight`. # Appends a behavior to the internal array along with its `weight`.
func add(behavior : GSAISteeringBehavior, weight : float) -> void: func add_behavior(behavior : GSAISteeringBehavior, weight : float) -> void:
behavior.agent = agent behavior.agent = agent
var dict : Dictionary = Dictionary() var dict : Dictionary = Dictionary()
@ -25,10 +24,9 @@ func add(behavior : GSAISteeringBehavior, weight : float) -> void:
_behaviors.append(dict) _behaviors.append(dict)
# Returns the behavior at the specified `index`, or an empty `Dictionary` if # Returns the behavior at the specified `index`, or an empty `Dictionary` if
# none was found. # none was found.
func get_behavior_at(index : int) -> Dictionary: func get_behavior(index : int) -> Dictionary:
if _behaviors.size() > index: if _behaviors.size() > index:
return _behaviors[index] return _behaviors[index]
@ -36,6 +34,21 @@ func get_behavior_at(index : int) -> Dictionary:
return Dictionary() return Dictionary()
func remove_behavior(index : int) -> void:
if _behaviors.size() > index:
_behaviors.remove(index)
return
printerr("Tried to get index " + str(index) + " in array of size " + str(_behaviors.size()))
return
func get_behaviour_count() -> int:
return _behaviors.size()
func get_accel() -> GSAITargetAcceleration:
return _accel
func _calculate_steering(blended_accel: GSAITargetAcceleration) -> void: func _calculate_steering(blended_accel: GSAITargetAcceleration) -> void:
blended_accel.set_zero() blended_accel.set_zero()

View File

@ -4,6 +4,8 @@
class_name GSAIFace class_name GSAIFace
extends GSAIMatchOrientation extends GSAIMatchOrientation
func face(acceleration: GSAITargetAcceleration, target_position: Vector3) -> void:
call("_face", acceleration, target_position)
func _face(acceleration: GSAITargetAcceleration, target_position: Vector3) -> void: func _face(acceleration: GSAITargetAcceleration, target_position: Vector3) -> void:
var to_target : Vector3 = target_position - agent.position var to_target : Vector3 = target_position - agent.position
@ -19,8 +21,7 @@ func _face(acceleration: GSAITargetAcceleration, target_position: Vector3) -> vo
else: else:
orientation = GSAIUtils.vector2_to_angle(GSAIUtils.to_vector2(to_target)) orientation = GSAIUtils.vector2_to_angle(GSAIUtils.to_vector2(to_target))
_match_orientation(acceleration, orientation) match_orientation(acceleration, orientation)
func _calculate_steering(acceleration: GSAITargetAcceleration) -> void: func _calculate_steering(acceleration: GSAITargetAcceleration) -> void:
_face(acceleration, target.position) face(acceleration, target.position)

View File

@ -35,11 +35,11 @@ func _calculate_steering(acceleration: GSAITargetAcceleration) -> void:
if is_arrive_enabled and path.is_open: if is_arrive_enabled and path.is_open:
if path_offset >= 0: if path_offset >= 0:
if target_distance > path.length - deceleration_radius: if target_distance > path.length - deceleration_radius:
_arrive(acceleration, target_position) arrive(acceleration, target_position)
return return
else: else:
if target_distance < deceleration_radius: if target_distance < deceleration_radius:
_arrive(acceleration, target_position) arrive(acceleration, target_position)
return return
acceleration.linear = (target_position - agent.position).normalized() acceleration.linear = (target_position - agent.position).normalized()

View File

@ -16,4 +16,4 @@ func _calculate_steering(accel: GSAITargetAcceleration) -> void:
else: else:
orientation = GSAIUtils.vector2_to_angle(GSAIUtils.to_vector2(agent.linear_velocity)) orientation = GSAIUtils.vector2_to_angle(GSAIUtils.to_vector2(agent.linear_velocity))
_match_orientation(accel, orientation) match_orientation(accel, orientation)

View File

@ -18,6 +18,8 @@ var time_to_reach : float = 0.1
# determining angles. X and Z should be used in 3D. # determining angles. X and Z should be used in 3D.
var use_z : bool = false var use_z : bool = false
func match_orientation(acceleration: GSAITargetAcceleration, desired_orientation: float) -> void:
call("_match_orientation", acceleration, desired_orientation)
func _match_orientation(acceleration: GSAITargetAcceleration, desired_orientation: float) -> void: func _match_orientation(acceleration: GSAITargetAcceleration, desired_orientation: float) -> void:
var rotation : float = wrapf(desired_orientation - agent.orientation, -PI, PI) var rotation : float = wrapf(desired_orientation - agent.orientation, -PI, PI)
@ -44,4 +46,4 @@ func _match_orientation(acceleration: GSAITargetAcceleration, desired_orientatio
func _calculate_steering(acceleration: GSAITargetAcceleration) -> void: func _calculate_steering(acceleration: GSAITargetAcceleration) -> void:
_match_orientation(acceleration, target.orientation) match_orientation(acceleration, target.orientation)

View File

@ -14,13 +14,12 @@ var zero_threshold : float = 0.0
# Appends a steering behavior as a child of this container. # Appends a steering behavior as a child of this container.
func add(behavior: GSAISteeringBehavior) -> void: func add_behavior(behavior: GSAISteeringBehavior) -> void:
_behaviors.append(behavior) _behaviors.append(behavior)
# Returns the behavior at the position in the pool referred to by `index`, or # Returns the behavior at the position in the pool referred to by `index`, or
# `null` if no behavior was found. # `null` if no behavior was found.
func get_behavior_at(index : int) -> GSAISteeringBehavior: func get_behavior(index : int) -> GSAISteeringBehavior:
if _behaviors.size() > index: if _behaviors.size() > index:
return _behaviors[index] return _behaviors[index]
@ -28,6 +27,19 @@ func get_behavior_at(index : int) -> GSAISteeringBehavior:
return null return null
func remove_behavior(index : int) -> void:
if _behaviors.size() > index:
_behaviors.remove(index)
return
printerr("Tried to get index " + str(index) + " in array of size " + str(_behaviors.size()))
return
func get_behaviour_count() -> int:
return _behaviors.size()
func _calculate_steering(accel : GSAITargetAcceleration) -> void: func _calculate_steering(accel : GSAITargetAcceleration) -> void:
var threshold_squared : float = zero_threshold * zero_threshold var threshold_squared : float = zero_threshold * zero_threshold

View File

@ -23,10 +23,12 @@ func _calculate_steering(acceleration : GSAITargetAcceleration) -> void:
predict_time = sqrt(predict_time_squared) predict_time = sqrt(predict_time_squared)
acceleration.linear = ((target_position + (target.linear_velocity * predict_time)) - agent.position).normalized() acceleration.linear = ((target_position + (target.linear_velocity * predict_time)) - agent.position).normalized()
acceleration.linear *= _get_modified_acceleration() acceleration.linear *= get_modified_acceleration()
acceleration.angular = 0 acceleration.angular = 0
func get_modified_acceleration() -> float:
return call("_get_modified_acceleration")
func _get_modified_acceleration() -> float: func _get_modified_acceleration() -> float:
return agent.linear_acceleration_max return agent.linear_acceleration_max

View File

@ -15,8 +15,8 @@ var acceleration : GSAITargetAcceleration
func _calculate_steering(_acceleration : GSAITargetAcceleration) -> void: func _calculate_steering(_acceleration : GSAITargetAcceleration) -> void:
acceleration.set_zero()
self.acceleration = _acceleration self.acceleration = _acceleration
acceleration.set_zero()
# warning-ignore:return_value_discarded # warning-ignore:return_value_discarded
proximity.find_neighbors(_callback) proximity.find_neighbors(_callback)

View File

@ -8,6 +8,8 @@ var proximity : GSAIProximity
var _callback : FuncRef = funcref(self, "_report_neighbor") var _callback : FuncRef = funcref(self, "_report_neighbor")
func get_callback() -> FuncRef:
return _callback
# Internal callback for the behavior to define whether or not a member is # Internal callback for the behavior to define whether or not a member is
# relevant # relevant

View File

@ -58,7 +58,7 @@ func calculate_distance(agent_current_position : Vector3) -> float:
for i in range(_segments.size()): for i in range(_segments.size()):
var segment: GSAISegment = _segments[i] var segment: GSAISegment = _segments[i]
var distance_squared : float = _calculate_point_segment_distance_squared(segment.begin, segment.end, agent_current_position) var distance_squared : float = calculate_point_segment_distance_squared(segment.begin, segment.end, agent_current_position)
if distance_squared < smallest_distance_squared: if distance_squared < smallest_distance_squared:
_nearest_point_on_path = _nearest_point_on_segment _nearest_point_on_path = _nearest_point_on_segment
@ -105,7 +105,7 @@ func get_end_point() -> Vector3:
return _segments.back().end return _segments.back().end
func _calculate_point_segment_distance_squared(start: Vector3, end: Vector3, position: Vector3) -> float: func calculate_point_segment_distance_squared(start: Vector3, end: Vector3, position: Vector3) -> float:
_nearest_point_on_segment = start _nearest_point_on_segment = start
var start_end : Vector3 = end - start var start_end : Vector3 = end - start
var start_end_length_squared : float = start_end.length_squared() var start_end_length_squared : float = start_end.length_squared()