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

public class BlackHoleGravity : MonoBehaviour {

	public float innerRadius = 4.0f;

	public float outerRadius = 8.0f;

	public float mass = 1000.0f;

	public float rotationSpeed = 45.0f;

	// Use this for initialization
	void Start () {

	}
	
	// Update is called once per frame
	void Update () {
		GetComponentInChildren<Transform>().rotation = Quaternion.Euler(0,0, GetComponentInChildren<Transform>().rotation.eulerAngles.z + rotationSpeed * Time.deltaTime);
	}

	public Vector2 doGravity(Vector3 position, float objectMass)
	{
		Vector3 blackHoleSucks = GetComponent<BlackHoleGravity>().transform.position - position;
		
		float gravity = (mass * objectMass) / (Mathf.Max(1000.0f, blackHoleSucks.sqrMagnitude));

		blackHoleSucks.Normalize();

		blackHoleSucks *= gravity;
		blackHoleSucks *= Time.deltaTime * gameObject.GetComponentInParent<Gameplay>().timeMultipler;

		return new Vector2(blackHoleSucks.x, blackHoleSucks.y) * Time.deltaTime;
	}

	public float calcWarp(Vector3 position)
	{
		Vector3 dist = position - transform.position;
		if (dist.magnitude > outerRadius)
			return 1.0f;
		if (dist.magnitude < innerRadius)
			return 0;
		return (dist.magnitude - innerRadius) / (outerRadius - innerRadius);
	}
}
