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

public class DangerItem : MonoBehaviour {

	public Vector2 velocity;
	public float rotationSpeed = 45.0f;

	public float mass = 10.0f;

	// Use this for initialization
	void Start () {
	}

	// Update is called once per frame
	void Update () {

		Gameplay gameplay = gameObject.GetComponentInParent<Gameplay>();

		transform.rotation = Quaternion.Euler(0, 0, transform.rotation.eulerAngles.z + rotationSpeed * Time.deltaTime * gameplay.timeMultipler);

		BlackHoleGravity blackHole = gameplay.GetComponentInChildren<BlackHoleGravity>();
		velocity += blackHole.doGravity(transform.position, mass);

		gameObject.transform.position += new Vector3(velocity.x, velocity.y, 0) * gameplay.timeMultipler;

		Vector3 playerCollide = gameplay.playerShip.transform.position - transform.position;

		if (playerCollide.magnitude < 2.0f)
		{
			Vector2 relVelocity = velocity - gameplay.playerShip.velocity;
			playerCollide.Normalize();
			float mag = Vector3.Dot(playerCollide, new Vector3(relVelocity.x, relVelocity.y, 0).normalized);
			gameplay.playerShip.velocity += new Vector2(playerCollide.x, playerCollide.y) * mag * 0.3f;
			gameplay.playerShip.dangerHit.Play();
			gameplay.playerShip.dangerSplode.Play();
			Destroy(gameObject);

		}

		float warpFactor = blackHole.calcWarp(gameObject.transform.position);

		if (warpFactor <= 1.0f)
		{
			gameObject.transform.localScale = new Vector3(warpFactor, warpFactor, 1.0f);
			if (warpFactor <= 0)
				Destroy(gameObject);
		}
	}
}
