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 mountains = processing.loadImage('/images/mountains.png');
var bg;
var goingLeft = false;
var goingRight = false;
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 Parallax(img, speed) {
this.speed = speed;
this.width = img.width;
this.height = img.height;
this.count = 0;
this.right = function() {
this.count -= this.speed;
}
this.left = function() {
this.count += this.speed;
}
this.draw = function() {
if (this.count > img.width) {
this.count = 0;
}
if (this.count < 0) {
this.count = img.width;
}
var x1 = this.count;
var x2 = this.count - img.width;
var x3 = this.count + img.width;
if (height > this.height) {
var y = height - img.height;
} else {
var y = 0;
}
processing.image(img, x1, y, img.width, img.height);
processing.image(img, x2, y, img.width, img.height);
processing.image(img, x3, y, img.width, img.height);
}
}
function UFO(position) {
console.log('making ufo');
this.millis = processing.millis();
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) {
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);
processing.textSize(drawSize / 2);
processing.text("millis " + this.millis, this.position.x, this.position.y);
}
};
this.serial = ufoSerial;
ufoSerial ++;
ufos.push(this);
}
processing.setup = function() {
processing.textAlign(processing.CENTER, processing.CENTER);
bg = new Parallax(space, 1);
mtns = new Parallax(mountains, 3);
};
processing.draw = function() {
processing.imageMode(processing.CORNER);
if (goingLeft) {
bg.left();
mtns.left();
} else if (goingRight) {
bg.right();
mtns.right();
}
bg.draw();
mtns.draw();
processing.imageMode(processing.CENTER);
for (var i = ufos.length - 1; i >= 0; i--) {
ufos[i].draw();
}
};
processing.keyPressed = function() {
switch(processing.key.toString()) {
case 'a':
case 'A':
goingLeft = true;
break;
case 'e':
case 'E':
case 'd':
case 'D':
goingRight = true;
break;
}
};
processing.keyReleased = function() {
switch(processing.key.toString()) {
case 'a':
case 'A':
goingLeft = false;
break;
case 'e':
case 'E':
case 'd':
case 'D':
goingRight = false;
break;
}
};
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);