Here's another way to do it with tweens. Since the tweens are executed in series, you don't have to do much state tracking.
# game.gd
extends Node2D
var player
func _input(event: InputEvent) -> void:
if event is InputEventMouseButton:
if event.pressed:
var pos = get_global_mouse_position()
player.new_target(pos)
func _ready() -> void:
player = Tank.new()
player.position = Vector2(200, 600)
player.rotation = PI
add_child(player)
# tank.gd
extends Sprite
class_name Tank
var target
var twn:SceneTreeTween
func _ready() -> void:
texture = preload('res://art/tank_blue.png')
rotation = PI
centered = true
func limit_ang(ang):
if ang > 2 * PI:
ang -= 2 * PI
elif ang < 0:
ang += 2 * PI
return ang
func new_target(tar):
target = tar
rotation = limit_ang(rotation)
var ang = target.angle_to_point(position)
ang -= PI / 2.0
ang = limit_ang(ang)
if abs(ang + 2 * PI - rotation) < abs(ang - rotation):
ang += 2 * PI
if abs(ang - (rotation + 2 * PI)) < abs(ang - rotation):
ang -= 2 * PI
if twn:
twn.stop()
twn = get_tree().create_tween()
twn.tween_property(self, 'rotation', ang, abs(ang - rotation) / 2.0)
twn.tween_property(self, 'position', target, position.distance_to(target) / 150.0)