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

public class Bullet : MonoBehaviour
{
    PlayerController shooter;
    float shootForce = 8.5f;
    int damage = 1;

    Vector3 direction;
    bool isActivated = false;

    private void OnBecameInvisible()
    {
        Destroy(gameObject);
    }

    public void InitBullet(PlayerController player, Vector3 dir)
    {
        shooter = player;
        direction = new Vector3(dir.x, 0, -dir.y);

        isActivated = true;
    }

    // Update is called once per frame
    void Update()
    {
        if(isActivated == true)
        {
            transform.position += direction * Time.deltaTime * shootForce;
        }
       
    }

    private void OnTriggerEnter(Collider other)
    {
        if(other.gameObject.GetComponent<PlayerController>())
        {
            PlayerController player = other.gameObject.GetComponent<PlayerController>();

            if(player != shooter)
            {
                other.gameObject.GetComponent<ITarget>().OnHit(damage);
                Destroy(gameObject);
            }

        }
    }
}
