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

public class CameraScript : MonoBehaviour {

    public static CameraScript instance;

    public static float offset;

    public float speed = 1f;
    private float acceleration = 0.2f;
    public float maxspeed = 1.5f;
    
	void Start () {
        MakeInstance();
    }
	
	void Update () {
        MoveCamera();

        if (this.gameObject.transform.position.x == 766.7f) {
            Debug.Log("Camera Stop!");
            speed = 0;
            maxspeed = 0;
            PlayerMovement.instance.speed = 1f;
        }
	}

    void MakeInstance() {
        if (instance == null) {
            instance = this;
        }
    }

    void MoveCamera() {
        Vector3 temp = transform.position;
        float oldX = temp.x;
        float newX = temp.x + speed * Time.deltaTime;
        
        temp.x = Mathf.Clamp(temp.x, newX, oldX);
        transform.position = temp;
        speed += acceleration * Time.deltaTime;

        if (speed > maxspeed) {
            speed = maxspeed;
        }
        
        //temp.x = PlayerMovement.instance.GetPositionX() + offset;
        //transform.position = temp;
    }
}
