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

public class Gravity : MonoBehaviour {

    public float reach;
    public GameObject UIring;
    public World ourWorld;

	void Start () {
	}
	
	void Update () {
        if (Input.GetMouseButton(0))
        {
            UIring.SetActive(true);
            UIring.transform.localScale = new Vector3(reach, reach, 1f);
            var tilesInRange = Physics.OverlapSphere(transform.position, reach);
            var worldsInRange = new HashSet<World>();
            foreach (var tileCollider in tilesInRange)
            {
                var tile = tileCollider.GetComponentInParent<Tile>();
                if (tile.world == null || tile.world == ourWorld)
                    continue;
                worldsInRange.Add(tile.world);
            }
            foreach (var world in worldsInRange)
            {
                if (world == ourWorld)
                    continue;
                var follower = world.GetComponent<FollowGravity>();
                follower.enabled = true;
                follower.target = transform;
                follower.targetWorld = ourWorld;
            }
        }
        else
            UIring.SetActive(false);
	}
}
