using System.Collections.Generic;
using UnityEngine;

public class RandomRotationAnimation : MonoBehaviour
{
    public float rotationRange = 30f;        // Maximum random rotation angle in degrees
    public float rotationDuration = 1.0f;    // Duration for each rotation
    public LeanTweenType easingType = LeanTweenType.easeInOutBack; // Easing for the animation

    private List<GameObject> pieces = new List<GameObject>(); // List to store all child pieces

    void Start()
    {
        // Find all child pieces in the parent object
        foreach (Transform child in transform)
        {
            pieces.Add(child.gameObject);
        }

        // Start the random rotation animation
        AnimatePieces();
    }

    void AnimatePieces()
    {
        foreach (GameObject piece in pieces)
        {
            // Store the original rotation of the piece
            Quaternion originalRotation = piece.transform.rotation;

            // Calculate a random rotation angle within the specified range
            float randomAngle = Random.Range(-rotationRange, rotationRange);
            
            // Rotate the piece to the random angle
            LeanTween.rotateZ(piece, randomAngle, rotationDuration / 2)
                .setEase(easingType)
                .setOnComplete(() =>
                {
                    // Rotate back to the original rotation
                    LeanTween.rotate(piece, originalRotation.eulerAngles, rotationDuration / 2)
                        .setEase(easingType);
                });
        }
    }
}