Your browser does not support the canvas tag.

previous        Show / Hide Source        Download        next
//Tibi Leonov
//Assignment 3 - Object Interactive Toy
//Meteor Shower
//Dodge the incoming meteors with the rocket

int numberOfMeteors=10;
Meteor[] meteors=new Meteor[numberOfMeteors];
Ship ship;


void setup() {
  //make a window sized 600 by 600
  size(600, 600);
  //set framerate
  frameRate(60);
  //cursor does not show to avoid obstructing the view of the rocket
  noCursor();
  
  //create the user controlled ship
  ship = new Ship();
  
  //starts the meteor shower
  createMeteorShower();
}

void draw() {
  //sets black background
  background(0);
  
  //updates the position of the meteors
  for (int i=0; i<meteors.length; i++) {
    meteors[i].update();
  }

  //displays meteors
  for (int i=0; i<meteors.length; i++) {
    meteors[i].display();
  }
  //displays the target area the player has to dodge
  for (int i=0; i<meteors.length; i++) {
    meteors[i].target();
  }
  //displays the ship
  ship.display();
}


//starts the meteor shower at the start such that all the meteors dont spawn at the same time
void createMeteorShower() {

  meteors[0]=new Meteor();
  int lastSpawn = millis();
  for (int i=1; i<meteors.length; ) {
    int timer =millis();
    if (timer - lastSpawn >200) {
      meteors[i]=new Meteor();

      i++;
      lastSpawn = millis();
    }
  }
}
class Meteor {

  PVector position=new PVector();
  PVector velocity=new PVector();
  float meteorTop, meteorBottom, meteorLeft, meteorRight;
  float meteorSize; 
  float xTarget, yTarget;
  float rangeTimerStart, rangeTimerEnd;


//initializes each meteor
  Meteor() {
    init();
  }


//gives a velocity and position to each meteor
  void init() {
    noStroke();
    fill(100);

    //the meteors move fropm the center of the screen towards the sides as the rocket passes them
    velocity.x=random(-2.5, 2.5);
    velocity.y=random(-2.5, 2.5);
    position.x=300+10*velocity.x;
    position.y=300+10*velocity.y;
    
    //the red target area to dodge predects where the meteor will be in order to dodge
    xTarget = position.x+120*velocity.x;
    yTarget = position.y+120*velocity.y;
    meteorSize = 70+random(40);
    rangeTimerStart=millis();
  }


//updates the location of the meteors
  void update() {


    position.add(velocity);
    rangeTimerEnd = millis();

    //checks if the meteor reached the target area predicted 
    if (position.x>600 ||position.x<0 ||position.y>600 ||position.y<0 ||rangeTimerEnd-rangeTimerStart>2000||(xTarget==position.x && yTarget==position.y)) {
      meteorTop=yTarget-meteorSize/2; 
      meteorBottom=yTarget+meteorSize/2; 
      meteorLeft=xTarget-meteorSize/2; 
      meteorRight=xTarget+meteorSize/2;

      //checks if the rocket has collided with the meteor in the target area
      if ( sqrt(sq(yTarget-mouseY)+ sq(xTarget-mouseX))< meteorSize/2 ) {
        println("You got hit!");
      }

      //another meteor is initiated after the previous one hit the ship or passed it
      init();
    }
  }
  
  //function displays the meteors and their proper target area to dodge
  void display() {
    //using range timer to fade the meteors in vision as the rocket approaches them
    rangeTimerEnd = millis();
    
    //displays the meteors
    strokeWeight(1);
    stroke(0);
    fill(100, 100, 100, (rangeTimerEnd-rangeTimerStart)/2);
    ellipse( position.x, position.y, meteorSize, meteorSize);

  }



    //displays the target area where the meteor will pass the ship
  void target() {

    noStroke();
    fill(100, 0, 0, 85);
    ellipse( xTarget, yTarget, meteorSize, meteorSize);
  }
}
class Ship {


  float xShip;
  float yShip;

  Ship() {
    
  }


  //displays the ship
  void display() {

    //ship follows the mouse
    xShip = mouseX;
    yShip = mouseY;

    //ship head
    noStroke();
    fill(200);
    ellipse(xShip, yShip, 40, 40);
    
    //ship body
    stroke(200);
    strokeWeight(40);
    line(xShip, yShip, (xShip-300)*1.3+300, ((yShip-300)*1.3+300));
    
    //ship engine
    noStroke();
    fill(150);
    ellipse(((xShip-300)*1.3+300), ((yShip-300)*1.3+300), 50, 50);

    //ship fuel burning
    fill(200, 200, 0);
    ellipse(((xShip-300)*1.32+300), ((yShip-300)*1.32+300), 30, 30);
    fill(200, 150, 0);
    ellipse(((xShip-300)*1.35+300), ((yShip-300)*1.35+300), 15, 15);
  }
}