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

public class CameraFollow : MonoBehaviour
{
	public float xPositionFreedom = 10.0f;

    void Awake()
    {
        EventManager.SubscribePlayerMoved(TrackPlayerMovement);
    }

    void TrackPlayerMovement(float xPosition)
    {
    	float xOffset = xPosition - transform.position.x;
    	float newX = 0.0f;

    	if (System.Math.Abs(xOffset) > xPositionFreedom) {
    		if (xOffset > 0) {
    			newX = xPosition - xPositionFreedom;		
    		} else {
    			newX = xPosition + xPositionFreedom;
    		}
    		transform.position = new Vector3(newX, transform.position.y, transform.position.z);		
    		EventManager.TriggerCameraMoved(transform.position.x);
    	}
    }


}
