(function() {
  'use strict';

  function Weapon(game, x, y, coolDown, bulletSpeed, bullets, damage) {
    this.game = game;
    this.x = x;
    this.y = y;
    this.coolDown = coolDown;
    this.nextFire = 0;
    this.bullets = bullets;
    this.bulletSpeed = bulletSpeed;
    this.damage = damage;
  }

  Weapon.prototype = {
    updateManually: function(x, y){
      this.x = x;
      this.y = y;
    },

    fire: function(x, y){
      if (this.game.time.now > this.nextFire && this.bullets.countDead() > 0){
          this.nextFire = this.game.time.now + this.coolDown;
          var bullet    = this.bullets.getFirstDead();
          bullet.reset(this.x, this.y);
          bullet.rotation = this.takeAim(x, y);
      }
    },

    takeAim: function(x, y) {
      var perfAngle = this.game.physics.angleToXY(this, x, y);
      // TODO: Make targeting depend on users intelligence.
      return perfAngle;
    },

    resetBullet: function(bullet){
      bullet.reset(this.x, this.y); // resets sprite and body
      bullet.rotation = this.game.physics.moveToPointer(bullet, this.bulletSpeed);
    }

  };

  window.Darwinator = window.Darwinator || {};
  window.Darwinator.Weapon = Weapon;

}());

(function() {
  'use strict';

  function Weapon(game, x, y, coolDown, bulletSpeed, bullets, damage, owner) {
    this.game         = game;
    this.x            = x;
    this.y            = y;
    this.coolDown     = coolDown;
    this.nextFire     = 0;
    this.bullets      = bullets;
    this.bulletSpeed  = bulletSpeed;
    this.damage       = damage;
    this.owner        = owner;
  }

  Weapon.prototype = {
    updateManually: function(x, y){
      this.x = x;
      this.y = y;
    },

    fire: function(x, y){
      if (this.game.time.now > this.nextFire && this.bullets.countDead() > 0){
          this.nextFire = this.game.time.now + this.coolDown;
          var bullet    = this.bullets.getFirstDead();
          this.resetBullet(bullet, x, y);
      }
    },

    takeAim: function(x, y) {
      var perfAngle = this.game.physics.angleToXY(this, x, y);
      perfAngle += (Math.random() - 0.5) * 15 / this.owner.attributes.intellect;
      return perfAngle;
    },

    resetBullet: function(bullet, x, y){
      bullet.reset(this.x, this.y); // resets sprite and body
      var angle = this.takeAim(x, y);
      bullet.rotation = angle;
      this.game.physics.velocityFromRotation(angle, this.bulletSpeed, bullet.body.velocity);
    }

  };

  window.Darwinator = window.Darwinator || {};
  window.Darwinator.Weapon = Weapon;

}());

Make aim depend on owners intelligence