Your browser does not support the canvas tag.

previous        Show / Hide Source        Download        next
////////////////////Interactive Toy///////////////////
//////////////////Hoverball_basement//////////////////
////////////////////By Gavin Nelson///////////////////
//This toy gives the you many options to alter the////
//environment. The swiches on the right side will/////
//alter the scene in different ways. Play around to///
//find out what you can do. There is a game mode//////
//that will randomize the x direction of the ball/////
//////////////////////////////////////////////////////

//Switches

boolean roomLight = false;
boolean gameMode = false;
boolean raveMode = false;
boolean raveModeFlag = false;
boolean playModeRestartFlag = false;
float redToggle = 245;
float redToggleValue;
float greenToggle = 245;
float greenToggleValue;
float blueToggle = 245;
float blueToggleValue;
float value;
float red;
float blue;
float green;
float playingBlockX = 162.5;
float playingBlockY = 50;
float playingBlockWeight = 0.1;
float playingBlockYMovement = 0;
float playingBlockYAcceleration = 2;
float playingBlockXMovement = 0;
int redFloor = 30;
int greenFloor = 60;
int blueFloor = 90;
int toggleLocation;



void setup() {
  size(400, 400);
  background(0);
  frameRate(60);
  println("Play around and see what you can do! My basement hoverball setup is ready to go");
  println("click on the control panel to play with the lights! Or use w-a-s-d to play with  the ball");
}

//Main Block

void draw() {
  rectMode(CENTER);
  noStroke();
  fill(0);
  if (gameMode == true) {
    fill(100 + 50 * sin(frameCount * 0.3));
  }
  rect(200, 200, 400, 400);
  layerOne();
  layerTwo();
  layerThree();
  layerFour();
}
/////////////////////////////////////////////////
////////////////////Layer One////////////////////
/////////////////////////////////////////////////

void layerOne() {

  //Universal counters

  int i = 0;
  int x = 0;

  /////////////
  //Back wall//
  /////////////

  rectMode(CORNERS);
  fill(100);
  rect(80, 0, 400, 320);
  rectMode(CENTER);
  strokeWeight(2);
  stroke(50);

  //Back wall horizontal mortar

  while (i<=8) {
    line(80, i*40, 400, i*40);
    i++;
  }
  i = 0;

  //Back wall vertical mortar

  line(80, 0, 80, 320);
  while (i<=8) {
    if (i%2 == 0) {
      while (x<4) {
        line(160+i*40, x*80, 160+i*40, 40+x*80);
        x++;
      }
      x = 0;
    }
    if (i%2 == 1) {
      while (x<4) {
        line(80+i*40, 40+x*80, 80+i*40, 80+x*80);
        x++;
      }
      x = 0;
    }
    i++;
  }
  i = 0;

  /////////////
  //Left wall//
  /////////////

  quad(0, 0, 80, 0, 80, 320, 0, 400);

  //Left wall horizontal mortar 

  strokeWeight(1.4);
  while (i <8) {
    line(80, 40+i*40, 0, 50+i*50);
    i++;
  }
  i = 0;

  //Left wall vertical mortar


  while (i<8) {
    if (i%2 == 0) {
      while (x<4) {
        line(40, 0+i*45, 40, 45+i*45);
        x++;
      }
      x = 0;
    }
    if (i%2 == 1) {
      while (x<4) {
        line(60, 0+i*42.5, 60, 42.5+i*42.5);
        line(20, 0+i*47.5, 20, 47.5+i*47.5);
        x++;
      }
      x = 0;
    }
    i++;
  }
  i = 0;

  /////////
  //Floor//
  /////////

  if (raveMode == true) {
    fill(redFloor, greenFloor, blueFloor);
  } else if (raveMode == false) {
    fill(200);
    stroke(50);
    strokeWeight(1);
  }
  while (i < 10) {
    quad(80+i*80, 320, 120+i*80, 320, 85+i*85, 360, 42.5+i*85, 360);
    quad(85+i*85, 360, 127.5+i*85, 360, 95+i*90, 400, 50+i*90, 400);
    i++;
    redFloor = 100+ (redFloor +35) % 255;
    greenFloor = 100+ (greenFloor +35) % 255;
    blueFloor = 100+ (blueFloor +35) % 255;
  }


  /////////
  //Light//
  /////////

  //Electrical feed
  strokeWeight(4);
  stroke(150);
  line(300, 25, 300, 100);
  line(300, 100, 400, 100);
  noStroke();

  //Light bulb and fixture

  if (roomLight == true) {
    fill(255, 255, 0);
  }
  if (roomLight == false) {
    fill(255, 255, 0, 40);
  }
  ellipse(300, 25, 20, 20);
  rect(300, 35, 10, 10, 10);
  fill(230);
  rect(300, 45, 20, 20, 10);

  //Light emission

  if (roomLight == true && raveMode == false) {
    radiantLight(255, 255, 0, 300, 25, 20, 20, 255, 4);
  }
}

