2020-01-15 20:42:24 +01:00
|
|
|
extends KinematicBody2D
|
|
|
|
|
|
|
|
|
2020-01-22 17:55:49 +01:00
|
|
|
var _velocity := Vector2.ZERO
|
|
|
|
var _accel := GSTTargetAcceleration.new()
|
|
|
|
var _valid := false
|
|
|
|
var _drag := 0.1
|
|
|
|
|
2020-01-16 12:10:55 +01:00
|
|
|
onready var agent := GSTSteeringAgent.new()
|
|
|
|
onready var path := GSTPath.new([
|
2020-01-15 20:42:24 +01:00
|
|
|
Vector3(global_position.x, global_position.y, 0),
|
|
|
|
Vector3(global_position.x, global_position.y, 0)
|
|
|
|
], true)
|
2020-01-16 18:31:02 +01:00
|
|
|
onready var follow := GSTFollowPath.new(agent, path, 0, 0)
|
2020-01-15 20:42:24 +01:00
|
|
|
|
|
|
|
|
2020-01-16 18:31:02 +01:00
|
|
|
func setup(
|
|
|
|
path_offset: float,
|
|
|
|
predict_time: float,
|
2020-01-22 17:55:49 +01:00
|
|
|
accel_max: float,
|
|
|
|
speed_max: float,
|
2020-01-16 18:31:02 +01:00
|
|
|
decel_radius: float,
|
|
|
|
arrival_tolerance: float
|
|
|
|
) -> void:
|
2020-01-15 20:42:24 +01:00
|
|
|
owner.drawer.connect("path_established", self, "_on_Drawer_path_established")
|
2020-01-16 18:31:02 +01:00
|
|
|
follow.path_offset = path_offset
|
|
|
|
follow.prediction_time = predict_time
|
2020-01-22 17:55:49 +01:00
|
|
|
agent.linear_acceleration_max = accel_max
|
|
|
|
agent.linear_speed_max = speed_max
|
2020-01-16 18:31:02 +01:00
|
|
|
follow.deceleration_radius = decel_radius
|
|
|
|
follow.arrival_tolerance = arrival_tolerance
|
2020-01-15 20:42:24 +01:00
|
|
|
|
|
|
|
|
|
|
|
func _physics_process(delta: float) -> void:
|
|
|
|
if _valid:
|
|
|
|
_update_agent()
|
|
|
|
_accel = follow.calculate_steering(_accel)
|
|
|
|
_velocity += Vector2(_accel.linear.x, _accel.linear.y)
|
2020-01-16 18:31:02 +01:00
|
|
|
_velocity = _velocity.linear_interpolate(Vector2.ZERO, _drag)
|
2020-01-22 17:55:49 +01:00
|
|
|
_velocity = _velocity.clamped(agent.linear_speed_max)
|
2020-01-15 20:42:24 +01:00
|
|
|
_velocity = move_and_slide(_velocity)
|
|
|
|
|
|
|
|
|
|
|
|
func _update_agent() -> void:
|
|
|
|
agent.position.x = global_position.x
|
|
|
|
agent.position.y = global_position.y
|
|
|
|
agent.linear_velocity.x = _velocity.x
|
|
|
|
agent.linear_velocity.y = _velocity.y
|
|
|
|
|
|
|
|
|
|
|
|
func _on_Drawer_path_established(points: Array) -> void:
|
2020-01-16 12:10:55 +01:00
|
|
|
var points3 := []
|
2020-01-15 20:42:24 +01:00
|
|
|
for p in points:
|
|
|
|
points3.append(Vector3(p.x, p.y, 0))
|
|
|
|
path.create_path(points3)
|
|
|
|
_valid = true
|