<commit_before>﻿using UnityEngine;
using System.Collections;

public class TowerController : MonoBehaviour {

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	
	}
}<commit_msg>Implement tower<commit_after>﻿using UnityEngine;
using System.Collections;

public class TowerController : MonoBehaviour {

	Transform towerTransform;

	public float range = 10f;
	public GameObject bulletPrefab;

	public int cost = 5;

	float fireCooldown = 0.5f;
	float fireCooldownLeft = 0;

	public float damage = 1;
	public float radius = 0;

	void Start () {
		towerTransform = transform.Find("Tower");
	}
	
	void Update () {
		// TODO: Optimize this!
		Metallkefer[] enemies = GameObject.FindObjectsOfType<Metallkefer>();

		Metallkefer nearestMetallkefer = null;
		float dist = Mathf.Infinity;

		foreach(Metallkefer e in enemies) {
			float d = Vector3.Distance(this.transform.position, e.transform.position);
			if(nearestMetallkefer == null || d < dist) {
				nearestMetallkefer = e;
				dist = d;
			}
		}

		if(nearestMetallkefer == null) {
			Debug.Log("No enemies?");
			return;
		}

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

		Quaternion lookRot = Quaternion.LookRotation( dir );

		//Debug.Log(lookRot.eulerAngles.y);
		towerTransform.rotation = Quaternion.Euler( 0, lookRot.eulerAngles.y, 0 );

		fireCooldownLeft -= Time.deltaTime;
		if(fireCooldownLeft <= 0 && dir.magnitude <= range) {
			fireCooldownLeft = fireCooldown;
			ShootAt(nearestMetallkefer);
		}

	}

	void ShootAt(Metallkefer e) {
		GameObject bulletGO = (GameObject)Instantiate(bulletPrefab, this.transform.position, this.transform.rotation);

		Bullet b = bulletGO.GetComponent<Bullet>();
		b.target = e.transform;
		b.damage = damage;
		b.radius = radius;
	}
}

{'fraction_non_alphanumeric': 0.11178082191780822, 'fraction_numerical': 0.007123287671232876, 'mean_word_length': 4.016483516483516, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5542942', 'n_tokens_mistral': 681, 'n_tokens_neox': 601, 'n_words': 137}