﻿document.addEventListener("DOMContentLoaded", function(event) { 

  var ctx;
  var canvas = document.getElementById('fleet-display');
  if (canvas.getContext) {
    ctx = canvas.getContext('2d');
  }

  document.getElementById('button-progress').addEventListener('click', function(){
    draw();
  });
  
  // One half of the basic invader, 6x8.
  // Invaders are symmetrical to a vertical axis, making the final size 11x8 pixels.
  var baseInvader = [
    [0,0,1,0,0,0],
    [0,0,0,1,0,0],
    [0,0,1,1,1,1],
    [0,1,1,0,1,1],
    [1,1,1,1,1,1],
    [1,0,1,1,1,1],
    [1,0,1,0,0,0],
    [0,0,0,1,1,0],
  ];

  function draw() {
    drawInvader(ctx,0,0,baseInvader);
  }
});

function drawInvader(context, x0, y0, invader){
  var pixelSize = 10;
  context.fillStyle = 'rgb(200,200,200)';
  
  for(var y=0;y<invader.length;y++){
    var len = invader[y].length;
    //we're mirroring every column but the last
    for(var x=0;x<len*2-1;x++){ 
      var xPos = x>=len? x-(x-len+1)*2 : x;
      if(invader[y][xPos]){
        context.fillRect(x0+x*pixelSize,y0+y*pixelSize,pixelSize,pixelSize);        
      }
    }
  }  
}
@@ -73,22 +73,8 @@
  ctx
-;%0A  var canvas
  = d
@@ -116,53 +116,8 @@
 ay')
-;%0A  if (canvas.getContext) %7B%0A    ctx = canvas
 .get
@@ -130,20 +130,16 @@
 t('2d');
-%0A  %7D
 %0A%0A  docu
@@ -570,49 +570,910 @@
     
-drawInvader(ctx,0,0,baseInvader);%0A  %7D%0A%7D);
+//TODO: generate input seed object - length should equal no. of mutators!%0A    var inputSeed = %5B%0A      %7Bmovement: 0%7D%0A    %5D;%0A    var invader = mutateInvader(baseInvader, inputSeed);%0A    drawInvader(ctx,0,0,invader);%0A  %7D%0A%7D);%0A%0Afunction deepCopy(obj)%7B%0A  return JSON.parse(JSON.stringify(obj));%0A%7D%0A%0Afunction mutateInvader(base, inputs)%7B%0A  var invader = deepCopy(base);%0A  mutators = %5BantennaMutator/*, weaponMutator, thrusterMutator*/%5D;%0A  mutators.forEach(function(mutator, i)%7B%0A    invader = mutator(invader, inputs%5Bi%5D);%0A  %7D);%0A  return invader;%0A%7D%0A%0Afunction antennaMutator(invader, input)%7B%0A  var part = %5B%0A    %5B1,1,1%5D,%0A    %5B1,1,1%5D%0A  %5D;%0A  //TODO mutate based on input%0A  return applyPart(invader, part, 2, 0);%0A%7D%0A%0Afunction applyPart(base, part, x, y)%7B%0A  var invader = deepCopy(base);%0A  for(var i=0;i%3Cpart.length;i++)%7B%0A    for(var j=0;j%3Cpart%5Bi%5D.length;j++)%7B%0A      invader%5By+i%5D%5Bx+j%5D = 1;%0A    %7D%0A  %7D%0A  return invader;%0A%7D
 %0A%0Afu

add mutator infrastructure with dummy mutator