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

public class LDBackgroundStar : MonoBehaviour
{
    public float ParallaxLevel;
    private GameManager theGame;
    private Vector2 vOldCamPos;
    private Vector2 vGamePos;

	// Use this for initialization
	void Start () 
    {
	    theGame = GameManager.GetGame();
        vOldCamPos = theGame.theCamera.GetWorldPosition();
    }

    public void Init(Vector2 gamePos, float parallax)
    {
        vGamePos = gamePos;
        ParallaxLevel = parallax;
    }
	
	// Update is called once per frame
	void Update ()
	{
	    Vector2 vCamNewPos = theGame.theCamera.GetWorldPosition();
	    if (vCamNewPos != vOldCamPos )
	    {
            //the camera moved this way
	        Vector2 vDelta = vCamNewPos - vOldCamPos;
            vOldCamPos = vCamNewPos;

            //so we move the other way
	        vDelta *= -1.0f;

            //based on our parallax level
	        vDelta /= ParallaxLevel;

	        vGamePos += vDelta;


	    }
        Vector3 vNewTransformPos = theGame.GetScreenspaceVectorFromGamePosition(vGamePos);
	    vNewTransformPos.z = 10;
	    transform.position = vNewTransformPos;
	}
}
