Reversalator6000
2014-11-18 00:17:49 +0000
var width = window.innerWidth;
var height = $('body').height() - 3;
var halfWidth = width / 2;
var halfHeight = height / 2;

function drawerSketch(processing) {
  // set size
  processing.size(width, height);

  var img = processing.loadImage('/images/ufo.png');
var space = processing.loadImage('/images/space.jpg');
var bg;

var lifeTime = 100
var ufos = [];
var ufoSerial = 0;

function Vector(x, y) {
  this.x = x;
  this.y = y;
  this.add = function(vector) {
    this.x += vector.x;
    this.y += vector.y;
  };
  this.times = function(vector) {
    this.x *= vector.x;
    this.y *= vector.y;
  };
}


launchUFO = function() {
  var position = new Vector(processing.mouseX, processing.mouseY);
  var ufo = new UFO(position);
};

function Space() {
  this.width = space.width;
  this.height = space.height;
  this.count = 0;

  this.draw = function() {
    this.count ++;
    if (this.count > width) {
      this.count = 0;
    }
    var x1 = this.count;
    var x2 = this.count - space.width;
    processing.image(space, x1, 0, this.width, this.height);
    processing.image(space, x2, 0, this.width, this.height);
    console.log(x1, x2);
  }
}

function UFO(position) {
  console.log('making ufo');
  this.lifespan = lifeTime;
  this.position = position;

  if (this.position.x < halfWidth) {
    this.direction = 6;
  } else {
    this.direction = -4;
  }


  this.draw = function() {
    this.position.y += 1;
    this.position.x += this.direction;
    this.lifespan -= 1;
    if (this.lifespan <= 0) {
      console.log('deleting');
      for (var b in ufos) {
        var ufo = ufos[b];
        if (ufo.serial === this.serial) {
          ufos.splice(b, 1);
        }
      }
    } else {
      var drawSize = processing.map(this.lifespan, 30, 0, lifeTime, 0);
      processing.image(img, this.position.x, this.position.y, drawSize, drawSize);
    }
  };

  this.serial = ufoSerial;
  ufoSerial ++;

  ufos.push(this);
}

processing.setup = function() {
  bg = new Space();
};

processing.draw = function() {
  processing.imageMode(processing.CORNER);
  bg.draw();
  processing.imageMode(processing.CENTER);
  for (var i = ufos.length - 1; i >= 0; i--) {
    ufos[i].draw();
  }

};

processing.mousePressed = function() {
  launchUFO();
  mouseDown = true;
};

processing.mouseReleased = function() {
  mouseDown = false;
};


}
// attach the sketch function to the canvas
var processingInstance = new Processing(document.getElementById('sketch'), drawerSketch);