2020-01-16 16:04:27 +01:00
|
|
|
extends KinematicBody2D
|
2020-01-02 23:42:41 +01:00
|
|
|
# Controls the player ship's movements based on player input.
|
2019-12-19 21:24:40 +01:00
|
|
|
|
|
|
|
|
2020-01-16 09:44:44 +01:00
|
|
|
onready var agent := GSTSteeringAgent.new()
|
2019-12-19 21:24:40 +01:00
|
|
|
|
2020-01-16 09:44:44 +01:00
|
|
|
export var thruster_strength := 250.0
|
|
|
|
export var side_thruster_strength := 10.0
|
|
|
|
export var max_velocity := 300.0
|
|
|
|
export var max_angular_velocity := 2.0
|
|
|
|
export var angular_drag := 0.025
|
|
|
|
export var linear_drag := 0.025
|
2019-12-19 21:24:40 +01:00
|
|
|
|
2020-01-16 09:44:44 +01:00
|
|
|
var _linear_velocity := Vector2()
|
|
|
|
var _angular_velocity := 0.0
|
2019-12-19 21:24:40 +01:00
|
|
|
|
|
|
|
|
|
|
|
func _physics_process(delta: float) -> void:
|
2020-01-16 09:44:44 +01:00
|
|
|
var movement := _get_movement()
|
2019-12-19 21:24:40 +01:00
|
|
|
_angular_velocity = _calculate_angular_velocity(
|
|
|
|
movement.x,
|
|
|
|
_angular_velocity,
|
|
|
|
side_thruster_strength,
|
|
|
|
max_angular_velocity,
|
|
|
|
angular_drag,
|
|
|
|
delta
|
|
|
|
)
|
2020-01-16 16:04:27 +01:00
|
|
|
rotation += _angular_velocity * delta
|
2019-12-19 21:24:40 +01:00
|
|
|
|
|
|
|
_linear_velocity = _calculate_linear_velocity(
|
|
|
|
movement.y,
|
|
|
|
_linear_velocity,
|
|
|
|
Vector2.UP.rotated(rotation),
|
|
|
|
linear_drag,
|
|
|
|
thruster_strength,
|
|
|
|
max_velocity,
|
|
|
|
delta
|
|
|
|
)
|
|
|
|
|
|
|
|
_linear_velocity = move_and_slide(_linear_velocity)
|
2020-01-16 16:04:27 +01:00
|
|
|
_update_agent()
|
2019-12-19 21:24:40 +01:00
|
|
|
|
|
|
|
|
|
|
|
func _calculate_angular_velocity(
|
2020-01-16 16:04:27 +01:00
|
|
|
horizontal_movement: float,
|
|
|
|
current_velocity: float,
|
|
|
|
thruster_strength: float,
|
|
|
|
max_velocity: float,
|
|
|
|
ship_drag: float,
|
|
|
|
delta: float
|
|
|
|
) -> float:
|
2020-01-16 09:44:44 +01:00
|
|
|
var velocity := clamp(
|
2019-12-19 21:24:40 +01:00
|
|
|
current_velocity + thruster_strength * horizontal_movement * delta,
|
|
|
|
-max_velocity,
|
|
|
|
max_velocity
|
|
|
|
)
|
|
|
|
|
2020-01-15 20:15:31 +01:00
|
|
|
velocity = lerp(velocity, 0, ship_drag)
|
2019-12-19 21:24:40 +01:00
|
|
|
|
|
|
|
return velocity
|
|
|
|
|
|
|
|
|
|
|
|
func _calculate_linear_velocity(
|
2020-01-16 16:04:27 +01:00
|
|
|
vertical_movement: float,
|
|
|
|
current_velocity: Vector2,
|
|
|
|
facing_direction: Vector2,
|
|
|
|
ship_drag_coefficient: float,
|
|
|
|
strength: float,
|
|
|
|
max_speed: float,
|
|
|
|
delta: float
|
|
|
|
) -> Vector2:
|
2020-01-16 09:44:44 +01:00
|
|
|
var actual_strength := 0.0
|
2019-12-19 21:24:40 +01:00
|
|
|
if vertical_movement > 0:
|
|
|
|
actual_strength = strength
|
|
|
|
elif vertical_movement < 0:
|
|
|
|
actual_strength = -strength/1.5
|
|
|
|
|
2020-01-16 09:44:44 +01:00
|
|
|
var velocity := current_velocity + facing_direction * actual_strength * delta
|
2020-01-15 20:15:31 +01:00
|
|
|
velocity = velocity.linear_interpolate(Vector2.ZERO, ship_drag_coefficient)
|
2019-12-19 21:24:40 +01:00
|
|
|
|
|
|
|
return velocity.clamped(max_speed)
|
|
|
|
|
|
|
|
|
|
|
|
func _get_movement() -> Vector2:
|
|
|
|
return Vector2( Input.get_action_strength("sf_right") - Input.get_action_strength("sf_left"),
|
|
|
|
Input.get_action_strength("sf_up") - Input.get_action_strength("sf_down"))
|
|
|
|
|
|
|
|
|
2020-01-16 16:04:27 +01:00
|
|
|
func _update_agent() -> void:
|
2020-01-13 22:15:44 +01:00
|
|
|
agent.position.x = global_position.x
|
|
|
|
agent.position.y = global_position.y
|
2020-01-16 16:04:27 +01:00
|
|
|
agent.linear_velocity.x = _linear_velocity.x
|
|
|
|
agent.linear_velocity.y = _linear_velocity.y
|
2020-01-13 22:15:44 +01:00
|
|
|
agent.angular_velocity = _angular_velocity
|
2020-01-16 16:04:27 +01:00
|
|
|
agent.orientation = rotation
|