From 57f3c4a24a247e8e0a14a31a85ac1b55d7f8d6d8 Mon Sep 17 00:00:00 2001 From: Francois Belair Date: Thu, 6 Feb 2020 14:46:21 -0500 Subject: [PATCH] Make calculate_acceleration return void This makes the code more in line with GDQuest gdscript guidelines about not both transforming state and returning a value. --- project/demos/Arrive/Arriver.gd | 2 +- project/demos/AvoidCollisions/Avoider.gd | 2 +- project/demos/Face/Turret.gd | 2 +- project/demos/FollowPath/PathFollower.gd | 2 +- project/demos/GroupBehaviors/Member.gd | 2 +- project/demos/PursueSeek/Pursuer.gd | 4 ++-- project/demos/SeekFlee/Seeker.gd | 4 ++-- project/src/Behaviors/GSTArrive.gd | 8 +++----- project/src/Behaviors/GSTAvoidCollisions.gd | 4 +--- project/src/Behaviors/GSTBlend.gd | 4 +--- project/src/Behaviors/GSTCohesion.gd | 3 +-- project/src/Behaviors/GSTFace.gd | 9 ++++----- project/src/Behaviors/GSTFlee.gd | 4 +--- project/src/Behaviors/GSTFollowPath.gd | 10 +++++----- project/src/Behaviors/GSTLookWhereYouGo.gd | 5 ++--- project/src/Behaviors/GSTMatchOrientation.gd | 8 +++----- project/src/Behaviors/GSTPriority.gd | 4 +--- project/src/Behaviors/GSTPursue.gd | 4 +--- project/src/Behaviors/GSTSeek.gd | 4 +--- project/src/Behaviors/GSTSeparation.gd | 3 +-- project/src/GSTSteeringBehavior.gd | 11 ++++------- 21 files changed, 38 insertions(+), 61 deletions(-) diff --git a/project/demos/Arrive/Arriver.gd b/project/demos/Arrive/Arriver.gd index 9a71df8..dedce81 100644 --- a/project/demos/Arrive/Arriver.gd +++ b/project/demos/Arrive/Arriver.gd @@ -12,7 +12,7 @@ var _drag := 0.1 func _physics_process(delta: float) -> void: _update_agent() - _accel = arrive.calculate_steering(_accel) + arrive.calculate_steering(_accel) _velocity += Vector2(_accel.linear.x, _accel.linear.y) _velocity = _velocity.linear_interpolate(Vector2.ZERO, _drag).clamped(agent.linear_speed_max) _velocity = move_and_slide(_velocity) diff --git a/project/demos/AvoidCollisions/Avoider.gd b/project/demos/AvoidCollisions/Avoider.gd index ad87840..7027e13 100644 --- a/project/demos/AvoidCollisions/Avoider.gd +++ b/project/demos/AvoidCollisions/Avoider.gd @@ -28,7 +28,7 @@ func _draw() -> void: func _physics_process(delta: float) -> void: _update_agent() - _accel = priority.calculate_steering(_accel) + priority.calculate_steering(_accel) _velocity += Vector2(_accel.linear.x, _accel.linear.y) _velocity = _velocity.linear_interpolate(Vector2.ZERO, _drag) _velocity = _velocity.clamped(agent.linear_speed_max) diff --git a/project/demos/Face/Turret.gd b/project/demos/Face/Turret.gd index 468bf54..38fa4fa 100644 --- a/project/demos/Face/Turret.gd +++ b/project/demos/Face/Turret.gd @@ -19,7 +19,7 @@ func _ready() -> void: func _physics_process(delta: float) -> void: - _accel = face.calculate_steering(_accel) + face.calculate_steering(_accel) agent.angular_velocity = clamp( agent.angular_velocity + _accel.angular, -agent.angular_speed_max, diff --git a/project/demos/FollowPath/PathFollower.gd b/project/demos/FollowPath/PathFollower.gd index c19c9a6..9445050 100644 --- a/project/demos/FollowPath/PathFollower.gd +++ b/project/demos/FollowPath/PathFollower.gd @@ -34,7 +34,7 @@ func setup( func _physics_process(delta: float) -> void: if _valid: _update_agent() - _accel = follow.calculate_steering(_accel) + follow.calculate_steering(_accel) _velocity += Vector2(_accel.linear.x, _accel.linear.y) _velocity = _velocity.linear_interpolate(Vector2.ZERO, _drag) _velocity = _velocity.clamped(agent.linear_speed_max) diff --git a/project/demos/GroupBehaviors/Member.gd b/project/demos/GroupBehaviors/Member.gd index 43eca39..f3285a1 100644 --- a/project/demos/GroupBehaviors/Member.gd +++ b/project/demos/GroupBehaviors/Member.gd @@ -46,7 +46,7 @@ func _physics_process(delta: float) -> void: agent.position.x = global_position.x agent.position.y = global_position.y if blend: - acceleration = blend.calculate_steering(acceleration) + blend.calculate_steering(acceleration) _velocity += Vector2(acceleration.linear.x, acceleration.linear.y) _velocity = _velocity.linear_interpolate(Vector2.ZERO, 0.1) _velocity = _velocity.clamped(agent.linear_speed_max) diff --git a/project/demos/PursueSeek/Pursuer.gd b/project/demos/PursueSeek/Pursuer.gd index 133fd57..86cf241 100644 --- a/project/demos/PursueSeek/Pursuer.gd +++ b/project/demos/PursueSeek/Pursuer.gd @@ -25,11 +25,11 @@ func _ready() -> void: func _physics_process(delta: float) -> void: _update_agent() - accel = _behavior.calculate_steering(accel) + _behavior.calculate_steering(accel) _direction_face.position = agent.position + accel.linear.normalized() - accel = _orient_behavior.calculate_steering(accel) + _orient_behavior.calculate_steering(accel) _angular_velocity += accel.angular _angular_velocity = clamp( diff --git a/project/demos/SeekFlee/Seeker.gd b/project/demos/SeekFlee/Seeker.gd index d27b381..5dd86d6 100644 --- a/project/demos/SeekFlee/Seeker.gd +++ b/project/demos/SeekFlee/Seeker.gd @@ -25,9 +25,9 @@ func _physics_process(delta: float) -> void: _update_agent() if use_seek: - accel = seek.calculate_steering(accel) + seek.calculate_steering(accel) else: - accel = flee.calculate_steering(accel) + flee.calculate_steering(accel) velocity = (velocity + Vector2(accel.linear.x, accel.linear.y)).clamped(agent.linear_speed_max) velocity = move_and_slide(velocity) diff --git a/project/src/Behaviors/GSTArrive.gd b/project/src/Behaviors/GSTArrive.gd index 8b4a8ce..17083be 100644 --- a/project/src/Behaviors/GSTArrive.gd +++ b/project/src/Behaviors/GSTArrive.gd @@ -19,7 +19,7 @@ func _init(agent: GSTSteeringAgent, target: GSTAgentLocation).(agent) -> void: self.target = target -func _arrive(acceleration: GSTTargetAcceleration, target_position: Vector3) -> GSTTargetAcceleration: +func _arrive(acceleration: GSTTargetAcceleration, target_position: Vector3) -> void: var to_target := target_position - agent.position var distance := to_target.length() @@ -38,8 +38,6 @@ func _arrive(acceleration: GSTTargetAcceleration, target_position: Vector3) -> G acceleration.linear = GSTUtils.clampedv3(desired_velocity, agent.linear_acceleration_max) acceleration.angular = 0 - return acceleration - -func _calculate_steering(acceleration: GSTTargetAcceleration) -> GSTTargetAcceleration: - return _arrive(acceleration, target.position) +func _calculate_steering(acceleration: GSTTargetAcceleration) -> void: + _arrive(acceleration, target.position) diff --git a/project/src/Behaviors/GSTAvoidCollisions.gd b/project/src/Behaviors/GSTAvoidCollisions.gd index d00afc0..711b744 100644 --- a/project/src/Behaviors/GSTAvoidCollisions.gd +++ b/project/src/Behaviors/GSTAvoidCollisions.gd @@ -16,7 +16,7 @@ func _init(agent: GSTSteeringAgent, proximity: GSTProximity).(agent, proximity) pass -func _calculate_steering(acceleration: GSTTargetAcceleration) -> GSTTargetAcceleration: +func _calculate_steering(acceleration: GSTTargetAcceleration) -> void: _shortest_time = INF _first_neighbor = null _first_minimum_separation = 0 @@ -37,8 +37,6 @@ func _calculate_steering(acceleration: GSTTargetAcceleration) -> GSTTargetAccele acceleration.linear = acceleration.linear.normalized() * -agent.linear_acceleration_max acceleration.angular = 0 - 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. diff --git a/project/src/Behaviors/GSTBlend.gd b/project/src/Behaviors/GSTBlend.gd index 57daff3..db20eab 100644 --- a/project/src/Behaviors/GSTBlend.gd +++ b/project/src/Behaviors/GSTBlend.gd @@ -33,7 +33,7 @@ func get_behavior_at(index: int) -> Dictionary: return {} -func _calculate_steering(blended_accel: GSTTargetAcceleration) -> GSTTargetAcceleration: +func _calculate_steering(blended_accel: GSTTargetAcceleration) -> void: blended_accel.set_zero() for i in range(_behaviors.size()): @@ -48,5 +48,3 @@ func _calculate_steering(blended_accel: GSTTargetAcceleration) -> GSTTargetAccel -agent.angular_acceleration_max, agent.angular_acceleration_max ) - - return blended_accel diff --git a/project/src/Behaviors/GSTCohesion.gd b/project/src/Behaviors/GSTCohesion.gd index 062cc43..eb2c816 100644 --- a/project/src/Behaviors/GSTCohesion.gd +++ b/project/src/Behaviors/GSTCohesion.gd @@ -11,14 +11,13 @@ func _init(agent: GSTSteeringAgent, proximity: GSTProximity).(agent, proximity) pass -func _calculate_steering(acceleration: GSTTargetAcceleration) -> GSTTargetAcceleration: +func _calculate_steering(acceleration: GSTTargetAcceleration) -> void: acceleration.set_zero() _center_of_mass = Vector3.ZERO var neighbor_count = proximity._find_neighbors(_callback) if neighbor_count > 0: _center_of_mass *= 1.0 / neighbor_count acceleration.linear = (_center_of_mass - agent.position).normalized() * agent.linear_acceleration_max - return acceleration # Callback for the proximity to call when finding neighbors. Adds `neighbor`'s position diff --git a/project/src/Behaviors/GSTFace.gd b/project/src/Behaviors/GSTFace.gd index 81338a5..8a97e0b 100644 --- a/project/src/Behaviors/GSTFace.gd +++ b/project/src/Behaviors/GSTFace.gd @@ -8,17 +8,16 @@ func _init(agent: GSTSteeringAgent, target: GSTAgentLocation).(agent, target) -> pass -func _face(acceleration: GSTTargetAcceleration, target_position: Vector3) -> GSTTargetAcceleration: +func _face(acceleration: GSTTargetAcceleration, target_position: Vector3) -> void: var to_target := target_position - agent.position var distance_squared := to_target.length_squared() if distance_squared < agent.zero_linear_speed_threshold: acceleration.set_zero() - return acceleration else: var orientation = GSTUtils.vector3_to_angle(to_target) - return _match_orientation(acceleration, orientation) + _match_orientation(acceleration, orientation) -func _calculate_steering(acceleration: GSTTargetAcceleration) -> GSTTargetAcceleration: - return _face(acceleration, target.position) +func _calculate_steering(acceleration: GSTTargetAcceleration) -> void: + _face(acceleration, target.position) diff --git a/project/src/Behaviors/GSTFlee.gd b/project/src/Behaviors/GSTFlee.gd index e5aa961..86c071b 100644 --- a/project/src/Behaviors/GSTFlee.gd +++ b/project/src/Behaviors/GSTFlee.gd @@ -7,9 +7,7 @@ func _init(agent: GSTSteeringAgent, target: GSTAgentLocation).(agent, target) -> pass -func _calculate_steering(acceleration: GSTTargetAcceleration) -> GSTTargetAcceleration: +func _calculate_steering(acceleration: GSTTargetAcceleration) -> void: acceleration.linear = ( (agent.position - target.position).normalized() * agent.linear_acceleration_max) acceleration.angular = 0 - - return acceleration diff --git a/project/src/Behaviors/GSTFollowPath.gd b/project/src/Behaviors/GSTFollowPath.gd index 9d634c9..384d2d2 100644 --- a/project/src/Behaviors/GSTFollowPath.gd +++ b/project/src/Behaviors/GSTFollowPath.gd @@ -25,7 +25,7 @@ func _init( self.prediction_time = prediction_time -func _calculate_steering(acceleration: GSTTargetAcceleration) -> GSTTargetAcceleration: +func _calculate_steering(acceleration: GSTTargetAcceleration) -> void: var location := ( agent.position if prediction_time == 0 else agent.position + (agent.linear_velocity * prediction_time)) @@ -38,13 +38,13 @@ func _calculate_steering(acceleration: GSTTargetAcceleration) -> GSTTargetAccele if is_arrive_enabled and path.is_open: if path_offset >= 0: if target_distance > path.length - deceleration_radius: - return _arrive(acceleration, target_position) + _arrive(acceleration, target_position) + return else: if target_distance < deceleration_radius: - return _arrive(acceleration, target_position) + _arrive(acceleration, target_position) + return acceleration.linear = (target_position - agent.position).normalized() acceleration.linear *= agent.linear_acceleration_max acceleration.angular = 0 - - return acceleration diff --git a/project/src/Behaviors/GSTLookWhereYouGo.gd b/project/src/Behaviors/GSTLookWhereYouGo.gd index 7229e11..9e254f3 100644 --- a/project/src/Behaviors/GSTLookWhereYouGo.gd +++ b/project/src/Behaviors/GSTLookWhereYouGo.gd @@ -8,10 +8,9 @@ func _init(agent: GSTSteeringAgent).(agent, null) -> void: pass -func _calculate_steering(accel: GSTTargetAcceleration) -> GSTTargetAcceleration: +func _calculate_steering(accel: GSTTargetAcceleration) -> void: if agent.linear_velocity.length_squared() < agent.zero_linear_speed_threshold: accel.set_zero() - return accel else: var orientation := GSTUtils.vector3_to_angle(agent.linear_velocity) - return _match_orientation(accel, orientation) + _match_orientation(accel, orientation) diff --git a/project/src/Behaviors/GSTMatchOrientation.gd b/project/src/Behaviors/GSTMatchOrientation.gd index d132280..a682d70 100644 --- a/project/src/Behaviors/GSTMatchOrientation.gd +++ b/project/src/Behaviors/GSTMatchOrientation.gd @@ -20,7 +20,7 @@ func _init(agent: GSTSteeringAgent, target: GSTAgentLocation).(agent) -> void: self.target = target -func _match_orientation(acceleration: GSTTargetAcceleration, desired_orientation: float) -> GSTTargetAcceleration: +func _match_orientation(acceleration: GSTTargetAcceleration, desired_orientation: float) -> void: var rotation := wrapf(desired_orientation - agent.orientation, -PI, PI) var rotation_size := abs(rotation) @@ -43,8 +43,6 @@ func _match_orientation(acceleration: GSTTargetAcceleration, desired_orientation acceleration.linear = Vector3.ZERO - return acceleration - -func _calculate_steering(acceleration: GSTTargetAcceleration) -> GSTTargetAcceleration: - return _match_orientation(acceleration, target.orientation) +func _calculate_steering(acceleration: GSTTargetAcceleration) -> void: + _match_orientation(acceleration, target.orientation) diff --git a/project/src/Behaviors/GSTPriority.gd b/project/src/Behaviors/GSTPriority.gd index 184b1f1..f352461 100644 --- a/project/src/Behaviors/GSTPriority.gd +++ b/project/src/Behaviors/GSTPriority.gd @@ -31,7 +31,7 @@ func get_behavior_at(index: int) -> GSTSteeringBehavior: return null -func _calculate_steering(accel: GSTTargetAcceleration) -> GSTTargetAcceleration: +func _calculate_steering(accel: GSTTargetAcceleration) -> void: var threshold_squared := zero_threshold * zero_threshold last_selected_index = -1 @@ -48,5 +48,3 @@ func _calculate_steering(accel: GSTTargetAcceleration) -> GSTTargetAcceleration: break else: accel.set_zero() - - return accel diff --git a/project/src/Behaviors/GSTPursue.gd b/project/src/Behaviors/GSTPursue.gd index f9935ba..301bdd7 100644 --- a/project/src/Behaviors/GSTPursue.gd +++ b/project/src/Behaviors/GSTPursue.gd @@ -19,7 +19,7 @@ func _init( self.predict_time_max = predict_time_max -func _calculate_steering(acceleration: GSTTargetAcceleration) -> GSTTargetAcceleration: +func _calculate_steering(acceleration: GSTTargetAcceleration) -> void: var target_position := target.position var distance_squared := (target_position - agent.position).length_squared() @@ -37,8 +37,6 @@ func _calculate_steering(acceleration: GSTTargetAcceleration) -> GSTTargetAccele acceleration.angular = 0 - return acceleration - func _get_modified_acceleration() -> float: return agent.linear_acceleration_max diff --git a/project/src/Behaviors/GSTSeek.gd b/project/src/Behaviors/GSTSeek.gd index 4371f3f..61229e4 100644 --- a/project/src/Behaviors/GSTSeek.gd +++ b/project/src/Behaviors/GSTSeek.gd @@ -12,9 +12,7 @@ func _init(agent: GSTSteeringAgent, target: GSTAgentLocation).(agent) -> void: self.target = target -func _calculate_steering(acceleration: GSTTargetAcceleration) -> GSTTargetAcceleration: +func _calculate_steering(acceleration: GSTTargetAcceleration) -> void: acceleration.linear = ( (target.position - agent.position).normalized() * agent.linear_acceleration_max) acceleration.angular = 0 - - return acceleration diff --git a/project/src/Behaviors/GSTSeparation.gd b/project/src/Behaviors/GSTSeparation.gd index 0a3252e..87bec2a 100644 --- a/project/src/Behaviors/GSTSeparation.gd +++ b/project/src/Behaviors/GSTSeparation.gd @@ -18,11 +18,10 @@ func _init(agent: GSTSteeringAgent, proximity: GSTProximity).(agent, proximity) pass -func _calculate_steering(acceleration: GSTTargetAcceleration) -> GSTTargetAcceleration: +func _calculate_steering(acceleration: GSTTargetAcceleration) -> void: acceleration.set_zero() self._acceleration = acceleration proximity._find_neighbors(_callback) - return acceleration # Callback for the proximity to call when finding neighbors. Determines the amount of diff --git a/project/src/GSTSteeringBehavior.gd b/project/src/GSTSteeringBehavior.gd index c89a92f..b503d15 100644 --- a/project/src/GSTSteeringBehavior.gd +++ b/project/src/GSTSteeringBehavior.gd @@ -18,16 +18,13 @@ func _init(agent: GSTSteeringAgent) -> void: self.agent = agent -# Returns the `acceleration` modified with the behavior's desired amount of -# acceleration. -func calculate_steering(acceleration: GSTTargetAcceleration) -> GSTTargetAcceleration: +# Sets the `acceleration` with the behavior's desired amount of acceleration. +func calculate_steering(acceleration: GSTTargetAcceleration) -> void: if is_enabled: - return _calculate_steering(acceleration) + _calculate_steering(acceleration) else: acceleration.set_zero() - return acceleration -func _calculate_steering(acceleration: GSTTargetAcceleration) -> GSTTargetAcceleration: +func _calculate_steering(acceleration: GSTTargetAcceleration) -> void: acceleration.set_zero() - return acceleration