2020-01-16 16:04:27 +01:00
|
|
|
extends KinematicBody2D
|
2020-01-02 23:42:41 +01:00
|
|
|
# Represents a ship that chases after the player.
|
2019-12-19 21:24:40 +01:00
|
|
|
|
|
|
|
|
2020-01-16 09:44:44 +01:00
|
|
|
onready var agent := GSTSteeringAgent.new()
|
|
|
|
onready var accel := GSTTargetAcceleration.new()
|
2019-12-19 21:24:40 +01:00
|
|
|
onready var player_agent: GSTSteeringAgent = owner.find_node("Player", true, false).agent
|
|
|
|
|
|
|
|
export var use_seek: bool = false
|
|
|
|
|
|
|
|
var _orient_behavior: GSTSteeringBehavior
|
|
|
|
var _behavior: GSTSteeringBehavior
|
|
|
|
|
2020-01-16 09:44:44 +01:00
|
|
|
var _linear_velocity := Vector2()
|
|
|
|
var _linear_drag_coefficient := 0.025
|
|
|
|
var _angular_velocity := 0.0
|
2020-01-16 18:31:02 +01:00
|
|
|
var _angular_drag := 0.1
|
2019-12-19 21:24:40 +01:00
|
|
|
|
|
|
|
|
|
|
|
func _ready() -> void:
|
2020-01-16 16:04:27 +01:00
|
|
|
set_physics_process(false)
|
2019-12-19 21:24:40 +01:00
|
|
|
|
|
|
|
|
|
|
|
func _physics_process(delta: float) -> void:
|
2020-01-16 18:31:02 +01:00
|
|
|
_update_agent()
|
|
|
|
|
2019-12-19 21:24:40 +01:00
|
|
|
accel = _orient_behavior.calculate_steering(accel)
|
|
|
|
_angular_velocity += accel.angular
|
|
|
|
|
2020-01-16 18:31:02 +01:00
|
|
|
_angular_velocity = clamp(
|
|
|
|
lerp(_angular_velocity, 0, _angular_drag),
|
|
|
|
-agent.max_angular_speed,
|
|
|
|
agent.max_angular_speed
|
|
|
|
)
|
2019-12-19 21:24:40 +01:00
|
|
|
|
2020-01-16 16:04:27 +01:00
|
|
|
rotation += _angular_velocity * delta
|
2019-12-19 21:24:40 +01:00
|
|
|
|
|
|
|
accel = _behavior.calculate_steering(accel)
|
2020-01-15 20:15:31 +01:00
|
|
|
_linear_velocity += Vector2(accel.linear.x, accel.linear.y)
|
|
|
|
_linear_velocity = _linear_velocity.linear_interpolate(Vector2.ZERO, _linear_drag_coefficient)
|
2019-12-21 21:20:06 +01:00
|
|
|
_linear_velocity = _linear_velocity.clamped(agent.max_linear_speed)
|
2019-12-19 21:24:40 +01:00
|
|
|
_linear_velocity = move_and_slide(_linear_velocity)
|
|
|
|
|
|
|
|
|
2020-01-16 16:04:27 +01:00
|
|
|
func setup(predict_time: float, max_linear_speed: float, max_linear_accel: float) -> void:
|
|
|
|
if use_seek:
|
|
|
|
_behavior = GSTSeek.new(agent, player_agent)
|
|
|
|
else:
|
|
|
|
_behavior = GSTPursue.new(agent, player_agent, predict_time)
|
|
|
|
|
|
|
|
_orient_behavior = GSTLookWhereYouGo.new(agent)
|
|
|
|
_orient_behavior.alignment_tolerance = 0.001
|
|
|
|
_orient_behavior.deceleration_radius = PI/2
|
|
|
|
|
|
|
|
agent.max_angular_acceleration = 2
|
|
|
|
agent.max_angular_speed = 5
|
|
|
|
agent.max_linear_acceleration = max_linear_accel
|
|
|
|
agent.max_linear_speed = max_linear_speed
|
|
|
|
|
|
|
|
_update_agent()
|
|
|
|
set_physics_process(true)
|
|
|
|
|
|
|
|
|
2019-12-19 21:24:40 +01:00
|
|
|
func _update_agent() -> void:
|
|
|
|
agent.position.x = global_position.x
|
|
|
|
agent.position.y = global_position.y
|
|
|
|
agent.orientation = rotation
|
|
|
|
agent.linear_velocity.x = _linear_velocity.x
|
|
|
|
agent.linear_velocity.y = _linear_velocity.y
|
|
|
|
agent.angular_velocity = _angular_velocity
|