using UnityEngine;
using System.Collections;

public class ExplodeBottle : MonoBehaviour
{
    public PuddleBit PuddleBit;
    public int Nb = 10;
    public float MaxForce = 20;
    public float MaxVelocity = 2;

    public void Update()
    {
    }

    public void Explode()
    {
        //Debug.LogFormat("Explode {0} {1}\n", Nb, PuddleBit.name);

        Vector2 initialVelocity = gameObject.GetComponent<Rigidbody2D>().velocity;
        if (initialVelocity.magnitude > MaxVelocity)
        {
            initialVelocity *= MaxVelocity / initialVelocity.magnitude;
        }            
        
        for (int i = 0; i < Nb; i++)
        {
            PuddleBit puddleBit = Instantiate(PuddleBit, transform.position, Quaternion.identity) as PuddleBit;
            Rigidbody2D rb = puddleBit.gameObject.GetComponent<Rigidbody2D>();

            rb.velocity = initialVelocity;

            Vector2 randomForce = new Vector2(Random.Range(-MaxForce, MaxForce), Random.Range(-MaxForce, MaxForce));
            rb.AddForce(randomForce);
        }
        Destroy(gameObject);
    }
}
