using UnityEngine;
using System.Collections;

public class FoodProjectile : MonoBehaviour 
{
    public Rigidbody myRigidbody;
    public GameObject splatObject;
    public float splatSpeed = 1f;
    public AudioClip hitSound;
    
    void OnCollisionEnter(Collision collision)
    {
        //if (myRigidbody.velocity.sqrMagnitude > 0f)
        if (gameObject.layer == Layers.Projectile)
        {
            if (collision.relativeVelocity.sqrMagnitude > splatSpeed * splatSpeed)
            {
                OneShot3DAudio.PlayClipAtPoint(hitSound, transform.position);
                collision.transform.gameObject.SendMessageUpwards("Splatted", SendMessageOptions.DontRequireReceiver);

                if (collision.transform.gameObject.layer != Layers.Player)
                {
                    var rotation = Quaternion.FromToRotation(Vector3.forward, collision.contacts[0].normal);
                    var splat = Instantiate(splatObject, collision.contacts[0].point, rotation) as GameObject;
                    splat.transform.SetParent(collision.transform);
                }

                Destroy(gameObject);
            }
        }
    }
}
