class_name NumberChaser
extends Node

@export var StringFormat : String = ""
@export var ModifiedLabel : Label = null

var DisplayedValue : int = 0
var Value : int = 0
var UpdatePeriod : float = 0.2
var UpdateDelay : float = 0.0

# Called when the node enters the scene tree for the first time.
func _ready():
	if ModifiedLabel == null:
		ModifiedLabel = get_parent()
	UpdateValue()

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	if (DisplayedValue != Value):
		if UpdateDelay < 0:
			UpdateValue()
			UpdateDelay = UpdatePeriod
		else:
			UpdateDelay -= delta
	
func UpdateValue():
	var difference := Value - DisplayedValue
	if (absi(difference) < 3):
		DisplayedValue = Value
	else:
		DisplayedValue += (difference / 2)

	if StringFormat.length() > 0:
		ModifiedLabel.text = StringFormat % DisplayedValue
	else:
		ModifiedLabel.text = str(DisplayedValue)
	
