using UnityEngine;

public class Projectile : MonoBehaviour{

    Vector3 p0;
    Vector3 p1;
    Vector3 p2;
    float speed = 1;
    int senderID = -1;
    int damage = 20;
    Element element;

    float time = 0;

    public void init(Vector3 p0, Vector3 p1, Vector3 p2, Element element, float speed, int senderID){
        this.p0 = p0;
        this.p1 = p1;
        this.p2 = p2;
        this.element = element;
        this.speed = speed;
        this.senderID = senderID;
    }

    Vector3 projectileTrajectory(float t){
        return Mathf.Pow((1-t),2) * p0 + 2 * t * (1-t) * p1 + Mathf.Pow(t, 2) * p2;
    }

    void Update() {
        if(time <= 1){
            time += Time.deltaTime * speed;
            transform.position = projectileTrajectory(time);
        }else{
            Destroy(gameObject);
        }

    }

    void OnTriggerEnter(Collider other) {
        PlayerController pc = other.gameObject.GetComponent<PlayerController>();
        if(pc != null && pc.id != senderID){
            pc.takeDamage(damage);
        }
    }

    public Element getElement(){
        return element;
    }

}
