diff --git a/project/src/Behaviors/GSTArrive.gd b/project/src/Behaviors/GSTArrive.gd index c150f53..16fab06 100644 --- a/project/src/Behaviors/GSTArrive.gd +++ b/project/src/Behaviors/GSTArrive.gd @@ -1,7 +1,7 @@ -class_name GSTArrive -extends GSTSteeringBehavior # Calculates acceleration to take an agent to its target's location. # The calculation will attempt to arrive with zero remaining velocity. +class_name GSTArrive +extends GSTSteeringBehavior # The target whose location the agent will be steered to arrive at @@ -10,7 +10,7 @@ var target: GSTAgentLocation var arrival_tolerance: float # The distance from the target for the agent to begin slowing down var deceleration_radius: float -# A constant that represents the time it takes to change acceleration +# The amount of time to reach the target velocity var time_to_reach := 0.1 diff --git a/project/src/Behaviors/GSTAvoidCollisions.gd b/project/src/Behaviors/GSTAvoidCollisions.gd index 7fa99f0..def0890 100644 --- a/project/src/Behaviors/GSTAvoidCollisions.gd +++ b/project/src/Behaviors/GSTAvoidCollisions.gd @@ -1,6 +1,6 @@ +# Behavior that steers the agent to avoid obstacles lying in its path as approximated by spheres. class_name GSTAvoidCollisions extends GSTGroupBehavior -# Behavior that steers the agent to avoid obstacles lying in its path as approximated by spheres. var first_neighbor: GSTSteeringAgent @@ -39,6 +39,8 @@ func _calculate_steering(acceleration: GSTTargetAcceleration) -> GSTTargetAccele return acceleration +# Callback for the proximity to call when finding neighbors. Keeps track of every `neighbor` +# that was found but only keeps the one the owning agent will most likely collide with. func report_neighbor(neighbor: GSTSteeringAgent) -> bool: var relative_position := neighbor.position - agent.position var relative_velocity := neighbor.linear_velocity - agent.linear_velocity diff --git a/project/src/Behaviors/GSTBlend.gd b/project/src/Behaviors/GSTBlend.gd index c26e71e..5a451b6 100644 --- a/project/src/Behaviors/GSTBlend.gd +++ b/project/src/Behaviors/GSTBlend.gd @@ -1,5 +1,3 @@ -class_name GSTBlend -extends GSTSteeringBehavior # Blends multiple steering behaviors into one, and returns acceleration combining all of them. # # Each behavior is associated with a weight - a modifier by which the result will be multiplied by, @@ -7,6 +5,8 @@ extends GSTSteeringBehavior # # Each behavior is stored internally as a `Dictionary` with a `behavior` key with a value of type # `GSTSteeringBehavior` and a `weight` key with a value of type float. +class_name GSTBlend +extends GSTSteeringBehavior var _behaviors := [] diff --git a/project/src/Behaviors/GSTCohesion.gd b/project/src/Behaviors/GSTCohesion.gd index b3d8059..1e7e244 100644 --- a/project/src/Behaviors/GSTCohesion.gd +++ b/project/src/Behaviors/GSTCohesion.gd @@ -1,7 +1,7 @@ -class_name GSTCohesion -extends GSTGroupBehavior # Group behavior that produces linear acceleration that attempts to move the agent towards the # center of mass of the agents in the area defined by the Proximity. +class_name GSTCohesion +extends GSTGroupBehavior var center_of_mass: Vector3 @@ -21,6 +21,8 @@ func _calculate_steering(acceleration: GSTTargetAcceleration) -> GSTTargetAccele return acceleration +# Callback for the proximity to call when finding neighbors. Adds `neighbor`'s position +# to the center of mass of the group. func report_neighbor(neighbor: GSTSteeringAgent) -> bool: center_of_mass += neighbor.position return true diff --git a/project/src/Behaviors/GSTEvade.gd b/project/src/Behaviors/GSTEvade.gd index b32e849..8499feb 100644 --- a/project/src/Behaviors/GSTEvade.gd +++ b/project/src/Behaviors/GSTEvade.gd @@ -1,6 +1,6 @@ +# Calculates acceleration to take an agent away from where a target agent will be. class_name GSTEvade extends GSTPursue -# Calculates acceleration to take an agent away from where a target agent will be. func _init( diff --git a/project/src/Behaviors/GSTFace.gd b/project/src/Behaviors/GSTFace.gd index ab9bd61..384745c 100644 --- a/project/src/Behaviors/GSTFace.gd +++ b/project/src/Behaviors/GSTFace.gd @@ -1,7 +1,7 @@ -class_name GSTFace -extends GSTMatchOrientation # Calculates angular acceleration to rotate a target to face its target's position. # The acceleration will attempt to arrive with zero remaining angular velocity. +class_name GSTFace +extends GSTMatchOrientation func _init(agent: GSTSteeringAgent, target: GSTAgentLocation).(agent, target) -> void: diff --git a/project/src/Behaviors/GSTFlee.gd b/project/src/Behaviors/GSTFlee.gd index 35196f5..284a579 100644 --- a/project/src/Behaviors/GSTFlee.gd +++ b/project/src/Behaviors/GSTFlee.gd @@ -1,6 +1,6 @@ +# Calculates acceleration to take an agent directly away from a target agent. class_name GSTFlee extends GSTSeek -# Calculates acceleration to take an agent directly away from a target agent. func _init(agent: GSTSteeringAgent, target: GSTAgentLocation).(agent, target) -> void: diff --git a/project/src/Behaviors/GSTFollowPath.gd b/project/src/Behaviors/GSTFollowPath.gd index d4cf6e6..96cc2b5 100644 --- a/project/src/Behaviors/GSTFollowPath.gd +++ b/project/src/Behaviors/GSTFollowPath.gd @@ -1,6 +1,6 @@ +# Produces a linear acceleration that moves the agent along the specified path. class_name GSTFollowPath extends GSTArrive -# Produces a linear acceleration that moves the agent along the specified path. # The path to follow and travel along diff --git a/project/src/Behaviors/GSTLookWhereYouGo.gd b/project/src/Behaviors/GSTLookWhereYouGo.gd index 3057c1f..bc76876 100644 --- a/project/src/Behaviors/GSTLookWhereYouGo.gd +++ b/project/src/Behaviors/GSTLookWhereYouGo.gd @@ -1,6 +1,6 @@ +# Calculates an angular acceleration to match an agent's orientation to its direction of travel. class_name GSTLookWhereYouGo extends GSTMatchOrientation -# Calculates an angular acceleration to match an agent's orientation to its direction of travel. func _init(agent: GSTSteeringAgent).(agent, null) -> void: diff --git a/project/src/Behaviors/GSTMatchOrientation.gd b/project/src/Behaviors/GSTMatchOrientation.gd index 38405ff..743dc5b 100644 --- a/project/src/Behaviors/GSTMatchOrientation.gd +++ b/project/src/Behaviors/GSTMatchOrientation.gd @@ -1,7 +1,7 @@ -class_name GSTMatchOrientation -extends GSTSteeringBehavior # Calculates an angular acceleration to match an agent's orientation to its target's. # The calculation will attempt to arrive with zero remaining angular velocity. +class_name GSTMatchOrientation +extends GSTSteeringBehavior # The target orientation for the behavior to try and match rotations to @@ -10,7 +10,7 @@ var target: GSTAgentLocation var alignment_tolerance: float # The amount of distance in radians from the goal to start slowing down var deceleration_radius: float -# A constant to represent the time it takes to change angular accelerations +# The amount of time to reach the target velocity var time_to_reach: float = 0.1 diff --git a/project/src/Behaviors/GSTPriority.gd b/project/src/Behaviors/GSTPriority.gd index b05bf41..cf5c2bf 100644 --- a/project/src/Behaviors/GSTPriority.gd +++ b/project/src/Behaviors/GSTPriority.gd @@ -1,6 +1,6 @@ +# Contains multiple behaviors and returns only the result of the first with non-zero acceleration. class_name GSTPriority extends GSTSteeringBehavior -# Contains multiple behaviors and returns only the result of the first with non-zero acceleration. var _behaviors := [] diff --git a/project/src/Behaviors/GSTPursue.gd b/project/src/Behaviors/GSTPursue.gd index df783fc..6d98a73 100644 --- a/project/src/Behaviors/GSTPursue.gd +++ b/project/src/Behaviors/GSTPursue.gd @@ -1,7 +1,7 @@ -class_name GSTPursue -extends GSTSteeringBehavior # Calculates acceleration to take an agent to intersect with where a target agent will be, instead # of where it currently is. +class_name GSTPursue +extends GSTSteeringBehavior # The target agent that the behavior is trying to intercept diff --git a/project/src/Behaviors/GSTSeek.gd b/project/src/Behaviors/GSTSeek.gd index 2d0961f..fa4cfba 100644 --- a/project/src/Behaviors/GSTSeek.gd +++ b/project/src/Behaviors/GSTSeek.gd @@ -1,6 +1,6 @@ +# Calculates acceleration to take an agent to a target agent's position directly class_name GSTSeek extends GSTSteeringBehavior -# Calculates acceleration to take an agent to a target agent's position directly # The target that the behavior aims to move the agent to diff --git a/project/src/Behaviors/GSTSeparation.gd b/project/src/Behaviors/GSTSeparation.gd index a6d5b17..296372c 100644 --- a/project/src/Behaviors/GSTSeparation.gd +++ b/project/src/Behaviors/GSTSeparation.gd @@ -1,13 +1,13 @@ -class_name GSTSeparation -extends GSTGroupBehavior # Group behavior that produces acceleration that repels the agent from the other neighbors that # are in the area defined by the given `GSTProximity`. # # The produced acceleration is an average of all agents under consideration, multiplied by a # strength decreasing by the inverse square law in relation to distance, and accumulated. +class_name GSTSeparation +extends GSTGroupBehavior -# The constant 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 acceleration: GSTTargetAcceleration @@ -24,6 +24,8 @@ func _calculate_steering(acceleration: GSTTargetAcceleration) -> GSTTargetAccele return acceleration +# 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. func report_neighbor(neighbor: GSTSteeringAgent) -> bool: var to_agent := agent.position - neighbor.position diff --git a/project/src/GSTAgentLocation.gd b/project/src/GSTAgentLocation.gd index 881fd23..fba8a8d 100644 --- a/project/src/GSTAgentLocation.gd +++ b/project/src/GSTAgentLocation.gd @@ -1,5 +1,5 @@ -class_name GSTAgentLocation # Represents an agent with only a location and an orientation. +class_name GSTAgentLocation # The agent's position in space. diff --git a/project/src/GSTGroupBehavior.gd b/project/src/GSTGroupBehavior.gd index 3d9f340..4336e71 100644 --- a/project/src/GSTGroupBehavior.gd +++ b/project/src/GSTGroupBehavior.gd @@ -1,9 +1,9 @@ +# Base type for group-based steering behaviors. extends GSTSteeringBehavior class_name GSTGroupBehavior -# Base type for group-based steering behaviors. -# Container to find neighbors of the agent and apply a group behavior. +# Container to find neighbors of the agent and calculate group behavior. var proximity: GSTProximity var _callback := funcref(self, "report_neighbor") diff --git a/project/src/GSTPath.gd b/project/src/GSTPath.gd index bc04136..56a0591 100644 --- a/project/src/GSTPath.gd +++ b/project/src/GSTPath.gd @@ -1,6 +1,6 @@ -extends Reference class_name GSTPath # Represents a path made up of Vector3 waypoints, split into segments path # follow behaviors can use. +extends Reference class_name GSTPath # If `false`, the path loops. diff --git a/project/src/GSTSteeringAgent.gd b/project/src/GSTSteeringAgent.gd index cafbdf9..fa58033 100644 --- a/project/src/GSTSteeringAgent.gd +++ b/project/src/GSTSteeringAgent.gd @@ -1,9 +1,9 @@ -extends GSTAgentLocation -class_name GSTSteeringAgent # Adds velocity, speed, and size data to `GSTAgentLocation`. # # It is the character's responsibility to keep this information up to date for # the steering toolkit to work correctly. +extends GSTAgentLocation +class_name GSTSteeringAgent # The amount of velocity to be considered effectively not moving. diff --git a/project/src/GSTSteeringBehavior.gd b/project/src/GSTSteeringBehavior.gd index 0cab174..d0bd881 100644 --- a/project/src/GSTSteeringBehavior.gd +++ b/project/src/GSTSteeringBehavior.gd @@ -1,4 +1,3 @@ -class_name GSTSteeringBehavior # Base class for all steering behaviors. # # Steering behaviors calculate the linear and the angular acceleration to be @@ -6,6 +5,7 @@ class_name GSTSteeringBehavior # # The `calculate_steering` function is the entry point for all behaviors. # Individual steering behaviors encapsulate the steering logic. +class_name GSTSteeringBehavior # If `false`, all calculations return zero amounts of acceleration. diff --git a/project/src/GSTTargetAcceleration.gd b/project/src/GSTTargetAcceleration.gd index d976964..b03c11f 100644 --- a/project/src/GSTTargetAcceleration.gd +++ b/project/src/GSTTargetAcceleration.gd @@ -1,6 +1,6 @@ -class_name GSTTargetAcceleration # A desired linear and angular amount of acceleration requested by the steering # system. +class_name GSTTargetAcceleration # Linear acceleration diff --git a/project/src/GSTUtils.gd b/project/src/GSTUtils.gd index f256eaf..4a5c311 100644 --- a/project/src/GSTUtils.gd +++ b/project/src/GSTUtils.gd @@ -1,5 +1,5 @@ -class_name GSTUtils # Math and vector utility functions. +class_name GSTUtils # Returns the `vector` with its length capped to `limit`. diff --git a/project/src/Proximities/GSTInfiniteProximity.gd b/project/src/Proximities/GSTInfiniteProximity.gd index a6d3a2d..dbe358c 100644 --- a/project/src/Proximities/GSTInfiniteProximity.gd +++ b/project/src/Proximities/GSTInfiniteProximity.gd @@ -1,7 +1,7 @@ +# Determines any agent that is in the specified list as being neighbors with the +# owner agent, regardless of distance. extends GSTProximity class_name GSTInfiniteProximity -# Determines any agent that is in the specified list as being neighbors with the -# owner agent. func _init(agent: GSTSteeringAgent, agents: Array).(agent, agents) -> void: @@ -11,7 +11,7 @@ func _init(agent: GSTSteeringAgent, agents: Array).(agent, agents) -> void: # Returns a number of neighbors based on a `callback` function. # # `find_neighbors` calls `callback` for each agent in the `agents` array and -# adds one to the count it if `callback` returns true. +# adds one to the count if its `callback` returns true. func find_neighbors(callback: FuncRef) -> int: var neighbor_count := 0 var agent_count := agents.size() diff --git a/project/src/Proximities/GSTProximity.gd b/project/src/Proximities/GSTProximity.gd index 718af9d..0439cad 100644 --- a/project/src/Proximities/GSTProximity.gd +++ b/project/src/Proximities/GSTProximity.gd @@ -1,9 +1,11 @@ +# Base container type that stores data to find the neighbors of an agent. extends Reference class_name GSTProximity -# Base container type that stores data to find the neighbors of an agent. +# The owning agent whose neighbors are found in the group var agent: GSTSteeringAgent +# The agents who are part of this group and could be potential neighbors var agents := [] @@ -12,5 +14,9 @@ func _init(agent: GSTSteeringAgent, agents: Array) -> void: self.agents = agents +# Returns a number of neighbors based on a `callback` function. +# +# `find_neighbors` calls `callback` for each agent in the `agents` array and +# adds one to the count if its `callback` returns true. func find_neighbors(callback: FuncRef) -> int: return 0 diff --git a/project/src/Proximities/GSTRadiusProximity.gd b/project/src/Proximities/GSTRadiusProximity.gd index 511f8c0..cb90527 100644 --- a/project/src/Proximities/GSTRadiusProximity.gd +++ b/project/src/Proximities/GSTRadiusProximity.gd @@ -1,9 +1,10 @@ -extends GSTProximity -class_name GSTRadiusProximity # Determines any agent that is in the specified list as being neighbors with the owner agent if # they lie within the specified radius. +extends GSTProximity +class_name GSTRadiusProximity +# The radius around the owning agent to find neighbors in var radius := 0.0 var _last_frame := 0 @@ -15,6 +16,10 @@ func _init(agent: GSTSteeringAgent, agents: Array, radius: float).(agent, agents _scene_tree = Engine.get_main_loop() +# Returns a number of neighbors based on a `callback` function. +# +# `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. func find_neighbors(callback: FuncRef) -> int: var agent_count := agents.size() var neighbor_count := 0