using Godot;
using Newlon.Components.Level;
using Newlon.Components.Zombies;

namespace Newlon.Components;

[GlobalClass]
public partial class FallParticle : RigidBody2D
{
	[Export] private RuntimeZombieData data;
	[Export] private float minAngle;
	[Export] private float maxAngle;
	[Export] private float minTorque;
	[Export] private float maxTorque;
	[Export] private float Impulse;
	public void FallOff()
	{
		SetDeferred("freeze", false);
		foreach (var floor in FallFloor.GetEverythingElse(FallFloor.GetNearest(data.GlobalPosition)))
		{
			AddCollisionExceptionWith(floor);
		}
		Callable.From(() =>
		{
			Reparent(PoolContainer.Instance.Particles);
			float rng_angle = Mathf.DegToRad((float)GD.RandRange(minAngle, maxAngle));
			float rng_torque = Mathf.DegToRad((float)GD.RandRange(minTorque, maxTorque));
			if (Impulse != 0)
				ApplyImpulse(new Vector2(Mathf.Sin(rng_angle) * Impulse, Mathf.Cos(rng_angle) * Impulse));
			ApplyTorqueImpulse(rng_torque);
		}).CallDeferred();
		var tween = CreateTween();
		tween.TweenInterval(4.0);
		tween.TweenProperty(this, "modulate", new Color(Modulate.R, Modulate.G, Modulate.B, 0f), 1.0);
		tween.TweenCallback(Callable.From(QueueFree));
	}
}
