extends Sprite

onready var tank    = get_node("/root/Tank")
onready var spawner = get_node("/root/Tank/HazardSpawner")
var playingforwards = true


func _ready() -> void:
	_reposition()
#	_rewind()
#	print("rewinding")
#	yield($AnimationPlayer, "animation_finished")
#	print("rewound")
	$Timer.start()


func _reposition() -> void:
	var tween_duration = 0.5
	$Tween.interpolate_property(self, "position:x", position.x, rand_range(400, 2000), tween_duration, Tween.TRANS_BACK, Tween.EASE_IN_OUT)
	$Tween.start()
#	yield($Tween, "finished")


func _on_Timer_timeout() -> void:
	_stab()


func _stab() -> void:
	playingforwards = true
	$AnimationPlayer.play("Stab")


func _rewind() -> void:
	if playingforwards:
		playingforwards = false
		$AnimationPlayer.play_backwards("Stab")
	#yield($AnimationPlayer, "animation_finished")
	#_reposition()


#static func random_position(area: CollisionShape2D) -> Vector2:
#	var _topleft  : Vector2 = area.global_position - area.shape.extents
#	var _botright : Vector2 = area.global_position + area.shape.extents
#	var _x = rand_range(_topleft.x, _botright.x)
#	var _y = rand_range(_topleft.y, _botright.y)
#	return Vector2(_x, _y)
