60 lines
1.9 KiB
GDScript
60 lines
1.9 KiB
GDScript
extends State
|
|
|
|
## If target is further than this distance, transit to WanderingState
|
|
@export var target_loss_distance: float = 1536.0
|
|
## Node which is being targeted by this ship
|
|
var target: Node2D
|
|
## Shortcut to get_parent().get_parent()
|
|
@onready var ship = get_parent().get_parent()
|
|
## 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
|
|
|
|
var current_destination_difference: float:
|
|
set(value):
|
|
if value > 180:
|
|
current_destination_difference = value - 360
|
|
else:
|
|
current_destination_difference = value
|
|
|
|
## Gun node to change its behavior
|
|
var gun: Node2D
|
|
|
|
func enter(message: Dictionary):
|
|
if message.has("target"):
|
|
target = message["target"]
|
|
gun = ship.weapons.list[0]
|
|
destination_difference = gun.max_gun_rotation / 2
|
|
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)
|
|
gun.shoot_request = 0.0
|
|
else:
|
|
ship.engine.rotation_axis = 0.0
|
|
gun.shoot_request = 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")
|
|
gun.gun_rotation = 0.0
|
|
gun.shoot_request = 0.0
|
|
return
|
|
|
|
ship.engine.acceleration_axis = clamp((distance_to_target - 256) / 256 + 0.3, -1.0, 1.0)
|
|
gun.gun_rotation = current_destination_difference
|
|
|