xxxxxxxxxx
180
const canvasSize = 664;
let images = [];
let objects = [];
let surfaceImage = null;
let bgImage = null;
const threshold = canvasSize * 0.72;
const gravity = 1.01;
let spacesTaken = [];
function preload() {
surfaceImage = loadImage('surface.png');
bgImage = loadImage('bg.jpg');
images.push(loadImage('astronaut.png'));
images.push(loadImage('satelite.png'));
images.push(loadImage('spaceX.png'));
images.push(loadImage('apollo11.png'));
images.push(loadImage('lander.png'));
}
function setup() {
createCanvas(canvasSize, canvasSize);
angleMode(DEGREES);
objects.push( new SpaceObject( 0, 50, 50 * 1.58, width/2, 200) );
// objects.push( new SpaceObject( 1, 140, 140 * 0.73 ) );
// objects.push( new SpaceObject( 2, 160 , 160* 0.4 ) );
objects.push( new SpaceObject( 3, 80, 80 * 0.92, width-100, 50 ) );
objects.push( new SpaceObject( 4, 200 , 200* 0.76, 20, 50 ) );
}
function draw() {
background(0);
// image(bgImage, 0, 0, width, height);
image(surfaceImage, 0, height - width * 0.5, width, width * 0.53333);
stroke('red');
// line(0, threshold, width, threshold)
for (let i = 0; i < objects.length; i++) {
objects[i].place();
}
/*
for( let n = 0; n < spacesTaken.length; n++ ){
fill(0,255,0,10)
rect(spacesTaken[n].x,spacesTaken[n].y,spacesTaken[n].width,spacesTaken[n].height);
}
*/
}
class SpaceObject {
constructor(img, iWidth, iHeight, newX = random(width - iWidth), newY = random(threshold - height / 3)) {
/*
let newX = random(width - iWidth);
let newY = random(threshold - height / 3);
function isInBox( newX, iWidth, newY, iHeight, obj ) {
if( (newX + iWidth > obj.x && newX < obj.x + obj.width) ||
(newY + iHeight > obj.y && newY < obj.y + obj.height) ){
return true;
}else{
return false;
}
}
let foundCount = 0;
let foundFlag = false;
let randomCount = 0;
while ( !foundFlag && spacesTaken.length >= 1 ){
for( let n = 0; n < spacesTaken.length; n++ ){
let obj = spacesTaken[n];
if( !isInBox( newX, iWidth, newY, iHeight, obj ) ){
foundCount++;
}
}
if( foundCount === spacesTaken.length || randomCount === 40 ){
foundFlag = true;
console.log('found')
}else{
console.log(foundCount,'again')
foundCount = 0;
newX = random(width - iWidth);
newY = random(threshold - height / 3);
}
randomCount++;
}
*/
spacesTaken.push({
x: newX,
y: newY,
width: iWidth,
height: iHeight
})
this.img = images[img];
this.x = newX;
this.y = newY;
this.height = iHeight;
this.width = iWidth;
this.speed = 2;
this.mass = 10;
this.rotation = random(10);
this.direction = random(-1, 1);
}
place() {
// y axis
if (this.y + this.height >= threshold && this.speed > 0) {
this.speed = this.speed * -1;
} else if (this.y <= 0) {
this.speed = 1;
}
// falling
if (this.speed > 0) {
this.speed = this.speed * gravity;
// jumping
} else {
this.speed = this.speed * 0.99;
}
/*
fill(255)
noStroke()
text(this.speed,10,20)
*/
// x axis
if (this.x + this.width >= width || this.x <= 0) {
this.direction = this.direction * -1;
}
this.y = this.y + this.speed;
this.x = this.x + this.direction;
push();
//rotate(this.rotation);
image(this.img, this.x, this.y, this.width, this.height);
pop();
// check for collision
for (let i = 0; i < objects.length; i++) {
let col = objects[i];
if ( this.img !== col.img &&
this.x < col.x + col.width &&
this.x + this.width > col.x &&
this.y < col.y + col.height &&
this.y + this.height > col.y) {
this.direction = this.direction * -1;
this.speed = this.speed*-1;
fill(255,0,0,150)
// rect(this.x,this.y, this.width,this.height)
}
/*
if ( this.img !== col.img &&
this.y < col.y + col.height &&
this.y + this.height > col.y ) {
// this.direction = this.direction * -1;
fill(0,255,0,150)
rect(this.x,this.y, this.width,this.height)
}
*/
}
}
}