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

public class CameraFollow : MonoBehaviour {

    public GameObject Target;
    private Vector3 Offset = Vector3.zero;
    Vector3 OGposition;

    public bool avgMove = false;
    public bool trkFirst = false;

	void Start () {
        OGposition = transform.position;
        Offset = transform.position - Target.transform.position;
        
    }

	
	void Update () {


        if (Target.active)
        {
            Track1();
        }
        else if(trkFirst)
        {
            trackFirst();
        }
        else
        {
            Track1();
        }

       
        
	}

    // avg the position of the current players

   void Track1()
    {
        transform.localRotation = Quaternion.Euler(0, 30 * (ParticleController.player.speed - ParticleController.player.minSpeed)/ (ParticleController.player.maxSpeed - ParticleController.player.minSpeed), 0);

        transform.position = Target.transform.position + Offset;
    }

    void CameraAllMove()
    {
       ParticleController[] particles = FindObjectsOfType<ParticleController>();

        int ActiveCounter = 0;
        Vector3 TotalVector = Vector3.zero;

        for(int i = 0; i < particles.Length; i++)
        {
            if(particles[i].isActiveAndEnabled)
            {
                TotalVector += particles[i].transform.position;
                ActiveCounter++;
            }
        }

        Vector3 Track2 = TotalVector / (float)ActiveCounter;
        transform.position = 0.9f * transform.position + 0.1f * new Vector3(Track2.x, Track2.y, transform.position.z);

    }

    void trackFirst()
    {
        ParticleController[] particles = FindObjectsOfType<ParticleController>();

        int answerInt = 0;
        Vector3 TotalVector = particles[0].transform.position;

        for (int i = 0; i < particles.Length; i++)
        {
            if (particles[i].transform.position.x > TotalVector.x)
            {
                TotalVector = particles[i].transform.position;
                answerInt = i;
            }
        }
        transform.localRotation = Quaternion.Euler(0, 30 * (particles[answerInt].speed - particles[answerInt].minSpeed) / (particles[answerInt].maxSpeed - particles[answerInt].minSpeed), 0);

        transform.position =  new Vector3(1f * TotalVector.x + 0f * transform.position.x, 0.05f * TotalVector.y + 0.95f * transform.position.y, transform.position.z);

    }

}
