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

public class ThrowPrefect : MonoBehaviour
{
    public Transform targetPos;

    public Vector3 initPos;

    private float timer = 0;
   public float cd = 1;

    private float vx = 0f;
    private float vy = 0f;
    private float speedX = 0f;
    private float speedY = 0f;
    
    private void Awake()
    {
        initPos = transform.position;
    }
    // Start is called before the first frame update
    void Start()
    {
        cd = 1f;
        vx = TotalOffsetX() / cd;

        vy = GetVy();
    }

    private float GetVy()
    {
        float s = TotalOffsetY();
        float p2 = cd * cd * (-4.9f);
        float res = (s - p2) / cd;
        return res;
    }
    
    private float TotalOffsetX()
    {
        return targetPos.position.x-initPos.x;
    }
    
    private float TotalOffsetY()
    {
        return targetPos.position.y-initPos.y;
    }


    float getSpeed(float v0, float a, float t)
    {
        return v0  + a * t;
    }

    // Update is called once per frame
    void Update()
    {
        if (timer < cd)
        {
            timer += Time.deltaTime;
            float moveX = vx * timer;
            float moveY =vy*timer  - 4.9f * timer * timer;
            speedX = getSpeed(vx, 0, timer);
            speedY = getSpeed(vy, -9.8f, timer);
            transform.position = new Vector3(initPos.x + moveX, initPos.y + moveY, initPos.z);
           // print(Time.time);
          // print("x : y "+speedX +":"+speedY);
        }
    }

    public float GetArrowRotate()
    {
        float r = Mathf.Atan2(this.speedY, this.speedX);
        print("r"+r);
        return r*Mathf.Rad2Deg;
    }
}
