Your browser does not support the canvas tag.

previous        Show / Hide Source        Download        next
//Dogfighter, Sasha Ayagh, Interactive Toy
//To engage with the interactivity, press the mouse button to shoot the enemy
//This will create a very basic explosion effect over the foe
//It is presented in a first person view within a fighter jet cockpit
//Player can move around a crosshair with their mouse, press mous to interact
//Includes random speed for the plane and a looping cloud
//I used PAL's and Daniel Shiffmans videos as a reference to understand some problems
//The original intent was to have the plane be temporarily destroyed by placing..
//crosshair over the enemy plan and pressing mouse to have it dissapear
//however due to difficultys this idea was scraped for a simpler version of it

//variables for objects
float circleX;
float circletwoX;
float wingoneX;
float wingtwoX;
float exhuastX;
float exhuasttwoX;
float finX;
float fintwoX;
float cockpitX;
float explosionX;

//variables for looping cloud
float dynamiccloudsX; 

//variables for hitbox
float hitL, hitR, hitT, hitB;

//logic variables for speed

//random speed for enemy plane so speed is diffirent every time
float xspeed = random(50);

//set up and x,y positioning for moving shapes to start at
void setup() {
  size(400, 400);
  circleX = 0;
  circletwoX = 32;
  wingoneX = 48;
  wingtwoX =-80;
  exhuastX = 0;
  exhuasttwoX = 32;
  finX = 0;
  fintwoX = 32;
  cockpitX = 15;
  dynamiccloudsX = 0;
}

//basic setup for drawing
void draw() {
  background(0, 253, 255);
  noCursor(); 
  //functions for objects
  drawClouds();
  drawPlayerHUD();
  drawHostileaircraft();
  drawCrosshair();

  //create "explosion" effect everytime the player fires at the enemy
  if (mousePressed) {
    drawExplosionfromshooting();
    println("ENGAGING ENEMY");
  }
  //set speed for objects
  circleX = circleX + xspeed;
  circletwoX = circletwoX + xspeed;
  wingoneX = wingoneX + xspeed;
  wingtwoX = wingtwoX + xspeed;
  exhuastX = exhuastX + xspeed;
  exhuasttwoX = exhuasttwoX + xspeed;
  finX = finX + xspeed;
  fintwoX = fintwoX + xspeed;
  cockpitX = cockpitX + xspeed;
  explosionX = explosionX + xspeed;
  
  //loop cloud
  while(dynamiccloudsX < width) {
    dynamiccloudsX = dynamiccloudsX + 10;
    fill(255);
    noStroke(); 
    ellipse(dynamiccloudsX, height/5, 60, 30);
  }

  //getting the enemy plane to go back and forth
  if (circleX > width) {
    xspeed = -3;
  }
  if (circleX < 0) {
    xspeed = 10;
  }
}


//code for functions
void drawClouds() {
  
  //draw the static clouds
  noStroke();
  fill(255);
  ellipse(200, 50, 60, 30);
  ellipse(50, 200, 60, 30);
  ellipse(400, 150, 60, 30);
  ellipse(170, 50, 60, 30);
  ellipse(0, 50, 60, 30);
  ellipse(350, 200, 60, 30);
}
void drawPlayerHUD() {
  
  //draw player cockpit/HUD
  fill(0);
  stroke(0);
  rect(0, 375, 1000, 30);
  rect(132, 250, 5, 800);
  rect(260, 250, 5, 800);
  
  //cockpit HUD
  fill(155);
  rect(132, 350, 132, 200);
  strokeWeight(2.5);
  fill(0, 1, 0, 0);
  rect(132, 250, 132, 80);
  strokeWeight(1.5);
  stroke(26, 154, 43, 155);
  fill(34, 255, 0, 155);
  rect(142, 260, 112, 1);
  rect(152, 280, 95, 1);
  rect(162, 300, 75, 1);
  rect(142, 380, 110, 80);
}
void drawCrosshair() {
  //defining hitboxes for crosshair
  float hitx = mouseX;
  float hity = mouseY;
  float hitw = 1;
  float hith = 15;
  
  //crosshair
  strokeWeight(3);
  stroke(255, 0, 0);
  fill(1, 0, 0, 0);
  //hitbox for crosshair
  rect(hitx, hity, hitw, hith);
  rect(hitx, hity-15, hitw, hith);
  rect(hitx, hity, hitw+14, hith-14);
  rect(hitx-15, hity, hitw+14, hith-14);
  ellipse(hitx, hity, hitw+24, hith+10);
  
  //positioning for crosshair
  hitL=hitx-hitw/2;
  hitR=hitx+hitw/2;
  hitT=hity-hith/2;
  hitB=hity+hith/2;
}

//enemy aircraft function
void drawHostileaircraft() {
  //enemy aircraft
  
  //enemy cockpit
  fill(0, 0, 255);
  stroke(100);
  strokeWeight(2.5);
  ellipse(cockpitX, height/2.1, 32, 32);
  
  //fins
  fill(196, 183, 61);
  stroke(0);
  strokeWeight(1);
  rect(finX, height/2.5, 5, 32);
  rect(fintwoX, height/2.5, 5, 32);
  
  //engines
  fill(155);
  stroke(0);
  ellipse(circleX, height/2, 32, 32);
  ellipse(circletwoX, height/2, 32, 32);
  //main body shapes
  fill(196, 183, 61);
  stroke(0);
  rect(wingoneX, height/2, 64, 5);
  rect(wingtwoX, height/2, 64, 5);
  //exhuast of jet
  fill(219, 253, 0);
  strokeWeight(3);
  stroke(253, 147, 7);
  ellipse(exhuastX, height/2, 16, 16);
  ellipse(exhuasttwoX, height/2, 16, 16);
}

//explosion

void drawExplosionfromshooting() {
  fill(255, 0, 0);
  noStroke();
  ellipse(explosionX, height/2, 270, 80);
}

//fin