extends CharacterBody2D

var friction = 8.0 # Lower values make the slowdown more gradual
var is_collected = false
var bounceTimer = 0.0
var timeBeforePickup = 0.8

# Z-axis variables
var positionZ = 4.0 # Start slightly in the air
var velocityZ = 10.0 # Initial upward velocity
var accelerationZ = -300.0 # Gravity
var bounceRestitution = 0.6 # How much bounce energy is retained (0-1)
var minBounceVelocity = 40.0 # Minimum velocity needed to bounce
var bloodTime = 2.0
func _process(delta: float) -> void:
	# Update vertical movement
	velocityZ += accelerationZ * delta
	positionZ += velocityZ * delta
	
	# Ground collision and bounce
	if positionZ <= 0:
		velocity = velocity.lerp(Vector2.ZERO, 1.0 - exp(-friction * delta)) # only slow down if on floor
		positionZ = 0
		velocityZ = 0
		if bloodTime <= 0.0:
			var fade_tween = create_tween()
			fade_tween.set_trans(Tween.TRANS_CUBIC)
			fade_tween.set_ease(Tween.EASE_OUT)
			fade_tween.tween_property($Sprite2D, "modulate:a", 0.0, 0.5)
			await fade_tween.finished
			call_deferred("queue_free")
		bloodTime -= delta
		
	
	update_sprite_scale()
	move_and_slide()
	
func update_sprite_scale() -> void:
	# Calculate scale based on height
	# Maximum height will have scale 1.3, ground will have scale 1.0
	var height_factor = positionZ / 50.0 # Assuming 20 is max height
	var posY = 0.0 + (2 * positionZ)
	var sc = 1.0 + (0.8 * height_factor)
	$Sprite2D.scale = Vector2(sc, sc)
	$Sprite2D.position.y = -posY