/////////////////////////////////////////////////
////////////////////Layer Two////////////////////
/////////////////////////////////////////////////

void layerTwo() {

  /////////////////
  //Control panel//
  /////////////////

  rectMode(CORNERS);

  //Control panel shadow

  if (roomLight == true) {
    noStroke();
    fill(0, 150);
    triangle(300, 375, 350, 600, 500, 375);
  }

  //Control panel legs

  stroke(105, 59, 0);
  strokeWeight(6);
  line(310, 375, 310, 380);
  line(390, 375, 390, 380);

  //Control panel face

  noStroke();
  fill(155, 109, 50);
  rect(300, 125, 400, 375);
  fill(205, 159, 100);
  quad(300, 125, 325, 100, 400, 100, 400, 125);

  ///////////////////////////////////////////////
  //Control panel Labelling, Switches and Logic//
  ///////////////////////////////////////////////

  textAlign(CENTER);
  fill(0);
  textSize(10);

  //Room Light

  rectMode(CENTER);
  stroke(0);
  strokeWeight(1);
  fill(150);
  rect(350, 170, 60, 20);
  fill(0);

  if (mouseX >320 && mouseX < 385 && mouseY > 130 && mouseY < 180) {
    noFill();
    strokeWeight(4);
    stroke(255, 255, 0);
    rect(350, 170, 60, 20);
    fill((frameCount*6)%255, (frameCount*4)%255, (frameCount*2)%255);
    if (mousePressed && roomLight == true) {
      roomLight = false;
      //delay(50);
    } else if (mousePressed && roomLight == false) {
      roomLight = true;
      //delay(50);
    }
  }

  text("Room Light", 350, 150);

  //Board Lights

  stroke(0);
  strokeWeight(1);
  fill(150);
  rect(320, 230, 10, 35);
  rect(350, 230, 10, 35);
  rect(380, 230, 10, 35);

  fill(0);
  if (mouseX >315 && mouseX < 385 && mouseY > 180 && mouseY < 250) {
    noFill();
    strokeWeight(4);
    stroke(255, 255, 0);
    if (mouseX >315 && mouseX < 338 && mouseY > 180 && mouseY < 250) {
      rect(320, 230, 10, 35);
    }
    if (mouseX >338 && mouseX < 361 && mouseY > 180 && mouseY < 250) {
      rect(350, 230, 10, 35);
    }
    if (mouseX >361 && mouseX < 385 && mouseY > 180 && mouseY < 250) {
      rect(380, 230, 10, 35);
    }
    fill((frameCount*6)%255, (frameCount*4)%255, (frameCount*2)%255);
  }
  text("Board Lights", 350, 200);

  //Game mode

  stroke(0);
  strokeWeight(1);
  fill(150);
  rect(350, 280, 60, 20);
  fill(0);
  if (mouseX >315 && mouseX < 385 && mouseY > 250 && mouseY < 300) {
    noFill();
    strokeWeight(4);
    stroke(255, 255, 0);
    rect(350, 280, 60, 20);
    fill((frameCount*6)%255, (frameCount*4)%255, (frameCount*2)%255);
    if (mousePressed && gameMode == true) {
      gameMode = false;
      //delay(100);
    } else if (mousePressed && gameMode == false) {
      gameMode = true;
      println("Oh i'm sorry, the parts for the game haven't come in yet. The score board and nets should be coming in soon");
      println("feel free to play with its wacky setting though");
      //delay(100);
    }
  }
  text("Game Mode", 350, 260);

  //Basement rave
  stroke(0);
  strokeWeight(1);
  fill(150);
  rect(350, 330, 60, 20);
  fill(0);
  if (mouseX >315 && mouseX < 390 && mouseY > 300 && mouseY < 350) {
    noFill();
    strokeWeight(4);
    stroke(255, 255, 0);
    rect(350, 330, 60, 20);
    fill((frameCount*6)%255, (frameCount*4)%255, (frameCount*2)%255);
    if (mousePressed && raveMode == true) {
      raveMode = false;
      //delay(100);
    } else if (mousePressed && raveMode == false) {
      raveMode = true;
      println("Party Time!!!");
    }
  }
  text("Basement Rave", 350, 310);
  fill(0);

  ///////////
  //Board//
  ///////////

  //Back section

  fill(45, 30, 15);
  stroke(0);
  strokeWeight(1);
  rectMode(CORNERS);
  rect(100, 50, 225, 275); 
  fill(145, 130, 115);
  quad(100, 50, 105, 45, 230, 45, 225, 50);
  quad(225, 50, 230, 45, 230, 270, 225, 275);
  fill(245, 230, 215);
  rect(105, 55, 220, 270);
  fill(255, 50, 50);
  stroke(50, 50, 255);
  strokeWeight(1);
  triangle(162.5, 70, 215, 100, 110, 100);
  triangle(162.5, 250, 215, 220, 110, 220);
  rect(110, 165, 215, 168);
  fill(245, 230, 215);
  noStroke();
  triangle(162.5, 75, 215, 105, 110, 105);
  triangle(162.5, 245, 215, 215, 110, 215);
  fill(redToggleValue, 50, 50);
  quad(122.5, 125, 132.5, 175, 122.5, 225, 112.5, 175);
  fill(0, 50 + greenToggleValue, 50);
  quad(162.5, 125, 172.5, 175, 162.5, 225, 152.5, 175);
  fill(0, 50, 50 + blueToggleValue);
  quad(202.5, 125, 212.5, 175, 202.5, 225, 192.5, 175);
  radiantLight(redToggleValue, 0, 0, 122.5, 175, 10, 100, redToggleValue * 0.05, 10);
  radiantLight(0, greenToggleValue, 0, 162.5, 175, 10, 100, greenToggleValue * 0.05, 10);
  radiantLight(0, 0, blueToggleValue, 202.5, 175, 10, 100, blueToggleValue * 0.05, 10);


  //Stand-offs

  fill(200);
  stroke(0);
  ellipse(105, 55, 10, 10);
  ellipse(220, 55, 10, 10);
  ellipse(105, 270, 10, 10);
  ellipse(220, 270, 10, 10);

  ellipse(100, 60, 10, 10);
  ellipse(215, 60, 10, 10);
  ellipse(100, 275, 10, 10);
  ellipse(215, 275, 10, 10);

  //Pegs

  rectMode(CENTER);
  fill(50);
  rect(130, 110, 10, 10);
  rect(195, 110, 10, 10);
  rect(162.5, 160, 10, 10);
  rect(130, 210, 10, 10);
  rect(195, 210, 10, 10);
  rect(162.5, 260, 10, 10);
}

