extends Node

var hero: Node3D
var file: FileAccess
var lastS: int = 0
var done: bool = false

func _ready():
	hero = get_node("/root/main/hero")
	file = FileAccess.open("user://stats-%s.txt" % Time.get_date_string_from_system(), FileAccess.WRITE)
	Signals.connect("TShapeTaskStarted", TShapeTaskStarted)
	Signals.connect("TShapeTaskCompleted",TShapeTaskCompleted )
	Signals.connect("FinishPassed", FinishPassed)
	Signals.connect("MoneyCollected", MoneyCollected)

func _process(_delta):
	if (done):
		return
	if (Stopwatch.isRunning()):
		var s = Stopwatch.getSeconds()
		if (s != lastS):
			lastS = s
			file.store_string("move %d %f %f\n" % [s, hero.global_position.x, hero.global_position.y])
	
func TShapeTaskStarted():
	file.store_string("TShapeTaskStarted %d\n" % Stopwatch.getSeconds())

func TShapeTaskCompleted():
	file.store_string("TShapeTaskCompleted %d\n" % Stopwatch.getSeconds())

func FinishPassed():
	done = true
	file.close()

func MoneyCollected(_amount: int):
		file.store_string("coin %d %f %f\n" % [Stopwatch.getSeconds(), hero.global_position.x, hero.global_position.y])