Your browser does not support the canvas tag.

previous        Show / Hide Source        Download        next
/*
Smashed by Suraj Sirohi
 
 Use arrrow keys, left and right to move the hook horizontally and up and down for vertical movement.
 
 Try to hook the reddish box, once hooked the box will turn dark.
 
 Press Shift to drop the box anywhere (Once Hooked).
 
 Dropping the box directly aboive the person will smash the person.
 
 After the person is crushed under the box you can pick up the box again, the person will respawn.
 
 */



float cranePosX = 200 ;
float craneSpeed = 6;
float craneStringEndPoint = 150;
float boxPosX = 300, boxPosY = 340;
float fallingSpeed = 2;
float personPosX = 200, personPosY = 350;
float boomEffectRadius;
float boomEffectAlpha = 255;
float bloodDropPosX, bloodDropPosY;
float bloodDropRadius = 15;
float bloodPoolRadius, bloodPoolRadiusExpandSpeed = 2;
float unitsMoved;
float personHeight = 20;
float randomNumber;
float personSpeed = 0.5;

int bloodPoolAlpha = 255;
int hookedColor = 166;

boolean checkForHook = true, Hooked;
boolean doGenerateRandomNumber;
boolean personAlreadySmashed;
boolean movePersonBool;
boolean createBloodPoolBool;
boolean randomNumberGenerated;
boolean speak;


void setup() {

  size(400, 400);
}

void draw() {

  background(124, 225, 255);

  //Make cloud pattern
  makeClouds();

  //Draw Ground
  drawGround();

  //Draw Crane Rod
  drawCraneRod();

  //Draw Crane String
  drawCraneString();

  //Draw Person
  drawPerson();



  //Hook the PickUpBox to Crane String when close enough
  lookForHook();

  //Move the PickUpBox wth crane when Hooked
  postHooked();

  //Generate a random number once
  generateRandomNumber();

  //Make the PickUpBox Fall
  boxDrop();

  //Reset The smash bool
  resetSmashingBooleans();


  //Animate Respawning
  respawnPerson();


  //Call screateBloodPool function
  if (createBloodPoolBool) {

    createBloodPool();
  }

  //Spread The Blood
  spreadBlood();

  //Draw Crane Box
  Crane();

  //Draw Blue Box
  drawPickUpBox();

  //Constraint Y-position of PickUpBox
  constraintBoxPosition();


  //Smash the person when box falls directly on it.
  startSmashPersonProcess();


  //Reset Smashing
  resetSmash();

  //Move Person In Random Directions
  if (movePersonBool && unitsMoved < 40) {

    movePerson();
    unitsMoved+=0.1;
  }

  //Make Person Speak
  makePersonSpeak();
}

void keyPressed() {

  if (key == CODED) {
    if (keyCode == LEFT && cranePosX > 169) {
      cranePosX -= craneSpeed;
    } else if ( keyCode == RIGHT) {
      cranePosX += craneSpeed;
    }

    if (keyCode == UP && craneStringEndPoint > 120) {
      craneStringEndPoint -= craneSpeed;
    } else if (keyCode == DOWN && craneStringEndPoint < 340) {
      craneStringEndPoint += craneSpeed;
    }

    if (keyCode == SHIFT) {
      Hooked =false;
      hookedColor=166;
    }
  }
}

//Draw Ground
void drawGround() {

  rectMode(CORNER);
  fill(100, 100, 0);
  rect(0, 360, 400, 50);
}

//Draw Crane Long rod
void drawCraneRod() {

  strokeWeight(9);
  stroke(252, 237, 79);
  line(64, 324, cranePosX, 70);
}

//Draw Crane Strings
void drawCraneString() {

  //Strings
  strokeWeight(2);
  line(cranePosX -3, 70, cranePosX-3, craneStringEndPoint - 20);
  line(cranePosX +3, 70, cranePosX+3, craneStringEndPoint - 20);

  noStroke();
  //Joint Circle
  fill(252, 237, 79);
  ellipse(cranePosX -2, 72, 15, 15);

  //Hook Base
  quad(cranePosX -2, craneStringEndPoint -22, cranePosX +2, craneStringEndPoint -22, cranePosX +5, craneStringEndPoint -16, cranePosX -5, craneStringEndPoint-16);
  quad(cranePosX -5, craneStringEndPoint - 16, cranePosX + 5, craneStringEndPoint -16, cranePosX + 2, craneStringEndPoint - 1, cranePosX - 2, craneStringEndPoint - 1);

  //Hook
  stroke(252, 237, 79);
  noFill();
  arc(cranePosX, craneStringEndPoint + 7, 15, 15, PI + 1.6, 2*PI + 2);
}

//Draw the Crane Box
void Crane() {

  rectMode(CORNER);
  noStroke();

  //Yellow Back Box
  fill(252, 237, 79);
  rect(20, 312, 50, 40);

  //Black Front Box
  fill(40);
  rect(30, 302, 40, 40);

  //Sky Color Window
  fill(149, 231, 255);
  rect(35, 307, 30, 30);

  //Black Wheels
  strokeWeight(15);
  stroke(40);
  line(20, 355, 70, 355);

  //Inner Wheels
  strokeWeight(7);
  stroke(100);
  line(20, 355, 70, 355);
  noStroke();
}

//Draw Box to be picked up by Crane
void drawPickUpBox() {

  //Main Box
  rectMode(CENTER);
  fill(hookedColor, 113, 142);
  rect(boxPosX, boxPosY, 50, 40);


  //In-Between Lines
  stroke(hookedColor, 59, 126);
  strokeWeight(4);
  line(boxPosX - 24, boxPosY -18, boxPosX - 24, boxPosY +18);
  line(boxPosX - 12, boxPosY -18, boxPosX - 12, boxPosY +18);
  line(boxPosX - 2, boxPosY -18, boxPosX - 2, boxPosY +18);
  line(boxPosX + 8, boxPosY -18, boxPosX + 8, boxPosY +18);
  line(boxPosX + 22, boxPosY -18, boxPosX + 22, boxPosY +18);
  noStroke();
}


