// IGO_SimpleCircleLaser.js
// By Patrick Gillespie

// Implements a laser gun interface

function IGO_SimpleCircleLaser() {
  this.xPos=0;
  this.yPos=0;
  this.xVel=0;
  this.yVel=0;
  this.rotationPos=0;
  this.existenceCountDown=0;
  this.laserImages=[];
  this.halfWidth=8;
  this.halfHeight=8;
  this.laserState=1;
  this.shouldRedraw=true;//not used yet
}

IGO_SimpleCircleLaser.prototype.init = function() {
  for (var i = 0; i < 3; i++) {
    this.laserImages[i] = new Image();
    this.laserImages[i].src = "../images/lasers/circle-blue-"+i+".png";
  }
}

IGO_SimpleCircleLaser.prototype.updatePosition = function() {
  this.xPos = this.xPos + this.xVel; 
  this.yPos = this.yPos + this.yVel;
  //this.laserState++;
  //if (this.laserState>=2) {this.laserState=0;}
}

IGO_SimpleCircleLaser.prototype.draw = function(context) {
   
  context.save();
  
  context.translate(this.xPos+this.halfWidth,this.yPos+this.halfHeight);
  context.rotate(SpaceShip.rotationPos);
  context.translate(-(this.xPos+this.halfWidth),-(this.yPos+this.halfHeight));
  context.drawImage(this.laserImages[this.laserState], this.xPos, this.yPos);
  //this.shouldRedraw=false;
  
  context.restore();
   
}

function IGO_SimpleCircleLaserGun() {
  this.numFramesBetweenLasers=5;
  this.frameAt=0;
  this.readyLaserQueue=[];
  this.firedLaserQueue=[];
  this.xSpeed=7;
  this.ySpeed=7;
  this.laserQueueMax=10;
  this.laserExistenceTime=30;//frames
}

IGO_SimpleCircleLaserGun.prototype.draw = function(context) {
  
  for (var i = 0; i < this.firedLaserQueue.length; i++) {
    this.firedLaserQueue[i].draw(context);
  }
}

IGO_SimpleCircleLaserGun.prototype.updatePosition = function(context) {
  
  for (var i = 0; i < this.firedLaserQueue.length; i++) {
    
    this.firedLaserQueue[i].updatePosition();
    this.firedLaserQueue[i].existenceCountDown--;
    
    if (this.firedLaserQueue[i].existenceCountDown==0) {
      this.readyLaserQueue.push(this.firedLaserQueue.pop());
    }
  }
  
  if (this.frameAt>0) {this.frameAt--;}
}

IGO_SimpleCircleLaserGun.prototype.init = function() {
  
  for (var i = 0; i < this.laserQueueMax; i++) {
     var newLaser = new IGO_SimpleCircleLaser;
     newLaser.init();
     this.readyLaserQueue.push(newLaser);
  }
  
}

IGO_SimpleCircleLaserGun.prototype.fireLaser = function(posX, posY, dirX, dirY, shipVelX, shipVelY, shipHalfWidth, shipHalfHeight) {

  if (this.readyLaserQueue.length==0) {return;}
  if (this.frameAt!=0) {return;} // return if we haven't counted down enough between laser shots
  
  this.frameAt=this.numFramesBetweenLasers;
  
  var newLaser = this.readyLaserQueue.pop();
  newLaser.xPos = posX+(shipHalfWidth-newLaser.halfWidth)+(dirX * (shipHalfWidth+4));
  newLaser.yPos = posY+(shipHalfHeight-newLaser.halfHeight)+(dirY * (shipHalfHeight+4));
  newLaser.xVel = 20 * dirX + shipVelX;
  newLaser.yVel = 20 * dirY + shipVelY;
  newLaser.existenceCountDown = this.laserExistenceTime; // frames
  this.firedLaserQueue.unshift(newLaser);

}