xxxxxxxxxx
172
let currentScene = 0;
let obstacles = [];
let backgroundMusic = null;
let rupeeSound = null;
let dragableElement = null;
let mySpecialFont = null;
function preload() {
// load sounds
backgroundMusic = loadSound('03_game.wav');
rupeeSound = loadSound('rupee.wav');
mySpecialFont = loadFont('Urbanist-BoldItalic.ttf');
}
function setup() {
createCanvas(400, 400);
// create obstacles
obstacles.push( new Thing( random(width) ) )
obstacles.push( new Thing( random(width) ) )
obstacles.push( new Thing( random(width) ) )
dragableElement = new PickUp( width/2, height/2 )
backgroundMusic.setVolume(0.03);
// backgroundMusic.loop(0);
// rupeeSound.pause();
// rupeeSound.stop();
textFont(mySpecialFont);
textSize(36);
}
function draw() {
background(220);
if( frameCount === 120 ){
console.log('music is playing')
// backgroundMusic.loop(0);
}
if( frameCount === 60*3 ){
// obstacles.pop() // remove the last obstacle from array
}
switch( currentScene ){
case 0:
// console.log('Yay first level')
for( let i = 0; i < obstacles.length; i++ ){
obstacles[i].place();
}
dragableElement.place();
break;
}
text( 'Yay Game!', 10, 50 );
}
class Thing {
constructor( posX = 10, posY = 10 ){
// the frameCount the object has been created
this.creationTime = frameCount;
this.x = posX;
this.y = posY;
this.w = 50;
this.h = 50;
/*
setTimeout( function(){
console.log('something after 3 sec')
obstacles.shift();
}, 1000 * 3 )
*/
}
place() {
if( (frameCount - this.creationTime) > 180 ){
// fill('red')
obstacles.shift(); // remove the first element of the obstacle array
}
rect( this.x, this.y, this.w, this.h );
}
}
class PickUp {
constructor( posX = 10, posY = 10 ){
this.x = posX;
this.y = posY;
this.w = 50;
this.h = 50;
this.isDragging = false;
this.innerX = 0;
this.innerY = 0;
}
place() {
push();
let isInObject = false
if( mouseX >= this.x && mouseX <= this.x + this.w ){
// x is within the object
if( mouseY >= this.y && mouseY <= this.y + this.h ){
// y is within the object too
// console.log('for real')
isInObject = true;
}
}
if( isInObject && mouseIsPressed ){
fill('red');
this.isDragging = true;
this.innerX = mouseX - this.x
this.innerY = mouseY - this.y
}else if( isInObject ){
fill('green');
this.isDragging = false;
}else{
fill('blue');
this.isDragging = false;
}
rect( this.x, this.y, this.w, this.h );
pop();
}
}
function mousePressed(){
console.log('click')
rupeeSound.play()
obstacles.push( new Thing( mouseX, mouseY ) )
}
function mouseDragged(){
if( dragableElement.isDragging ){
dragableElement.x = mouseX - dragableElement.innerX
dragableElement.y = mouseY - dragableElement.innerY
}
}