Your browser does not support the canvas tag.

previous        Show / Hide Source        Download        next
/*
    Elite Space Cargo Transport Simulator Extreme Revolution Reborn Remix - Maximum Legendary Spacey Edition 
 By Robert Orange
 
 Notice: Not realistic space physics. Plz don't be triggered. Thank
 -------------------------------
 Controls:
 Use Mouse Cursor to Control Spaceship's Speed and Position.
 
 Faster Speed uses Power.
 Hp is represented by Cargo pods on the ship.
 ------------------------------
 Pseudo Code
 +If Player Moves the mouse up, background will move go faster, and 
 the ship will use power but the enemy will get further from the player.
 +If the player moves the mouse down, the background will move slower, and
 the ship will recharge power but the enemy will get closer to the player.
 +If the player moves the mouse right, the background's left acceleration will increase, and
 right acceleration will decrease.
 +If the player moves the mouse left, the background's right acceleration will increase, and
 left acceleration will decrease.
 -removed-If the player clicks the mouse, shoot a laser and use power.
 +If the ship collides with an asteroid, player takes damage and slows down slightly.
 -removed-If the ship collides with a missile, player takes damage and slows down slightly.
 +If the ship has fewer Cargo containers, the ship moves faster.
 +If the enemy reaches the player, the player is defeated.
 
 
 
 */
Spaceship playerShip;
Stars[] arrayOfStars;
SpaceBackground universe;
Asteroids[] arrayOfAsteroids;
//You get an asteroid, you get an asteroid, everyone gets an asteroid!
AsteroidSpawner oprah;
Scoretracker score;

void setup() {
  size(400, 400);
  frameRate(30);
  playerShip = new Spaceship(width/2, height-100, 3, 50, color(150, 50, 150));
  arrayOfAsteroids = new Asteroids[5];
  oprah = new AsteroidSpawner();
  score= new Scoretracker(100);
  universe = new SpaceBackground();
  arrayOfStars = new Stars[100];

  //Sets the values for every star in the array.
  for (int i=0; i<arrayOfStars.length; i+=1) {
    arrayOfStars[i]= new Stars(random(width), random(height), random(0.5, 3), color(random(200, 255), 200, random(200, 255)));
  }
  for (int i=0; i<arrayOfAsteroids.length; i+=1) {
    arrayOfAsteroids[i]= new Asteroids(random(400), 0, random(-3, 3), random(1, 3), 20, 20, false);
  }
}

void draw() {
  background(0);
  if (playerShip.isAlive==true) {
    playerShip.update();
    playerShip.movement();
    oprah.updateAsteroids();
    oprah.spawn();
    score.update();
    //Draws the universe Background.
    //Includes Stars.
    universe.display();
    oprah.displayAsteroids();
    //Draws the player's ship.
    playerShip.display();
    score.display();
  } else {
    drawDeathScreen();
  }
}
void drawDeathScreen() {
  fill(255, 0, 0);
  rectMode(CORNER);
  rect(0, 0, 400, 400);
  fill(255);
  textSize(30);
  text("You Dead", width/4, height/2);
  text("Score "+ round(score.playerDistance), width/4, height/3);
}
class Asteroids {
  boolean isSpawned;
  PVector position;
  PVector velocity;
  PVector asteroidSize;
  Asteroids(float tempX, float tempY, float tempXVelocity, float tempYVelocity, float tempXSize, float tempYSize, boolean tempIsSpawned) {
    position = new PVector(tempX, tempY);
    velocity = new PVector(tempXVelocity, tempYVelocity);
    asteroidSize = new PVector(tempXSize, tempYSize);
    isSpawned=tempIsSpawned;
  }

