using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Projectile : MonoBehaviour {

    public float velocity;

    public float damage;

    public float knockback;

    public GameObject hitEffect;

    Rigidbody rb;

	// Use this for initialization
	void Start () {
        rb = GetComponent<Rigidbody>();
        rb.AddForce(velocity * transform.forward, ForceMode.VelocityChange);
        GameObject.Destroy(gameObject, 5);
	}
	
	// Update is called once per frame
	void Update () {
		
	}

    private void OnTriggerEnter(Collider other)
    {
        if (gameObject == null)
            return;

        Monster monster = other.gameObject.GetComponentInParent<Monster>();
        if (monster != null)
        {
            if (monster.alive)
            {
                monster.TakeDamage(damage);
                if (knockback > 0)
                {
                    monster.Knockback(rb.velocity.normalized * knockback);
                }
            }
            else
                return;
        }
        Player player = other.gameObject.GetComponentInParent<Player>();
        if (player != null)
        {
            player.TakeDamage(damage);
        }

        if (hitEffect != null)
        {
            GameObject effect = GameObject.Instantiate(hitEffect, transform.position, Quaternion.identity);
            GameObject.Destroy(effect, 1);
        }

        GameObject.Destroy(gameObject);
    }
}
