// Update is called once per frame
	void Update () {
        if (isAggroed && player.isDead == false) {
            //Throw a bomb with a cooldown towards the direction of the player if the player is within aggro range
            currentTime += Time.deltaTime;
            if (currentTime >= throwCooldown) {
                bombObject = Instantiate(bomb, transform.position, transform.rotation) as BombScript;
                Vector2 toPlayer = new Vector2(playerPosition.position.x - transform.position.x, playerPosition.position.y - transform.position.y);
                bombObject.GetComponent<Rigidbody2D>().velocity = (toPlayer * throwForce);
                currentTime = 0;
            }
        }
	}
     public void Update()
     {

          rnd = new System.Random();
          checkInvincibility();
          if (checkStun())
          {
               stunTimer -= Time.deltaTime;
               moveController.Move(0, 0);
          }
          else if (player != null)
          {
               health.cancelKnockback();
               findPos();
               float xSp = player.transform.position.x - transform.position.x;
               float ySp = player.transform.position.y - transform.position.y;
               distance = player.transform.position - transform.position;

               if (distance.magnitude <= AgroRange)
               {
                    isAgro = true;

               }
               else
               {
                    isAgro = false;
               }
               distanceFromPoint = locArr[loc % 4] - transform.position;
               if (distanceFromPoint.magnitude < 0.3)
               {
                    loc++;
                    distanceFromPoint = locArr[loc % 4] - transform.position;
               }
               moveController.Move(distanceFromPoint.normalized/8);

               if (isAgro)
               {
                    if (cooldown >= 2)
                    {
                         cooldown = 0;
                         findPos();
                         //No prep animation, just has default spinning and creates projectiles
                         if (mine_CD > 5)
                         {
                              mine = Instantiate(mineObj, transform.position, transform.rotation) as BombScript;
                              mine_CD = 0;
                         }
                         if (homing_CD > 8)
                         {
                              homing = Instantiate(homingObj, transform.position, transform.rotation) as Homer;
                              homing_CD = 0;
                         }
                         else if (bomb_CD > 8)
                         {
                              //bomb = Instantiate(bombObj, transform.position, transform.rotation) as BombScript;
                             // bomb.GetComponent<Rigidbody>().velocity = direction * 6;
                              bomb_CD = 0;

                              bomb = Instantiate(bombObj, transform.position, transform.rotation) as BombScript;
                              Vector2 toPlayer = new Vector2(player.transform.position.x - transform.position.x, player.transform.position.y - transform.position.y);
                              bomb.GetComponent<Rigidbody2D>().velocity = (toPlayer.normalized/3);
                         }
                         else if (shot_CD > 6)
                         {
                              float angle = Mathf.Atan2(direction.x, direction.y) * Mathf.Rad2Deg;
                              var q = Quaternion.AngleAxis(angle, Vector3.forward);
                              missile = Instantiate(missileObj, transform.position, q) as WindupProjectile;
                              missile.Shoot(0,direction.normalized);
                              shot_CD = 0;
                         }
                         else
                         {
                              cooldown = 1;
                         }
                    }
               }
               bomb_CD += Time.deltaTime;
               shot_CD += Time.deltaTime;
               mine_CD += Time.deltaTime;
               homing_CD += Time.deltaTime;
               cooldown += Time.deltaTime;
          }
     }