  void update() {
    //if the asteroid leaves the screen, it is no longer "spawned".
    if (position.y>400||position.x>500||position.x<-100) {
      isSpawned=false;
    }
    //Check for Collision with player. If right side of ship, destroy right cargo, if left side then destroy left cargo. If no cargo exists then just damage player.
    if (isSpawned==true) {
      if (position.x>=(playerShip.xPos-50) && position.x<=(playerShip.xPos+50) && position.y>=(playerShip.yPos-50) && position.y<=(playerShip.yPos+50)) {
        if (position.x>playerShip.xPos && playerShip.isRightCargo==true) {
          playerShip.isRightCargo=false;
          playerShip.cargoRightWeight=0;
        } else if (position.x<playerShip.xPos && playerShip.isLeftCargo==true) {
          playerShip.isLeftCargo=false;
          playerShip.cargoLeftWeight=0;
        } else if (position.x>playerShip.xPos && playerShip.isRightCargo==false && playerShip.isLeftCargo==true) {
          playerShip.isLeftCargo=false;
          playerShip.cargoLeftWeight=0;
        } else if (position.x<playerShip.xPos && playerShip.isRightCargo==true && playerShip.isLeftCargo==false) {
          playerShip.isRightCargo=false;
          playerShip.cargoRightWeight=0;
        }
        isSpawned=false;
        playerShip.health-=1;
        playerShip.dampening=3;
      }
    }
  }
  void display() {
    //Only draw the asteroid if it is spawned.
    if (isSpawned==true) {
      position.add(velocity);
      position.x+=playerShip.horizontalSpeed;
      position.y+=playerShip.verticalSpeed;
      fill(200, 100, 50);
      ellipse(position.x, position.y, asteroidSize.x, asteroidSize.y);
    }
  }
}
class AsteroidSpawner {
  int waitTime;
  boolean spawnedAsteroid;
  AsteroidSpawner() {
    spawnedAsteroid=false;
    waitTime=round(random(60));
  }
  void updateAsteroids() {
    for (int i=0; i<arrayOfAsteroids.length; i+=1) {
      arrayOfAsteroids[i].update();
    }
  }
  void displayAsteroids() {
    for (int i=0; i<arrayOfAsteroids.length; i+=1) {
      arrayOfAsteroids[i].display();
    }
  }
  void spawn() {
    if (waitTime<=0) {
      spawnedAsteroid=false;
      for (int i=0; i<arrayOfAsteroids.length; i+=1) {
        if (spawnedAsteroid==false) {
          if (arrayOfAsteroids[i].isSpawned==false) {
            arrayOfAsteroids[i]= new Asteroids(random(400), 0, random(-4, 4), random(1, 4), 20, 20, true);
            spawnedAsteroid=true;
            waitTime=round(random(60));
          }
        }
      }
    } else {
      waitTime-=1;
    }
  }
}
class PVector {

  float x;
  float y;

  PVector(float tempX, float tempY) {
    x = tempX;
    y = tempY;
  }
  void add(PVector v) {
    y = y + v.y;
    x = x + v.x;
  }
}
class Scoretracker{
  
  float playerDistance,enemyDistance;
  
  Scoretracker(int startingDistance){
    playerDistance=startingDistance;
    enemyDistance=0;
  
  
  }
  void update(){
    playerDistance+=playerShip.verticalSpeed;
    enemyDistance+=5.5;
    if(enemyDistance>=playerDistance){
      playerShip.isAlive=false;
    }
  }
  void display(){
  fill(255);
  rect(10,10,390,15);
  ellipse(390,12.5,10,10);
  fill(255,0,0);
  ellipse(10+(380*(enemyDistance/playerDistance)),12.5,10,10);
  //The screen to become red as the enemy gets closer.
  fill(255,0,0,0+(255*((enemyDistance/2)/playerDistance)));
  rect(0,0,400,400);
  
  }
}
class SpaceBackground {


