mirror of
https://github.com/Relintai/godot-steering-ai-framework.git
synced 2024-11-14 04:57:19 +01:00
6e6f27505c
The use of `not is_inside_tree()` before setting the setting class' value meant that the starting values would always be the default. Moving the value setting before checking for tree readiness fixes the issue.
57 lines
1.5 KiB
GDScript
57 lines
1.5 KiB
GDScript
extends KinematicBody2D
|
|
|
|
|
|
onready var agent := GSTSteeringAgent.new()
|
|
onready var path := GSTPath.new([
|
|
Vector3(global_position.x, global_position.y, 0),
|
|
Vector3(global_position.x, global_position.y, 0)
|
|
], true)
|
|
onready var follow := GSTFollowPath.new(agent, path, 0, 0)
|
|
|
|
var _velocity := Vector2.ZERO
|
|
var _accel := GSTTargetAcceleration.new()
|
|
var _valid := false
|
|
var _drag := 0.1
|
|
|
|
|
|
func setup(
|
|
path_offset: float,
|
|
predict_time: float,
|
|
max_accel: float,
|
|
max_speed: float,
|
|
decel_radius: float,
|
|
arrival_tolerance: float
|
|
) -> void:
|
|
owner.drawer.connect("path_established", self, "_on_Drawer_path_established")
|
|
follow.path_offset = path_offset
|
|
follow.prediction_time = predict_time
|
|
agent.max_linear_acceleration = max_accel
|
|
agent.max_linear_speed = max_speed
|
|
follow.deceleration_radius = decel_radius
|
|
follow.arrival_tolerance = arrival_tolerance
|
|
|
|
|
|
func _physics_process(delta: float) -> void:
|
|
if _valid:
|
|
_update_agent()
|
|
_accel = follow.calculate_steering(_accel)
|
|
_velocity += Vector2(_accel.linear.x, _accel.linear.y)
|
|
_velocity = _velocity.linear_interpolate(Vector2.ZERO, _drag)
|
|
_velocity = _velocity.clamped(agent.max_linear_speed)
|
|
_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:
|
|
var points3 := []
|
|
for p in points:
|
|
points3.append(Vector3(p.x, p.y, 0))
|
|
path.create_path(points3)
|
|
_valid = true
|