Amend docstring w/ virtual funcs and private vars

This commit is contained in:
Francois Belair 2020-01-30 13:06:35 -05:00
parent 0586878fcf
commit b62d7f05aa
7 changed files with 37 additions and 31 deletions

View File

@ -4,12 +4,12 @@ class_name GSTAvoidCollisions
extends GSTGroupBehavior extends GSTGroupBehavior
var first_neighbor: GSTSteeringAgent var _first_neighbor: GSTSteeringAgent
var shortest_time: float var _shortest_time: float
var first_minimum_separation: float var _first_minimum_separation: float
var first_distance: float var _first_distance: float
var first_relative_position: Vector3 var _first_relative_position: Vector3
var first_relative_velocity: Vector3 var _first_relative_velocity: Vector3
func _init(agent: GSTSteeringAgent, proximity: GSTProximity).(agent, proximity) -> void: func _init(agent: GSTSteeringAgent, proximity: GSTProximity).(agent, proximity) -> void:
@ -17,22 +17,22 @@ func _init(agent: GSTSteeringAgent, proximity: GSTProximity).(agent, proximity)
func _calculate_steering(acceleration: GSTTargetAcceleration) -> GSTTargetAcceleration: func _calculate_steering(acceleration: GSTTargetAcceleration) -> GSTTargetAcceleration:
shortest_time = INF _shortest_time = INF
first_neighbor = null _first_neighbor = null
first_minimum_separation = 0 _first_minimum_separation = 0
first_distance = 0 _first_distance = 0
var neighbor_count := proximity._find_neighbors(_callback) var neighbor_count := proximity._find_neighbors(_callback)
if neighbor_count == 0 or not first_neighbor: if neighbor_count == 0 or not _first_neighbor:
acceleration.set_zero() acceleration.set_zero()
else: else:
if( if(
first_minimum_separation <= 0 or _first_minimum_separation <= 0 or
first_distance < agent.bounding_radius + first_neighbor.bounding_radius): _first_distance < agent.bounding_radius + _first_neighbor.bounding_radius):
acceleration.linear = first_neighbor.position - agent.position acceleration.linear = _first_neighbor.position - agent.position
else: else:
acceleration.linear = first_relative_position + (first_relative_velocity * shortest_time) acceleration.linear = _first_relative_position + (_first_relative_velocity * _shortest_time)
acceleration.linear = acceleration.linear.normalized() * -agent.linear_acceleration_max acceleration.linear = acceleration.linear.normalized() * -agent.linear_acceleration_max
acceleration.angular = 0 acceleration.angular = 0
@ -52,7 +52,7 @@ func _report_neighbor(neighbor: GSTSteeringAgent) -> bool:
else: else:
var time_to_collision = -relative_position.dot(relative_velocity) / relative_speed_squared var time_to_collision = -relative_position.dot(relative_velocity) / relative_speed_squared
if time_to_collision <= 0 or time_to_collision >= shortest_time: if time_to_collision <= 0 or time_to_collision >= _shortest_time:
return false return false
else: else:
var distance = relative_position.length() var distance = relative_position.length()
@ -60,10 +60,10 @@ func _report_neighbor(neighbor: GSTSteeringAgent) -> bool:
if minimum_separation > agent.bounding_radius + neighbor.bounding_radius: if minimum_separation > agent.bounding_radius + neighbor.bounding_radius:
return false return false
else: else:
shortest_time = time_to_collision _shortest_time = time_to_collision
first_neighbor = neighbor _first_neighbor = neighbor
first_minimum_separation = minimum_separation _first_minimum_separation = minimum_separation
first_distance = distance _first_distance = distance
first_relative_position = relative_position _first_relative_position = relative_position
first_relative_velocity = relative_velocity _first_relative_velocity = relative_velocity
return true return true

View File

@ -4,7 +4,7 @@ class_name GSTCohesion
extends GSTGroupBehavior extends GSTGroupBehavior
var center_of_mass: Vector3 var _center_of_mass: Vector3
func _init(agent: GSTSteeringAgent, proximity: GSTProximity).(agent, proximity) -> void: func _init(agent: GSTSteeringAgent, proximity: GSTProximity).(agent, proximity) -> void:
@ -13,16 +13,17 @@ func _init(agent: GSTSteeringAgent, proximity: GSTProximity).(agent, proximity)
func _calculate_steering(acceleration: GSTTargetAcceleration) -> GSTTargetAcceleration: func _calculate_steering(acceleration: GSTTargetAcceleration) -> GSTTargetAcceleration:
acceleration.set_zero() acceleration.set_zero()
center_of_mass = Vector3.ZERO _center_of_mass = Vector3.ZERO
var neighbor_count = proximity._find_neighbors(_callback) var neighbor_count = proximity._find_neighbors(_callback)
if neighbor_count > 0: if neighbor_count > 0:
center_of_mass *= 1.0 / neighbor_count _center_of_mass *= 1.0 / neighbor_count
acceleration.linear = (center_of_mass - agent.position).normalized() * agent.linear_acceleration_max acceleration.linear = (_center_of_mass - agent.position).normalized() * agent.linear_acceleration_max
return acceleration return acceleration
# Callback for the proximity to call when finding neighbors. Adds `neighbor`'s position # Callback for the proximity to call when finding neighbors. Adds `neighbor`'s position
# to the center of mass of the group. # to the center of mass of the group.
# virtual
func _report_neighbor(neighbor: GSTSteeringAgent) -> bool: func _report_neighbor(neighbor: GSTSteeringAgent) -> bool:
center_of_mass += neighbor.position _center_of_mass += neighbor.position
return true return true