  SpaceBackground() {
  }
  void display() {
    //If the stars leave the screen, make new stars at the opposite side of the screen.
    for (int i=0; i<arrayOfStars.length; i+=1) {
      if (arrayOfStars[i].yPos>height) {
        arrayOfStars[i]= new Stars(random(width), 0, random(0.5, 3), color(random(200, 255), 200, random(200, 255)));
      }
      if (arrayOfStars[i].xPos>width) {
        arrayOfStars[i]= new Stars(0, random(height), random(0.5, 3), color(random(200, 255), 200, random(200, 255)));
      }
      if (arrayOfStars[i].xPos<0) {
        arrayOfStars[i]= new Stars(width, random(height), random(0.5, 3), color(random(200, 255), 200, random(200, 255)));
      }
      arrayOfStars[i].display();
    }
  }
}
class Spaceship {
  int xPos;
  int yPos;
  int health;
  float energy, maxEnergy, energyPercentage;
  color shipColour;
  //Speed Variables
  float verticalSpeed;
  float horizontalSpeed;
  float verticalAcceleration;
  float horizontalAcceleration;
  float cargoRightWeight;
  float cargoLeftWeight;
  float dampening;
  boolean isRightCargo=true;
  boolean isLeftCargo=true;
  boolean isAlive=true;
  Spaceship(int tempXPos, int tempYPos, int tempHealth, float tempEnergy, color tempColour) {
    xPos=tempXPos;
    yPos=tempYPos;
    health=tempHealth;
    maxEnergy=tempEnergy;
    energy=maxEnergy;
    shipColour=tempColour;
    cargoRightWeight=1.2;
    cargoLeftWeight=1.2;
    constrain(horizontalSpeed, -10, 10);
    constrain(verticalSpeed, 0, 10);
  }
  void update() {
    //If the player's health is zero, they are dead.
    if (health<=0) {
      isAlive=false;
    }
    //Update Energy Consumption
    float energyConsumption;
    energyConsumption=1-(verticalAcceleration);
    energyPercentage=(energy/maxEnergy);
    //println(energyConsumption);
    energy=energy+energyConsumption;
    if (energy>=maxEnergy) {
      energy=maxEnergy;
    }
    if (energy<=0) {
      energy=0;
    }
  }
  void movement() {
    //The higher the mouse position is on the screen, the higher the vertical acceleration.
    dampening-=.2;
    verticalAcceleration=(200-mouseY)/100.00;
    if (verticalAcceleration<=0) {
      verticalAcceleration=0;
    }
    //Max Speed is determined by mouse position and cargo weight.
    float maxSpeed=constrain(((200-mouseY)/10.00)-(cargoRightWeight+cargoLeftWeight), 1, 20);
    if (energy<=0) {
      verticalSpeed=1;
    } else {
      //Vertical Speed is between 1 and the max speed.
      verticalSpeed=constrain(verticalSpeed+verticalAcceleration-dampening, 1, maxSpeed);
    }
    //println(verticalAcceleration);
    //println(verticalSpeed);
    //println((200-mouseY)/10.00);
    horizontalAcceleration=(200-mouseX)/100.00;
    //Horizontal Speed can go both directions, and there's drift for each cargo pod.
    horizontalSpeed=constrain(horizontalSpeed+horizontalAcceleration-cargoLeftWeight+cargoRightWeight, -10, 10);
  }
  void display() {
    rectMode(CENTER);
    ellipseMode(RADIUS);
    noStroke();
    //Thruster Fires
    //Appear larger when player is accelerating faster.
    fill(255, 255, 0);
    ellipse(xPos+15, yPos+25+(verticalAcceleration*2), 5, random(15+verticalAcceleration, 20+verticalAcceleration));
    ellipse(xPos-15, yPos+25+(verticalAcceleration*2), 5, random(15+verticalAcceleration, 20+verticalAcceleration));
    //SHIP
    fill(100);
    rect(xPos+10, yPos-30, 5, 30);
    rect(xPos+15, yPos+25, 10, 30);
    rect(xPos-15, yPos+25, 10, 30);
    fill(shipColour);
    ellipse(xPos, yPos-20, 10, 30);
    ellipse(xPos, yPos, 20, 40);
    triangle(xPos+10, yPos-10, xPos+30, yPos-10, xPos+30, yPos+40);
    triangle(xPos-10, yPos-10, xPos-30, yPos-10, xPos-30, yPos+40);
    fill(0, 0, 255);
    triangle(xPos, yPos-40, xPos+5, yPos-40, xPos, yPos-45);
    triangle(xPos, yPos-40, xPos-5, yPos-40, xPos, yPos-45);
    //RightCARGO
    if (isRightCargo==true) {
      fill(90);
      rect(xPos+40, yPos+20, 20, 60);
      fill(50);
      rect(xPos+40, yPos+20, 10, 50);
      for (int i=10; i<50; i+=10) {
        fill(90);
        rect(xPos+40, yPos-5+i, 20, 2.5);
      }
    }

    //LeftCARGO
    if (isLeftCargo==true) {
      fill(90);
      rect(xPos-40, yPos+20, 20, 60);
      fill(50);
      rect(xPos-40, yPos+20, 10, 50);
      for (int i=10; i<50; i+=10) {
        fill(90);
        rect(xPos-40, yPos-5+i, 20, 2.5);
      }
    }

    //Energy Bar
    rectMode(CORNERS);
    fill(30);
    rect(xPos-5, yPos+20, xPos+5, yPos-20);
    fill(200, 200, 0);
    rect(xPos-5, yPos+20, xPos+5, yPos+20-(40*energyPercentage));
  }
}
class Stars {
  float xPos;
  float yPos;
  float size;
  color starColor;
  Stars(float tempXPos, float tempYPos, float tempSize, color tempStarColor) {
    xPos=tempXPos;
    yPos=tempYPos;
    size=tempSize;
    starColor=tempStarColor;
  }
  void display() {
    fill(starColor);
    //The stars move depending on player's speed.
    yPos=yPos+playerShip.verticalSpeed;
    xPos=xPos+playerShip.horizontalSpeed;
    float flicker=random(2);
    ellipse(xPos, yPos, size, size);
    fill(starColor, 100);
    ellipse(xPos, yPos, size+flicker, size+flicker);
  }
}