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

public class Candy : MonoBehaviour
{
    public float minX = -20, maxX = 20, minZ = -10, maxZ = 20;
    Vector3 targetPos;
    public float height, moveSpeed;
    
    void Start()
    {
        Vector3 startPos = transform.position;
        targetPos = new Vector3(Random.Range(minX, maxX), transform.position.y, Random.Range(minZ, maxZ));
        StartCoroutine(Move(startPos, targetPos));
        GameManager.Instance.GetComponent<Tutorial>().candySpawned = true;
    }

    private void Update()
    {
        Quaternion rotTarget = Quaternion.LookRotation(PlayerMovement.Instance.transform.position - transform.position);
        transform.rotation = Quaternion.Euler(0, rotTarget.eulerAngles.y, 0);
    }
    IEnumerator Move(Vector3 origin, Vector3 target)
    {
        float distance = Vector3.Distance(origin, target);
        Vector3 control = origin + (target - origin) / 2 + Vector3.up * height;

        float t = 0.0f;
        while (t <= 1.0f)
        {
            t += Time.deltaTime * (moveSpeed/distance);
            Vector3 m1 = Vector3.LerpUnclamped(origin, control, t);
            Vector3 m2 = Vector3.LerpUnclamped(control, target, t);

            transform.position = Vector3.Lerp(m1, m2, t);
            yield return null;
        }
    }

    private void OnDestroy()
    {

    }
}
