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

public class Tower : MonoBehaviour {

    Transform turretTransform;

	// Use this for initialization
	void Start () {
        turretTransform = transform.Find("Base").transform.Find("Turret");
	}
	
	// Update is called once per frame
	void Update () {
        SolMinion[] solMinions = GameObject.FindObjectsOfType<SolMinion>();

        SolMinion nearestMinion = null;
        float dist = Mathf.Infinity;

        foreach(SolMinion e in solMinions)
        {
            float d = Vector3.Distance(this.transform.position, e.transform.position);
            if(nearestMinion == null || d < dist)
            {
                nearestMinion = e;
                dist = d;
            }
        }

        if(nearestMinion == null)
        {
            Debug.Log("No minions?");
            return;
        }

        Vector3 dir = nearestMinion.transform.position - this.transform.position;

        Quaternion lookRot = Quaternion.LookRotation(dir);

        turretTransform.rotation = Quaternion.Euler(0, lookRot.eulerAngles.y, 0);
	}
}