51 lines
1.8 KiB
GDScript
51 lines
1.8 KiB
GDScript
extends State
|
|
|
|
## This amount of force will be applied if target can be stabbed
|
|
@export var additional_stab_force: float = 200.0
|
|
## If target is further than this distance, transit to WanderingState
|
|
@export var target_loss_distance: float = 1536.0
|
|
## Shortcut to get_parent().get_parent()
|
|
@onready var ship = get_parent().get_parent()
|
|
## Node which is being targeted by this ship
|
|
var target: Node2D
|
|
## Ship will strive to achieve this angle
|
|
var destination_degrees: float = 0:
|
|
set(value):
|
|
if value > 180:
|
|
destination_degrees = value - 360
|
|
elif value < -180:
|
|
destination_degrees = 360 + value
|
|
else:
|
|
destination_degrees = value
|
|
## Delta to destination_degrees
|
|
var destination_difference: float = 5.0
|
|
|
|
var current_destination_difference: float:
|
|
set(value):
|
|
if value > 180:
|
|
current_destination_difference = value - 360
|
|
else:
|
|
current_destination_difference = value
|
|
|
|
func enter(message: Dictionary):
|
|
if message.has("target"):
|
|
target = message["target"]
|
|
else:
|
|
get_parent().transit("WanderingState")
|
|
|
|
func process(_delta):
|
|
# checking if need to apply torque
|
|
destination_degrees = rad_to_deg(ship.global_position.angle_to_point(target.global_position))
|
|
current_destination_difference = destination_degrees - ship.hull.global_rotation_degrees
|
|
|
|
if abs(current_destination_difference) > destination_difference:
|
|
ship.engine.rotation_axis = clamp(current_destination_difference / 360 + 0.5 * sign(current_destination_difference), -1.0, 1.0)
|
|
else:
|
|
ship.engine.rotation_axis = 0.0
|
|
ship.hull.apply_central_force(Vector2.from_angle(ship.hull.rotation) * additional_stab_force)
|
|
# making ship always accelerate
|
|
ship.engine.acceleration_axis = 1.0
|
|
|
|
var distance_to_target = ship.global_position.distance_to(target.global_position)
|
|
if distance_to_target > target_loss_distance:
|
|
get_parent().transit("WanderingState")
|