import java.applet.*; 
import java.awt.*; 
import java.awt.image.*; 
import java.awt.event.*; 
import java.io.*; 
import java.net.*; 
import java.text.*; 
import java.util.*; 
import java.util.zip.*; 

public class starPlane4 extends BApplet {

// star plane study 3
//
// space bar toggles fill
//

float a, b;
float xMouse, yMouse;

int totalPlanets = 1100;   //1140

Planet[] planet = new Planet[totalPlanets];

float rotationVar;

boolean click;
boolean boxFill;
boolean illuminate;

void setup(){
  size(800,400);
  background(0);
  
  for (int i=0; i<totalPlanets; i++){
    planet[i] = new Planet(i, i * 0.01f, (i+1) * .0001f, (i+1) * .02f, (i+1) * .10f);
  }
}

void loop(){
  xMouse -= (xMouse - (width/2 - mouseX)) * .2f;
  yMouse -= (yMouse - (height/2 - mouseY)) * .2f;
  
  if (mousePressed){
    click = true;
  } else {
    click = false;
  }
  
  translate(width/2, height/2, -2000);
  
  if (click){
    a -= (a - 0) * .2f;
    b -= (b - HALF_PI) * .2f;
    
    rotationVar -= (rotationVar - ((width/2 - mouseX) * 4.0f)) * .2f;
  } else {
    a += xMouse / 5000.0f;
    b += yMouse / 5000.0f;
    
    if (a > PI){
      a -= TWO_PI;
    } else if (a < -PI){
      a += TWO_PI;
    }
    
    if (b > PI){
      b -= TWO_PI;
    } else if (b < -PI){
      b += TWO_PI;
    }
  }
  
  rotateX(b);
  rotateY(a);
  
  for (int i=0; i<totalPlanets; i++){
    push();
    planet[i].move();
    pop();
  }
}

void keyReleased(){
  if (key == ' '){
    if (boxFill){
      boxFill = false;
    } else {
      boxFill = true;
    }
  }
  
  if (key == 'l'){
    if (illuminate){
      illuminate = false;
      noLights();
    } else {
      illuminate = true;
      lights();
    }
  }
}



class Planet{
  float orbit;
  float speed;
  float dist;
  float mass;
  
  float sVar = 50.0f;
  
  float alt1, alt2;
  
  int index;
  
  Planet(int i, float o, float s, float d, float m){
    orbit = o;
    speed = s;
    dist = (d * 100.0f);
    mass = m;
    
    index = i + 1;
  }
  
  void move(){
    orbit += ((rotationVar * speed) / 500.0f);
    if (orbit > PI){
      orbit -= TWO_PI;
    } else if (orbit < -PI){
      orbit += TWO_PI;
    }
    
    rotateZ(orbit);
    
    alt1 = 75;
    alt2 = 25;
    
    translate(dist,0,-100);
    render(0, mass, 222, 222, 197);
    
    translate(0,0,alt2);
    render(0, mass/2.0f, 0, 150, 100);
    
    translate(0,0,alt1);
    render(1, mass, 105, 0, 0);
    
    translate(0,0,alt1);
    render(0, mass/2.0f, 0, 150, 100);
    
    translate(0,0,alt2);
    render(0, mass, 222, 222, 197);
  }
  
  void render(int type, float scaleVar, float r, float g, float b){
    if (boxFill){
      noStroke();
      fill(r, g, b);
    } else {
      noFill();
      stroke(r, g, b);
    }
    
    if (type == 0){
      rect(-(scaleVar*0.5f), -(scaleVar*0.5f), scaleVar*1.0f, scaleVar*1.0f);
    } else {
      box(scaleVar/3.0f, scaleVar/3.0f, scaleVar);
    }
  }
}

}