2019-12-21 03:32:02 +01:00
|
|
|
extends KinematicBody2D
|
|
|
|
|
|
|
|
|
2020-01-15 20:15:31 +01:00
|
|
|
onready var agent: = GSTSteeringAgent.new()
|
|
|
|
onready var target: = GSTAgentLocation.new()
|
|
|
|
onready var arrive: = GSTArrive.new(agent, target)
|
2019-12-23 17:38:27 +01:00
|
|
|
var _accel: = GSTTargetAcceleration.new()
|
2019-12-21 03:32:02 +01:00
|
|
|
|
|
|
|
var _velocity: = Vector2()
|
2020-01-15 20:15:31 +01:00
|
|
|
var _drag: = 0.1
|
2019-12-21 03:32:02 +01:00
|
|
|
|
|
|
|
|
|
|
|
func _ready() -> void:
|
2020-01-15 20:15:31 +01:00
|
|
|
agent.max_linear_speed = owner.max_linear_speed
|
|
|
|
agent.max_linear_acceleration = owner.max_linear_accel
|
|
|
|
agent.position = Vector3(global_position.x, global_position.y, 0)
|
|
|
|
arrive.deceleration_radius = owner.deceleration_radius
|
|
|
|
arrive.arrival_tolerance = owner.arrival_tolerance
|
|
|
|
target.position = agent.position
|
2019-12-21 03:32:02 +01:00
|
|
|
|
|
|
|
|
|
|
|
func _unhandled_input(event: InputEvent) -> void:
|
|
|
|
if event is InputEventMouseButton:
|
|
|
|
var mb: InputEventMouseButton = event
|
|
|
|
if mb.button_index == BUTTON_LEFT and mb.pressed:
|
2020-01-15 20:15:31 +01:00
|
|
|
target.position = Vector3(mb.position.x, mb.position.y, 0)
|
|
|
|
owner.target.position = mb.position
|
2019-12-21 03:32:02 +01:00
|
|
|
|
|
|
|
|
|
|
|
func _physics_process(delta: float) -> void:
|
2020-01-15 20:15:31 +01:00
|
|
|
_update_agent()
|
|
|
|
_accel = arrive.calculate_steering(_accel)
|
2019-12-21 03:32:02 +01:00
|
|
|
_velocity += Vector2(_accel.linear.x, _accel.linear.y)
|
2020-01-15 20:15:31 +01:00
|
|
|
_velocity = _velocity.linear_interpolate(Vector2.ZERO, _drag).clamped(agent.max_linear_speed)
|
2019-12-21 03:32:02 +01:00
|
|
|
_velocity = move_and_slide(_velocity)
|
|
|
|
|
|
|
|
|
|
|
|
func _update_agent() -> void:
|
2020-01-15 20:15:31 +01:00
|
|
|
agent.position = Vector3(global_position.x, global_position.y, 0)
|
|
|
|
agent.linear_velocity = Vector3(_velocity.x, _velocity.y, 0)
|