using UnityEngine;
using System.Collections;

public class EnemyController : InteractableObject {


    public int health;


    private GameObject player;
    private Rigidbody2D playerRigid;
    public GameObject bullet;
    public float shotCoolDown = 0.5f;
    public float initShotWait = 0.0f;
    protected float lastShotTime = 0.0f;
    public GameObject healthDisp;

	// Use this for initialization
	protected virtual void Start () {
        lastShotTime = Time.time + initShotWait;
        player = GameObject.FindGameObjectWithTag("Player");
        playerRigid = player.GetComponent<Rigidbody2D>();
        healthDisp.SetActive(true);
    }
	
	// Update is called once per frame
	void FixedUpdate () {
	    if (Time.time - lastShotTime >= shotCoolDown) {
            lastShotTime = Time.time;
            shoot(Vector2.up);
            shoot(Vector2.down);
            shoot(Vector2.left);
            shoot(Vector2.right);
        }


	}


    protected void shoot(Vector2 startVelocity) {
        GameObject bulletClone = (GameObject)Instantiate(bullet, transform.position, transform.rotation);
        MissileController bulletScript = bulletClone.GetComponent<MissileController>();
        bulletScript.startVelocity = startVelocity;
        
        if (player != null)
            bulletScript.targetPos = player.transform.position;
        bulletScript.target = player;
        bulletScript.targetRigid = playerRigid;
        bulletScript.ignoreList = GetComponentsInChildren<Collider2D>();
        bulletScript.spawningObject = this;


        bulletScript.enabled = true;
        

    }

    public override void shot() {
        health--;
        //Debug.Log("I got hit, health: " + health);
        if (health <= 0) {
            GameMemory.control.enemyDeath();
            Destroy(gameObject);
        }

        Vector3 newScale = new Vector3(health / 10f, health / 10f, health / 10f);
        //Debug.Log("new scale = " + newScale);

        if (healthDisp != null) {
            healthDisp.transform.localScale = newScale;
        }
        transform.localScale = newScale;

    }
}