///////////////////////////////////////////////////
////////////////////Layer Three////////////////////
///////////////////////////////////////////////////

void layerThree() {

  /////////////////
  //Playing Block//
  /////////////////

  fill(255);
  strokeWeight(1);
  stroke(0);
  playingBlockX = playingBlockX + playingBlockXMovement;
  playingBlockYMovement = (playingBlockWeight * playingBlockYAcceleration) + playingBlockYMovement;
  playingBlockY = playingBlockY + playingBlockYMovement;
  if (gameMode == true) {
    if (frameCount % 10 == 0) { 
      playingBlockXMovement = random(10) - 5;
    }
  }
  if (playingBlockX <= 100) {
    playingBlockX = 100;
    playingBlockXMovement = 0;
  }
  if (playingBlockX >= 210) {
    playingBlockX = 210;
    playingBlockXMovement = 0;
  }
  if (playingBlockY <= 50) {
    playingBlockY = 50;
    playingBlockYMovement =0;
  }
  if (playingBlockY >= 330) {
    playingBlockY = 330;
    playingBlockYMovement = 0;
    playingBlockXMovement = 0;
    playingBlockWeight = 0.1;
    if (playModeRestartFlag == false) {
      println("press R to replace the ball");
      playModeRestartFlag = true;
    }
  }
  if (playingBlockY > 300) {
    playingBlockWeight = 0.4;
  }
  if (playingBlockY + 10 > 105 && playingBlockY < 115 && playingBlockX > 115 && playingBlockX < 145) {
    playingBlockXMovement = 1;
    playingBlockYMovement = 1;
  }
  if (playingBlockY + 10 > 105 && playingBlockY < 115 && playingBlockX > 180 && playingBlockX < 210) {
    playingBlockXMovement = 1;
    playingBlockYMovement = 1;
  }
  if (playingBlockY + 10 > 155 && playingBlockY < 165 && playingBlockX > 147.5 && playingBlockX < 172.5) {
    playingBlockXMovement = 1;
    playingBlockYMovement = 1;
  }
  if (playingBlockY + 10 > 205 && playingBlockY < 215 && playingBlockX > 115 && playingBlockX < 145) {
    playingBlockXMovement = 1;
    playingBlockYMovement = 1;
  }
  if (playingBlockY + 10 > 205 && playingBlockY < 215 && playingBlockX > 180 && playingBlockX < 210) {
    playingBlockXMovement = 1;
    playingBlockYMovement = 1;
  }
  ellipse(playingBlockX, playingBlockY, 20, 20);
  




  ///////////////////////////
  //Panel toggle switches//
  ///////////////////////////

  //Room light

  rectMode(CENTER);
  stroke(0);
  strokeWeight(1);
  fill(150);
  if (roomLight == true) {
    toggleLocation = 330;
  } else if (roomLight == false) {
    toggleLocation = 370;
  }
  rect(toggleLocation, 170, 10, 25);

  //Game Mode

  if (gameMode == true) {
    toggleLocation = 330;
  } else if (gameMode == false) {
    toggleLocation = 370;
  }
  rect(toggleLocation, 280, 10, 25);

  //Rave Mode

  if (raveMode == true ) {
    toggleLocation = 330;
    //delay(100);
  } else if (raveMode == false) {
    toggleLocation = 370;
    //delay(100);
  }
  rect(toggleLocation, 330, 10, 25);

  //Board Colours - RED

  if (mouseX >315 && mouseX < 338 && mouseY > 215 && mouseY < 245 && mousePressed) {
    redToggle = mouseY;
  }
  redToggleValue = (-1 * (redToggle - 245)) * 8.5;
  fill(redToggleValue, 0, 0);
  rect(320, redToggle, 20, 5);

  //Board Colours - GREEN

  if (mouseX >338 && mouseX < 361 && mouseY > 215 && mouseY < 245 && mousePressed) {
    greenToggle = mouseY;
  }
  greenToggleValue = (-1 * (greenToggle - 245)) * 8.5;
  fill(0, greenToggleValue, 0);
  rect(350, greenToggle, 20, 5);

  //Board Colours - BLUE

  if (mouseX >361 && mouseX < 400 && mouseY > 215 && mouseY < 245 && mousePressed) {
    blueToggle = mouseY;
  }
  blueToggleValue = (-1 * (blueToggle - 245)) * 8.5;
  fill(0, 0, blueToggleValue);
  rect(380, blueToggle, 20, 5);

  ///////////////
  //Board Glass//
  ///////////////

  rectMode(CORNERS);
  fill(200, 200, 255, 100);
  rect(90, 50, 225, 285, 20);
}




