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.
This commit is contained in:
Francois Belair 2020-02-06 14:46:21 -05:00
parent 9a207be03f
commit 57f3c4a24a
21 changed files with 38 additions and 61 deletions

View File

@ -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)

View File

@ -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)

View File

@ -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,

View File

@ -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)

View File

@ -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)

View File

@ -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(

View File

@ -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)

View File

@ -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)

View File

@ -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.

View File

@ -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

View File

@ -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

View File

@ -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)

View File

@ -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

View File

@ -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

View File

@ -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)

View File

@ -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)

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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