using UnityEngine;

public class ExplodeCharacter : MonoBehaviour
{
    // Use this for initialization
    void Start()
    {
        Rigidbody2D[] rigidbodies = transform.GetComponentsInChildren<Rigidbody2D>();
        foreach (Rigidbody2D child in rigidbodies)
        {
            //			AddExplosionForce(child, 300.0f, transform.position - new Vector3(10.0f, 10.0f, 0.0f), 300.0f);
            AddExplosionForce(child, 100.0f, transform.position, 300.0f);
        }
    }

    public void SetVelocity(Vector2 velocity)
    {
        Rigidbody2D[] rigidbodies = transform.GetComponentsInChildren<Rigidbody2D>();
        foreach (Rigidbody2D child in rigidbodies)
        {
            child.velocity = new Vector2(velocity.x / 2.0f, velocity.y / 2.0f);
        }
    }

    public void AddExplosionForce(Rigidbody2D body, float explosionForce, Vector3 explosionPosition, float explosionRadius)
    {
        var dir = (body.transform.position - explosionPosition);
        float wearoff = 1 - (dir.magnitude / explosionRadius);
        body.AddForce(dir.normalized * explosionForce * wearoff);
    }
}