//Draw Humanoid Character
void drawPerson() {

  noStroke();

  //Main Rect
  fill(153, 188, 104);
  rectMode(CENTER);
  rect(personPosX, personPosY, 15, personHeight);

  //Pants
  fill(245, 128, 70);
  rect(personPosX, personPosY + 6, 15, 10);
  stroke(126, 66, 38);
  line(personPosX, personPosY, personPosX, personPosY +10);
  noStroke();

  //Head
  fill(255, 227, 82);
  ellipse(personPosX, personPosY - 15, personHeight/1.2, personHeight/1.2);
  fill(103, 76, 19);
  arc(personPosX, personPosY - 18, personHeight/1.2, personHeight/1.2, PI, 2*PI);
}

//Mask the reddish blood pool
void drawBloodPoolMask() {

  fill(124, 225, 255);
  rect(personPosX, personPosY, 220, 20);
}

//Crush the person under the box
void smashPerson() {

  personHeight = 0;

  fill(250, 130, 80, boomEffectAlpha);
  ellipse(personPosX, personPosY, boomEffectRadius, boomEffectRadius);
  boomEffectRadius += 10;
  boomEffectAlpha -= 10;

  fill(255, 0, 0);
  ellipse(personPosX + bloodDropPosX, personPosY + bloodDropPosY, bloodDropRadius, bloodDropRadius);
  ellipse(personPosX + bloodDropPosX + 20, personPosY + bloodDropPosY + 7, bloodDropRadius, bloodDropRadius);
  ellipse(personPosX + bloodDropPosX - 20, personPosY + bloodDropPosY + 2, bloodDropRadius, bloodDropRadius);
  bloodDropPosY -= 4;
  bloodDropRadius -= 0.7;
}

//Create Blood pool that spreads after person is smashed
void createBloodPool() {

  fill(232, 93, 75, bloodPoolAlpha);
  ellipse(personPosX, personPosY + 10, bloodPoolRadius, 10); 

  if (bloodPoolRadiusExpandSpeed > 0) {
    bloodPoolRadius += bloodPoolRadiusExpandSpeed;
  }

  bloodPoolRadiusExpandSpeed -= 0.01;
}

//Reset values after smashing
void resetSmashing() {

  personAlreadySmashed = true;
  boomEffectAlpha = 255;
  boomEffectRadius = 0;
  bloodDropRadius = 15;
  bloodDropPosY = 0;
  unitsMoved = 0;
  randomNumberGenerated=false;
}

//Make person move in random direction
void movePerson() {

  if (personPosX > 149 && personPosX < 385) {

    if (randomNumber > 1) {

      personPosX += personSpeed;
    } else {

      personPosX -= personSpeed;
    }
  } else if (personPosX <= 149) {

    personPosX += personSpeed;
  } else if (personPosX >= 185) {

    personPosX -= personSpeed;
  }
}

//Print a statement when perons is crushed
void personSpeak() {

  println("Ouch!");
}

//Make CLoud Pattern
void makeClouds() {

  for (int k = 0; k < 9; k++) {

    for (int j =0; j < 4; j++) {

      for (int i = 0; i < 4; i++) {

        fill(152, 251, 255);
        ellipse(40 * i/1.7 + k *120 + j*35 - 90, 20 + j * 50, 30, 25);
      }
    }
  }
}

void lookForHook() {

  if (abs(boxPosX - cranePosX) < 4 && abs(craneStringEndPoint - boxPosY) < 4 && checkForHook) {

    hookedColor = 120;
    Hooked = true;
    checkForHook =false;
  }
}

void postHooked() {

  if (Hooked) {

    boxPosX = cranePosX;
    boxPosY = craneStringEndPoint;

    if (!randomNumberGenerated) {

      doGenerateRandomNumber = true;
    }
  }
}

void generateRandomNumber() {

  if (doGenerateRandomNumber) {

    randomNumber = random(2);
    doGenerateRandomNumber = false;
    randomNumberGenerated = true;
  }
}

void boxDrop() {

  if (!Hooked && boxPosY < 338) {
    boxPosY += fallingSpeed;
    fallingSpeed += 0.25;
  }
}

void resetSmashingBooleans() {

  if (boxPosY < 300) {

    personAlreadySmashed = false;
    movePersonBool = true;

    if (bloodPoolAlpha >0) {
      bloodPoolAlpha -= 8;
    } else {

      bloodPoolRadius = 0;
      bloodPoolRadiusExpandSpeed = 2;
    }
  }
}
void respawnPerson() {

  if (personHeight < 20 && boxPosY < 300) {

    personHeight++;
  }
}

void constraintBoxPosition() {

  if (boxPosY >= 338) {
    checkForHook =true;
    fallingSpeed =2;
    boxPosY =340;
    movePersonBool =false;
  }
}

void startSmashPersonProcess() {

  if (abs(personPosX - boxPosX) < 26 && abs(personPosY - boxPosY) < 15 && !personAlreadySmashed && !Hooked) {

    bloodPoolAlpha=255;
    smashPerson();
    personSpeak();
  }
}

void resetSmash() {

  if (boomEffectAlpha < 5) {

    resetSmashing();
  }
}

void makePersonSpeak() {
  if (speak) {

    personSpeak();
    speak = false;
  }
}

void spreadBlood() {
  if (personAlreadySmashed) {

    createBloodPoolBool =true;
    drawBloodPoolMask();
  }
}