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
|
|
|
|
|
|
|
|
|
|
|
export var use_seek: bool = false
|
|
|
|
|
2020-02-11 18:33:25 +01:00
|
|
|
var _blend: GSAIBlend
|
2019-12-19 21:24:40 +01:00
|
|
|
|
2020-01-16 09:44:44 +01:00
|
|
|
var _linear_drag_coefficient := 0.025
|
2020-01-16 18:31:02 +01:00
|
|
|
var _angular_drag := 0.1
|
2020-02-11 18:33:25 +01:00
|
|
|
var _direction_face := GSAIAgentLocation.new()
|
2019-12-19 21:24:40 +01:00
|
|
|
|
2020-02-11 18:33:25 +01:00
|
|
|
onready var agent := GSAIKinematicBody2DAgent.new(self)
|
|
|
|
onready var accel := GSAITargetAcceleration.new()
|
|
|
|
onready var player_agent: GSAISteeringAgent = owner.find_node("Player", true, false).agent
|
2020-01-22 17:55:49 +01:00
|
|
|
|
2019-12-19 21:24:40 +01:00
|
|
|
|
|
|
|
func _ready() -> void:
|
2020-02-06 20:54:12 +01:00
|
|
|
agent.calculate_velocities = false
|
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-29 16:04:47 +01:00
|
|
|
_direction_face.position = agent.position + accel.linear.normalized()
|
|
|
|
|
2020-02-06 20:54:12 +01:00
|
|
|
_blend.calculate_steering(accel)
|
|
|
|
|
|
|
|
agent.angular_velocity = clamp(
|
|
|
|
agent.angular_velocity + accel.angular,
|
2020-01-22 17:55:49 +01:00
|
|
|
-agent.angular_speed_max,
|
|
|
|
agent.angular_speed_max
|
2020-01-16 18:31:02 +01:00
|
|
|
)
|
2020-02-06 20:54:12 +01:00
|
|
|
agent.angular_velocity = lerp(agent.angular_velocity, 0, _angular_drag)
|
|
|
|
|
|
|
|
rotation += agent.angular_velocity * delta
|
2019-12-19 21:24:40 +01:00
|
|
|
|
2020-02-06 20:54:12 +01:00
|
|
|
var linear_velocity := (
|
2020-02-11 18:33:25 +01:00
|
|
|
GSAIUtils.to_vector2(agent.linear_velocity) +
|
|
|
|
(GSAIUtils.angle_to_vector2(rotation) * -agent.linear_acceleration_max)
|
2020-02-06 20:54:12 +01:00
|
|
|
)
|
|
|
|
linear_velocity = linear_velocity.clamped(agent.linear_speed_max)
|
|
|
|
linear_velocity = linear_velocity.linear_interpolate(
|
|
|
|
Vector2.ZERO,
|
|
|
|
_linear_drag_coefficient
|
|
|
|
)
|
2019-12-19 21:24:40 +01:00
|
|
|
|
2020-02-06 20:54:12 +01:00
|
|
|
linear_velocity = move_and_slide(linear_velocity)
|
2020-02-11 18:33:25 +01:00
|
|
|
agent.linear_velocity = GSAIUtils.to_vector3(linear_velocity)
|
2019-12-19 21:24:40 +01:00
|
|
|
|
|
|
|
|
2020-01-22 17:55:49 +01:00
|
|
|
func setup(predict_time: float, linear_speed_max: float, linear_accel_max: float) -> void:
|
2020-02-11 18:33:25 +01:00
|
|
|
var behavior: GSAISteeringBehavior
|
2020-01-16 16:04:27 +01:00
|
|
|
if use_seek:
|
2020-02-11 18:33:25 +01:00
|
|
|
behavior = GSAISeek.new(agent, player_agent)
|
2020-01-16 16:04:27 +01:00
|
|
|
else:
|
2020-02-11 18:33:25 +01:00
|
|
|
behavior = GSAIPursue.new(agent, player_agent, predict_time)
|
2020-02-06 20:54:12 +01:00
|
|
|
|
2020-02-11 18:33:25 +01:00
|
|
|
var orient_behavior := GSAIFace.new(agent, _direction_face)
|
2020-02-06 20:54:12 +01:00
|
|
|
orient_behavior.alignment_tolerance = deg2rad(5)
|
|
|
|
orient_behavior.deceleration_radius = deg2rad(5)
|
2020-01-16 16:04:27 +01:00
|
|
|
|
2020-02-11 18:33:25 +01:00
|
|
|
_blend = GSAIBlend.new(agent)
|
2020-02-06 20:54:12 +01:00
|
|
|
_blend.add(behavior, 1)
|
|
|
|
_blend.add(orient_behavior, 1)
|
2020-01-16 16:04:27 +01:00
|
|
|
|
2020-01-29 16:04:47 +01:00
|
|
|
agent.angular_acceleration_max = deg2rad(40)
|
|
|
|
agent.angular_speed_max = deg2rad(90)
|
2020-01-22 17:55:49 +01:00
|
|
|
agent.linear_acceleration_max = linear_accel_max
|
|
|
|
agent.linear_speed_max = linear_speed_max
|
2020-01-16 16:04:27 +01:00
|
|
|
|
|
|
|
set_physics_process(true)
|