//////////////////////////////////////////////////
////////////////////Layer Four////////////////////
//////////////////////////////////////////////////

void layerFour() {
  noStroke();

  //Rave spot lights

  if (raveMode == true) {
    radiantLight(200 + 100 * sin(frameCount * 0.2), 50, 50, 200 + 100 * sin(frameCount * 0.5), 200 + 100 * cos(frameCount * 0.5), 50 + 50 * sin(frameCount), 50 + 50 * cos(frameCount), 10, 20);
    radiantLight(50, 200 + 100 * cos(frameCount * 0.2), 50, 200 + 100 * cos(frameCount * 0.5), 200 + 100 * sin(frameCount * 0.5), 50 + 50 * cos(frameCount), 50 + 50 * sin(frameCount), 10, 20);
    radiantLight(50, 50, 200 + 100 * sin(frameCount * 0.2), 200 + 100 * sin(frameCount * 0.5), 200 + 100 * tan(frameCount * 0.5), 50 + 50 * cos(frameCount), 50 + 50 * cos(frameCount), 10, 20);
  }

  //Low light ambience

  if (roomLight == false && raveMode == false) {
    roomAmbience(100, 100, 255);
  }  

  //Rave mode ambience

  if (raveMode == true) {
    if (raveModeFlag == false) {
      redToggle = 245;
      greenToggle = 245;
      blueToggle = 245;
      raveModeFlag = true;
    }
    red = redToggleValue;
    green = greenToggleValue;
    blue = blueToggleValue;
    if (millis() % 3000 <= 1000) {
      red = 100 + red;
    } else if (millis() % 3000 <= 2000 &&  millis() % 3000 > 1000) {
      green = 100 + green;
    } else if (millis() % 3000 > 2000) {
      blue = 100 + blue;
    }
    roomAmbience(red, green, blue);
  }
  if (raveMode == false) {
    raveModeFlag = false;
  }
}


