Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //global variables
- import peasy.PeasyCam;
- import peasy.*;
- PVector pos;//position of the ship
- float dir = 0;//direction ship is facing
- float speed = 0;//speed of ship
- PVector vel;
- PVector acc;
- PVector sDir;
- PeasyCam cam;
- //====================================================
- boolean leftBool, rightBool, thrustBool;
- float speedLimit = 4;
- //global variables for tracers
- int maxSize = 30;
- float startSize = 30;
- PVector [] post = new PVector [maxSize];
- PVector [] velt = new PVector [maxSize];
- PVector [] acct = new PVector [maxSize];
- float [] sizes = new float [maxSize];
- float shrinkScale = 0.99;
- float velScale = 2;
- int state = 0;
- //-----------------------------------------------------------------------------
- //global variables for asteroid
- Asteroid main;
- ArrayList<Asteroid> asteroids = new ArrayList<Asteroid>();
- PShape model;
- PVector center;
- PImage texture;
- //global variables for star map
- int maxStarSize = 50;//number of stars
- int Smult = 800;
- float starSize = 1;
- PVector [] Spos2 = new PVector [maxStarSize];
- PVector mid;
- //global variables for sounds
- import ddf.minim.*;//needed to play the music
- import ddf.minim.analysis.*;//needed to access the BeatDetect Class
- Minim minim;//the object access all of the other minim goodies
- AudioPlayer song;//the player to play a specific song, multiple players would be needed to play multiple songs
- BeatDetect beat;//the class that will detect the changes in volume associated with beats
- //-------------------------------------void setup-------------------
- void setup() {
- noStroke();
- size(800, 800, P3D);
- textSize(30);
- textAlign(CENTER, CENTER);
- frameRate(120);
- pos = new PVector(width/2, height/2);
- vel = new PVector();
- acc = new PVector();
- sDir = new PVector(0, 1, 0);
- cam = new PeasyCam(this, width/2., height/2., 0., 745);
- mid = new PVector(width/2,height/2,Smult/2);
- for(int i=1; i < maxStarSize; i++){
- Spos2[i] = (PVector.random3D());
- Spos2[i].mult(Smult);
- Spos2[i].add(mid);
- //(pos2[i].x) += (width/2);
- //(pos2[i].y) += (height/2);
- //(pos2[i].z) += (mult/2);
- }
- initT();
- cam.setActive(false);
- }
- //---------------------------------void init--------------------------
- void initT() {
- for (int i = 0; i < maxSize; i++) {
- post[i] = new PVector(-1000, -1000);
- velt[i] = PVector.random2D();
- velt[i].mult(velScale);
- acct[i] = new PVector(0, 0);
- sizes[i] = startSize;
- }
- center = new PVector(width/2, height/2);
- texture = loadImage("texture.jpg");
- model = loadShape("asteroid.obj");
- model.setTexture(texture);
- main = new Asteroid(asteroids, 20., model, center, new PVector());
- asteroids.add(main);
- // asteroids.add(new Asteroid(asteroids, 20., model, new PVector(random(0,width),random(0,height)),new PVector()));
- }
- //-----------------------------void key pressed--------------------------
- void keyPressed() {
- if (keyCode == LEFT) {//turning left
- leftBool = true;
- }
- if (keyCode == RIGHT) {//turning right
- rightBool = true;
- }
- if (keyCode == UP) {//going forward
- thrustBool = true;
- }
- }
- //-----------------------------void key released--------------------------
- void keyReleased() {
- if (keyCode == LEFT) {
- leftBool = false;
- }
- if (keyCode == RIGHT) {
- rightBool = false;
- }
- if (keyCode == UP) {
- thrustBool = false;
- }
- }
- //---------------------------------void draw hud-----------------------
- void drawHUD() {
- float speed = 0;
- cam.beginHUD();
- fill(0, 128);
- rect(0, 0, 800, 80);
- fill(255);
- text("speed:" +speed , 70, 30);
- if(keyPressed && key == UP){
- speed++;
- }
- cam.endHUD();
- }
- //---------------------------------void pre game------------------------
- void preGame() {
- background(150, 100, 50);
- text("", width/2, height/2);
- text("Press space to continue", width/2, height/2+200);
- if (keyPressed && key == ' ') {
- state = 1;
- }
- }
- //-------------------------------------void ship driving------------------------
- void shipDriving() {
- //pos.x += cos(dir)*(speed);//current location + the next"step"
- //pos.y += sin(dir)*(speed);
- vel.add(acc);
- pos.add(vel);
- acc.set(0, 0, 0);
- fill(0);//colour of the ship
- pushMatrix();
- translate(pos.x, pos.y);//moving the ship to the new position
- //rotate(dir);//rotate the ship to face the desired direction
- rotate(sDir.heading());
- stroke(0);//shade of colour for stroke
- box(30, 10, 10);//drawing the ship, which is a box
- popMatrix();
- if (leftBool) {
- //dir -= 0.05;//rotate the ship to the left
- sDir.rotate(-0.05);
- }
- if (rightBool) {
- //dir += 0.05; //rotate the ship to the right
- sDir.rotate(0.1);
- }
- if (thrustBool) {
- //speed += 0.1;
- acc = sDir.copy().mult(0.1);
- } else {
- vel.mult(0.99);
- //speed *= 0.99;//redue the speed over time
- }
- vel.limit(speedLimit);
- //speed = constrain(speed, 0, speedLimit); //limit the speed
- }
- //-------------------------------void off edge----------------
- void offEdge() {
- if (pos.x > width) {
- pos.x = 0;
- } else if (pos.y > height) {
- pos.y = 0;
- } else if (pos.y < 0) {
- pos.y = height;
- } else if (pos.x < 0) {
- pos.x = width;
- }
- }
- //--------------------------void tracers------------------
- void tracers() {
- for (int i = maxSize-1; i > 0; i--) {
- post[i] = post[i-1];
- sizes[i] = sizes[i-1]*shrinkScale;//shrinks tracers as they go through the array
- velt[i] = velt[i-1];
- }
- if (thrustBool) {
- post[0] = new PVector(pos.x, pos.y);
- velt[0] = PVector.random2D();
- velt[0].mult(velScale);
- sizes[0] = startSize;
- } else {//if not pressing thrust button, place tracers off screen
- post[0] = new PVector(-1000, -1000);
- }
- for (int i = 0; i < maxSize; i++) {
- post[i].add(velt[i]);
- pushMatrix();
- translate(cos(dir)*speed, sin(dir)*speed);
- ellipse(post[i].x, post[i].y, sizes[i]/3, sizes[i]/3);
- fill(255,0,0);
- popMatrix();
- }
- }
- //------------------------------------void game------------
- void game() {
- background(255);
- shipDriving();
- tracers();
- offEdge();
- drawHUD();
- bullets();
- for (int i = 0; i<asteroids.size(); i++) {
- asteroids.get(i).update();
- }
- for(int i=1; i < maxStarSize; i++){
- pushMatrix();
- //fill(255.-(float)i/maxStarSize*255,255,255);
- fill(255,0,0);
- translate(Spos2[i].x,Spos2[i].y,Spos2[i].z);
- sphere(random(starSize, starSize*2));
- popMatrix();
- }
- //background(0);
- }
- //---------------------------void end game----------------------
- void endGame() {
- }
- /*void mousePressed(){
- PVector mouse = new PVector(mouseX, mouseY,0);
- asteroids.add(new Asteroid(main, mouse));
- }
- */
- //-----------------------------void draw---------------------------
- void draw() {
- background(255, 0, 0);
- //is state = 0, preagame
- if (state == 0) {
- preGame();
- }
- //if state = 1, game
- else if (state == 1) {
- game();
- }
- //if state = 2, endgame
- else if (state == 2) {
- endGame();
- }
- }
Add Comment
Please, Sign In to add comment