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

public class Projectile : MonoBehaviour
{
    public ProjectileData Data;
    public GameEntity Owner { get; set; }
    private Rigidbody _rigidbody;

    public virtual void Start()
    {
        _rigidbody = GetComponent<Rigidbody>();
        _rigidbody.AddForce(transform.forward * Data.Speed, ForceMode.Impulse);

        StartCoroutine(LifeTimeRoutine());
    }

    private IEnumerator LifeTimeRoutine()
    {
        float dieTime = Time.timeSinceLevelLoad + Data.LifeTime;

        while (Time.timeSinceLevelLoad < dieTime)
        {
            yield return null;
        }

        Destroy(gameObject);
    }

    public virtual void OnTriggerEnter(Collider other)
    {
        var player = other.GetComponentInParent<PlayerController>();
        if (player)
        {
            player.Player.Damage(Data.HitDamage, Owner);
            //if (Data.HitEffect)
            //{
            //    var bang = Instantiate(Data.HitEffect, player.transform.position, Quaternion.identity);
            //    Destroy(bang, 2f);
            //}
            Destroy(gameObject);

        }
    }

}
