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

public class CameraC : MonoBehaviour
{
    [SerializeField] private Transform mainCharacter = null;
    [SerializeField] private float deadZone = 1f;
    [SerializeField] private float speed = 1f;
    [SerializeField] private float ingameY = 3;
    [SerializeField] private float exitY = 12;


    private float targetY = 0;
    private float targetDeadZone = 0;
    // Start is called before the first frame update
    void Start()
    {
        this.targetY = this.ingameY;
    }

    // Update is called once per frame
    void Update()
    {
        float deltaX = (this.transform.position.x - this.mainCharacter.position.x);
        float deltaX2 = deltaX * deltaX;

        if (deltaX2 < this.targetDeadZone && 0 < deltaX2)
        {
            return;
        }

        Vector3 position = this.transform.position;
        position.x = Mathf.Lerp(position.x, this.mainCharacter.position.x, Time.deltaTime * this.speed);
        position.y = Mathf.Lerp(position.y, this.targetY, Time.deltaTime * this.speed);
        this.transform.position = position;

        if ((this.targetY == this.exitY) && (this.exitY < (position.y + 0.01f)))
        {
            SceneManager.LoadScene("MenuScene");
        }
    }

    public void Reset()
    {
        this.targetY = this.exitY;
        this.targetDeadZone = -1;
    }
}
