// Update is called once per frame
    void Update()
    {
        if(player == null){
            player = GameObject.FindGameObjectWithTag("Player").GetComponent(typeof(PlayerScript)) as PlayerScript;
            twin_fang = new Twin_Fang(player, twin_fang_object);
            noxious_blast = new Noxious_Blast(player, noxious_blast_object);
            miasma = new Miasma(player, miasma_object);
        }

        if(!alive && !staleWin){
            player.setWin(true);
            staleWin = true;
            return;
        }

        if(Time.time > deathTimer && !alive)
            Destroy(gameObject);

        if(!player.isAlive())
            return;

        Vector3 lookat = player.getGameObject().transform.position;
        lookat.y = gameObject.transform.position.y;
        gameObject.transform.LookAt(lookat);

        //If the Player is too far away to be aggroed, do not cast spells.
        if(Vector3.Distance(player.getGameObject().transform.position, gameObject.transform.position) > aggroRange)
            return;

        //Phase 1
        if(phase == 1){
            //If the player is poisoned, use twin fangs
            if(player.isPoisoned() && twin_fang.offCooldown() && !animator.isPlaying){
                animator.Play("Spell2");
                twin_fang.setInitialPosition(gameObject.transform.position);
                twin_fang.Execute();
                return;
            }

            //If off cooldown, attempt to use Noxious Blast to poison player
            if(noxious_blast.offCooldown() && !animator.isPlaying){
                animator.Play("Spell1");
                noxious_blast.Execute();
                return;
            }
            //Otherwise, autoattack
            if(nextAttack < Time.time){
                GameObject attack = (GameObject) Instantiate(autoAttack, gameObject.transform.position, Quaternion.identity);
                PhysicsProjectileScript projectile = attack.GetComponent(typeof(PhysicsProjectileScript)) as PhysicsProjectileScript;
                projectile.setTimeToLive(10);
                projectile.setDamage(weaponDamage);
                projectile.setSpeed(25);
                projectile.setDestination(player.getGameObject().transform.position);
                nextAttack = Time.time + 3;
            }

            if((currentHealth / maxHealth) < .66){
                //Phase Transition
                transitionPhase();
            }

        }

        else if (phase == 2){
            //If the player is poisoned, use twin fangs
            if(player.isPoisoned() && twin_fang.offCooldown()){
                animator.Play("Spell2");
                twin_fang.setInitialPosition(gameObject.transform.position);
                twin_fang.Execute();
                return;
            }

            //If off cooldown, attempt to use Miasma to poison player
            if(miasma.offCooldown()){
                animator.Play("Spell2");
                miasma.Execute();
                return;
            }

            //Otherwise, autoattack
            if(nextAttack < Time.time){
                GameObject attack = (GameObject) Instantiate(autoAttack, gameObject.transform.position, Quaternion.identity);
                PhysicsProjectileScript projectile = attack.GetComponent(typeof(PhysicsProjectileScript)) as PhysicsProjectileScript;
                projectile.setTimeToLive(10);
                projectile.setDamage(weaponDamage * 1.3f);
                projectile.setSpeed(35);
                projectile.setDestination(player.getGameObject().transform.position);
                nextAttack = Time.time + 3;
            }

            if((currentHealth / maxHealth) < .33){
                //Phase Transition
                transitionPhase();
            }

        }

        else{
            //If the player is poisoned, use twin fangs
            if(player.isPoisoned() && twin_fang.offCooldown()){
                animator.Play("Spell2");
                twin_fang.setInitialPosition(gameObject.transform.position);
                twin_fang.Execute();
                return;
            }

            //If off cooldown, attempt to use Noxious Blast or Miasma to poison player
            if(noxious_blast.offCooldown()){
                animator.Play("Spell1");
                noxious_blast.Execute();
                return;
            }

            if(miasma.offCooldown()){
                animator.Play("Spell2");
                miasma.Execute();
                return;
            }

            //Otherwise, autoattack
            if(nextAttack < Time.time){
                GameObject attack = (GameObject) Instantiate(autoAttack, gameObject.transform.position, Quaternion.identity);
                PhysicsProjectileScript projectile = attack.GetComponent(typeof(PhysicsProjectileScript)) as PhysicsProjectileScript;
                projectile.setTimeToLive(10);
                projectile.setDamage(weaponDamage * 1.5f);
                projectile.setSpeed(35);
                projectile.setDestination(player.getGameObject().transform.position);
                nextAttack = Time.time + 3;
            }
        }
    }
    // Use this for initialization
    void Start()
    {
        alive = true;
        debuffs = new Hashtable();
        phase = 1;
        animator = gameObject.GetComponent(typeof(Animation)) as Animation;
        //We generally like to avoid try catch blockes. However, in Unity, it is required that a "dummy" instance of every object be
        //present at the beginning of the game, or more mobs cannot be spawned. Since this "dummy" instance is present before the player
        //is instantied, it will be unable to find the player, and this initialization will fail. To remedy this situation, we surround
        //this assignment in a try-catch, then look to update the assignment in the "update" function. Future iterations should look
        //to address this Unity flaw.
        try{
            player = GameObject.FindGameObjectWithTag("Player").GetComponent(typeof(PlayerScript)) as PlayerScript;
            twin_fang = new Twin_Fang(player, twin_fang_object);
            noxious_blast = new Noxious_Blast(player, noxious_blast_object);
            miasma = new Miasma(player, miasma_object);
        } catch{
            ;
        }

        maxHealth = 750 * Mathf.Pow(2, player.Level);
        weaponDamage = 15 * Mathf.Pow(2, player.Level);
        currentHealth = maxHealth;
    }