2020-01-29 05:56:10 +01:00
|
|
|
# Produces a linear acceleration that moves the agent along the specified path.
|
2020-02-11 18:33:25 +01:00
|
|
|
class_name GSAIFollowPath
|
|
|
|
extends GSAIArrive
|
2020-01-09 18:26:35 +01:00
|
|
|
|
|
|
|
|
2020-01-29 17:04:04 +01:00
|
|
|
# The path to follow and travel along.
|
2020-02-11 18:33:25 +01:00
|
|
|
var path: GSAIPath
|
2020-01-27 19:24:05 +01:00
|
|
|
# The distance along the path to generate the next target position.
|
2020-01-16 09:44:44 +01:00
|
|
|
var path_offset := 0.0
|
2020-01-09 18:26:35 +01:00
|
|
|
|
2020-02-11 18:33:25 +01:00
|
|
|
# Whether to use `GSAIArrive` behavior on an open path.
|
2020-01-29 22:53:57 +01:00
|
|
|
var is_arrive_enabled := true
|
2020-01-29 17:04:04 +01:00
|
|
|
# The amount of time in the future to predict the owning agent's position along
|
|
|
|
# the path. Setting it to 0.0 will force non-predictive path following.
|
2020-01-16 09:44:44 +01:00
|
|
|
var prediction_time := 0.0
|
2020-01-09 18:26:35 +01:00
|
|
|
|
|
|
|
|
|
|
|
func _init(
|
2020-02-11 18:33:25 +01:00
|
|
|
agent: GSAISteeringAgent,
|
2020-02-11 20:36:06 +01:00
|
|
|
_path: GSAIPath,
|
|
|
|
_path_offset := 0.0,
|
|
|
|
_prediction_time := 0.0).(agent, null) -> void:
|
|
|
|
self.path = _path
|
|
|
|
self.path_offset = _path_offset
|
|
|
|
self.prediction_time = _prediction_time
|
2020-01-09 18:26:35 +01:00
|
|
|
|
|
|
|
|
2020-02-11 18:33:25 +01:00
|
|
|
func _calculate_steering(acceleration: GSAITargetAcceleration) -> void:
|
2020-01-16 09:44:44 +01:00
|
|
|
var location := (
|
2020-01-09 18:26:35 +01:00
|
|
|
agent.position if prediction_time == 0
|
|
|
|
else agent.position + (agent.linear_velocity * prediction_time))
|
2020-01-29 17:04:04 +01:00
|
|
|
|
2020-01-27 18:57:51 +01:00
|
|
|
var distance := path.calculate_distance(location)
|
2020-01-16 09:44:44 +01:00
|
|
|
var target_distance := distance + path_offset
|
2020-02-08 18:05:24 +01:00
|
|
|
|
|
|
|
if prediction_time > 0 and path.is_open:
|
|
|
|
if target_distance < path.calculate_distance(agent.position):
|
|
|
|
target_distance = path.length
|
2020-01-29 17:04:04 +01:00
|
|
|
|
2020-01-27 18:57:51 +01:00
|
|
|
var target_position := path.calculate_target_position(target_distance)
|
2020-02-08 18:05:24 +01:00
|
|
|
|
2020-01-29 22:53:57 +01:00
|
|
|
if is_arrive_enabled and path.is_open:
|
2020-01-09 18:26:35 +01:00
|
|
|
if path_offset >= 0:
|
|
|
|
if target_distance > path.length - deceleration_radius:
|
2020-02-06 20:46:21 +01:00
|
|
|
_arrive(acceleration, target_position)
|
|
|
|
return
|
2020-01-09 18:26:35 +01:00
|
|
|
else:
|
|
|
|
if target_distance < deceleration_radius:
|
2020-02-06 20:46:21 +01:00
|
|
|
_arrive(acceleration, target_position)
|
|
|
|
return
|
2020-01-29 17:04:04 +01:00
|
|
|
|
2020-01-09 18:26:35 +01:00
|
|
|
acceleration.linear = (target_position - agent.position).normalized()
|
2020-01-22 17:55:49 +01:00
|
|
|
acceleration.linear *= agent.linear_acceleration_max
|
2020-01-09 18:26:35 +01:00
|
|
|
acceleration.angular = 0
|