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

public class CentreOnOrbit : MonoBehaviour {
	
	// Use this for initialization
	void Start () {
		OrbitObject.findThingsToOrbit();
	}
	
	// Update is called once per frame
	void FixedUpdate () {
		CentreGameObject();
	}
	
	Vector3 cacheCentre;
	void  CentreGameObject(){
		
		cacheCentre = Vector3.zero;
		int numberOfThigns = 0;
		for (int i = 0; i < OrbitObject.thingsToOrbit.Length; i++)
		{
			if(!OrbitObject.thingsToOrbit[i].IsBeingThrown()){
				cacheCentre += OrbitObject.thingsToOrbit[i].Position();
				numberOfThigns++;
			}
		}
		cacheCentre = cacheCentre / numberOfThigns;
		cacheCentre += (cacheCentre -  transform.position);
		cacheCentre.y -= 2f;
		gameObject.transform.position = Vector3.Lerp(gameObject.transform.position, cacheCentre, 0.1f);
	}
	

	static Vector3 cacheVelocity;
	public static Vector3 UniverseVelocity(){
		cacheVelocity = Vector3.zero;
		for (int i = 0; i < OrbitObject.thingsToOrbit.Length; i++)
		{
			cacheVelocity += OrbitObject.thingsToOrbit[i].Velocity();
		}
		cacheVelocity = cacheVelocity / OrbitObject.thingsToOrbit.Length;
		return cacheVelocity;
	}

}