/////////////////////////////////////////////////
////////////////////Functions////////////////////
/////////////////////////////////////////////////

/////////////////
//Room ambience//
/////////////////

void roomAmbience(float red, float green, float blue)
{

  rectMode(CENTER);
  noStroke();
  fill(red, green, blue, 50);
  rect(200, 200, 400, 400);
}
/////////////////
//Radiant Light//
/////////////////

void radiantLight(float red, float green, float blue, float locX, float locY, float lightWidth, float lightLength, float size, int rayWidth) {



  int i = 0;
  while (i<size) {
    fill(red, green, blue, 30-i);
    ellipse(locX, locY, lightWidth+rayWidth*i, lightLength+rayWidth*i);
    i++;
  }
}


//referenced from collect_a_thon
//http://www-acad.sheridanc.on.ca/PROG14998/2017/interactive-toy/christopher_hewson_interactive_toy/index.html

void keyPressed() {
  if ((key == 'W' || key =='w') && playingBlockWeight > -0.1 && playingBlockY < 300) {
    playingBlockWeight = playingBlockWeight - 0.1;
  }
  if ((key == 's' || key == 'S') && playingBlockWeight < 0.4 && playingBlockY < 300) {
    playingBlockWeight = playingBlockWeight + 0.1;
  }
  if ((key == 'a' || key == 'A') && playingBlockY < 300 && gameMode == false) {
    playingBlockXMovement = playingBlockXMovement - 1;
  }
  if ((key == 'd' || key == 'D') && playingBlockY < 300 && gameMode == false) {
    playingBlockXMovement = playingBlockXMovement + 1;
  }
  if (key == 'r' || key == 'R') {
    playingBlockX = 162.5;
    playingBlockY = 50;
    playingBlockYMovement = 0;
    playingBlockXMovement = 0;
    playModeRestartFlag = false;
  }
}