using Godot;
using System;

public partial class Player : CharacterBody2D
{
	[Export] float Speed;
	[Export] float sugar_speed;
	[Export] AnimationPlayer anim;
	[Export] Camera2D cam;
	[Export] TextureProgressBar bar;
	[ExportGroup("controls")]
	[Export] Control menu;
	[Export] PackedScene principal;
	


	public float PixelSize = 1.0f;

	float sugar { get; set;}= 50;
	const float MAX_SUGAR = 100;

	public override void _PhysicsProcess(double delta)
	{
		Vector2 velocity = Velocity;

		sugar -= (float)delta / sugar_speed;
		bar.Value = sugar;

		if(sugar <= 0)
		{

		}
		else if (sugar >= 100)
		{

		}

	
		Vector2 direction = Input.GetVector("left", "right", "up", "down");
		direction = direction.Normalized();
		if (direction != Vector2.Zero)
		{
			velocity = direction * Speed * (float)delta;
		}
		else
		{
			velocity.X = Mathf.MoveToward(Velocity.X, 0, Speed);
			velocity.Y = Mathf.MoveToward(Velocity.Y, 0, Speed);

			velocity = velocity.Round();
		}
		
		
		if(Input.IsActionPressed("quit"))
		{
			Input.MouseMode = Input.MouseModeEnum.Visible;
			anim.Play("paused");
			GetTree().Paused= true;
		}
		else if(velocity.X > 0)
		{
			anim.Play("right");
		}
		else if (velocity.X < 0)
		{
			anim.Play("left");
		}
		else if (velocity.Y > 0)
		{
			anim.Play("down");
		}
		else if (velocity.Y < 0)
		{
			anim.Play("up");
		}
		else
		{
			anim.Play("idle");
			
		}
		

		Velocity = velocity;
		MoveAndSlide();

		Position = Position.Round();
	}
}
