using UnityEngine;
using System.Collections;

using System;
public class Enemy : Character
	{
	protected float m_randomDirCounter;
	protected Vector2 m_targetPos;
	public float m_noobify = 0.0f;
	protected float m_noobifyAim = 4.0f;

	public Enemy ()
	{
		m_randomDirCounter = -0.01f;
	}
	void FixedUpdate () {
		if (m_randomDirCounter < 0) {
			m_targetPos = HelperFunctions.GetRandomPosition();
			m_randomDirCounter += HelperFunctions.getScaledRandom(5f, 10f);
		}
		Vector2 mov = m_targetPos - m_body.position;
		mov.y = 0;
		if (mov.magnitude < 0.5) {
			m_randomDirCounter -= 1;
		}
		m_randomDirCounter -= Time.deltaTime;
		m_blocking = (Rand.r.NextDouble() > 0.97 ? !m_blocking : m_blocking);
		commonUpdate (mov, Rand.r.NextDouble() > 0.95 + 0.05*m_noobify);
		GameObject pl = GameObject.FindWithTag ("Player");
		if (pl != null) {
			if (Rand.r.NextDouble() > 0.95 + 0.04*m_noobify) {
				Vector2 target = pl.GetComponent<Rigidbody2D> ().position;
				target.x += HelperFunctions.getScaledRandom(-m_noobifyAim * m_noobify, m_noobifyAim * m_noobify);
				target.y += HelperFunctions.getScaledRandom(-m_noobifyAim * m_noobify, m_noobifyAim * m_noobify);
				attack (target, (target - m_body.position).magnitude < 5 ? 1 : 0);
			}
		}
		if (m_win) {
			m_win = false;
			winCallback();
		}
	}

	public void winCallback() {
		GameController cont = GameObject.FindWithTag("gamecontroller").GetComponent<GameController>();
		cont.gameOver();
	}
}