View File

@ -10,8 +10,8 @@ extends GSTGroupBehavior
# The coefficient to calculate how fast the separation strength decays with distance. # The coefficient to calculate how fast the separation strength decays with distance.
var decay_coefficient := 1.0 var decay_coefficient := 1.0
# Container for the calculated acceleration.
var acceleration: GSTTargetAcceleration var _acceleration: GSTTargetAcceleration
func _init(agent: GSTSteeringAgent, proximity: GSTProximity).(agent, proximity) -> void: func _init(agent: GSTSteeringAgent, proximity: GSTProximity).(agent, proximity) -> void:
@ -20,13 +20,14 @@ func _init(agent: GSTSteeringAgent, proximity: GSTProximity).(agent, proximity)
func _calculate_steering(acceleration: GSTTargetAcceleration) -> GSTTargetAcceleration: func _calculate_steering(acceleration: GSTTargetAcceleration) -> GSTTargetAcceleration:
acceleration.set_zero() acceleration.set_zero()
self.acceleration = acceleration self._acceleration = acceleration
proximity._find_neighbors(_callback) proximity._find_neighbors(_callback)
return acceleration return acceleration
# Callback for the proximity to call when finding neighbors. Determines the amount of # Callback for the proximity to call when finding neighbors. Determines the amount of
# acceleration that `neighbor` imposes based on its distance from the owner agent. # acceleration that `neighbor` imposes based on its distance from the owner agent.
# virtual
func _report_neighbor(neighbor: GSTSteeringAgent) -> bool: func _report_neighbor(neighbor: GSTSteeringAgent) -> bool:
var to_agent := agent.position - neighbor.position var to_agent := agent.position - neighbor.position
@ -37,6 +38,6 @@ func _report_neighbor(neighbor: GSTSteeringAgent) -> bool:
if strength > acceleration_max: if strength > acceleration_max:
strength = acceleration_max strength = acceleration_max
acceleration.linear += to_agent * (strength / sqrt(distance_squared)) _acceleration.linear += to_agent * (strength / sqrt(distance_squared))
return true return true

View File

@ -15,5 +15,6 @@ func _init(agent: GSTSteeringAgent, proximity: GSTProximity).(agent) -> void:
# 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
# virtual
func _report_neighbor(neighbor: GSTSteeringAgent) -> bool: func _report_neighbor(neighbor: GSTSteeringAgent) -> bool:
return false return false

View File

@ -12,6 +12,7 @@ func _init(agent: GSTSteeringAgent, agents: Array).(agent, agents) -> void:
# #
# `_find_neighbors` calls `callback` for each agent in the `agents` array and # `_find_neighbors` calls `callback` for each agent in the `agents` array and
# adds one to the count if its `callback` returns true. # adds one to the count if its `callback` returns true.
# virtual
func _find_neighbors(callback: FuncRef) -> int: func _find_neighbors(callback: FuncRef) -> int:
var neighbor_count := 0 var neighbor_count := 0
var agent_count := agents.size() var agent_count := agents.size()

View File

@ -18,5 +18,6 @@ func _init(agent: GSTSteeringAgent, agents: Array) -> void:
# #
# `_find_neighbors` calls `callback` for each agent in the `agents` array and # `_find_neighbors` calls `callback` for each agent in the `agents` array and
# adds one to the count if its `callback` returns true. # adds one to the count if its `callback` returns true.
# virtual
func _find_neighbors(callback: FuncRef) -> int: func _find_neighbors(callback: FuncRef) -> int:
return 0 return 0

View File

@ -20,6 +20,7 @@ func _init(agent: GSTSteeringAgent, agents: Array, radius: float).(agent, agents
# #
# `_find_neighbors` calls `callback` for each agent in the `agents` array that lie within # `_find_neighbors` calls `callback` for each agent in the `agents` array that lie within
# the radius around the owning agent and adds one to the count if its `callback` returns true. # the radius around the owning agent and adds one to the count if its `callback` returns true.
# virtual
func _find_neighbors(callback: FuncRef) -> int: func _find_neighbors(callback: FuncRef) -> int:
var agent_count := agents.size() var agent_count := agents.size()
var neighbor_count := 0 var neighbor_count := 0