/** * lolphysics - Built with Processing

* * Modes: * Game: The object of the game is to destroy as many boxes as possible, before you run out of health, or 90 has passed. Your health goes down everytime you pass outside of the window's boundaries.
* Free play: As the name implies, you're free to do whatever you want, for as long as you want. :)

* Controls:
* Arrow Keys: Move the blue box.
* Shift + Arrow Keys: Boost.
* Mouse Click: (free play only) Create a randomly colored box or circle. Size is a random number between 5 and the value of the 'Max Size' slider.
* Ctrl + Mouse Click: (free play only) Create a randomly colored box or circle. Size is the value of the 'Max Size' slider.
* Space: (free play only) Set the location of the blue box to where the mouse is. Useful if the box goes off the screen. (Note: The box's velocity is unchanged. If it's moving fast, it'll be moving fast when it appears at the mouse coordinates.)
* * R: (free play only) Resets all the sliders to their default value.
* C: (free play only) Removes all Boxes and Circles from the screen.
* *
Radio: (free play only)
* Box: Changes the selected shape to a box.
* Circle: Changes the selected shape to a circle.
* *
Sliders: (free play only)
* Gravity: Pretty self explanatory. Positive values, stuff goes down; negative, stuff goes up; zero, stuff floats around.
* Shift Force: Determines the amount of the boost.
* Max Size: Determines the max of the randomly generated numbers to get the size of new shapes.
* Friction: A higher friction means that touching bodies will loose energy, and will result in a "sticky" effect.
* * Restitution: Set the restitution of the edges, which define itself by the "spring" effect occurring when two bodies. A value of 1 would be a pool ball. The default is 0, which mean no restitution.
* Damping: Damping is the effect of "air" on a body. The more damping you have, the more energy lost you will get, resulting in an impression resistance to the gravity effect. Use this function to adjust the damping of all bodies, wich will make it fall faster or slower.
* Angular Velocity: How fast the blue cube spins. > 0 is clock wise, < 0 is counter-clockwise.

* * Credits: *
* Coding - tacticalbread/Xitherun
* ControlP5 (GUI Library) - Andreas Schlegel
* PPhys2D (Physics Engine) - Jean-Maxime Couillard (slightly) modified by tacticalbread/Xitherun
* */ import pphys2d.bodies.*; import pphys2d.joints.*; import pphys2d.shapes.*; import pphys2d.phys2d.raw.collide.*; import pphys2d.phys2d.raw.strategies.*; import pphys2d.phys2d.raw.forcesource.*; import pphys2d.phys2d.util.*; import pphys2d.phys2d.raw.shapes.*; import pphys2d.*; import pphys2d.phys2d.raw.*; import pphys2d.phys2d.math.*; import controlP5.*; //import processing.video.*; import ddf.minim.signals.*; import ddf.minim.*; import ddf.minim.analysis.*; import ddf.minim.effects.*; ControlP5 control; ControlTimer time; Textlabel[] label = new Textlabel[10]; //MovieMaker movie Minim minim; AudioSample hit; AudioSample on; AudioSample off; AudioSample gameover; AudioSample dead; AudioSample begin; AudioSample hurt; AudioPlayer bgm; //Create a PPhys2D world PPWorld world = new PPWorld(); //Create PPhys2D Objects PPBox[] boxes = new PPBox[100]; PPBox rofl; PPBox rofl2; PPCircle[] circles = new PPCircle[100]; float rotation = 0.0; boolean[] keyIsPressed = new boolean[612]; boolean mouse = false; boolean played = false; boolean endgame = false; int count = 0; int cCount = 0; int maxSize = 0; int shiftForce = 750000; int shape1 = 1; int hits = 0; int kills = 0; int lol; int mode = 0; /* * 0 = menu; * 1 = game; * 2 = freeplay; * 3 = gameover; */ void setup() { //Set size and framerate frameRate(60); size(1000,600); //Uncomment for Anti-Aliasing //smooth(); //Set a standard world gravity world.setGravity(0, 1); //Set world edges in dark gray world.setEdges(this, new Color (40, 40, 40)); //Add a box to the world rofl = new PPBox(15, 15); rofl.setPosition(250, 100); rofl.setStrokeWidth(1); rofl.setRotation(rotation); rofl.setFillColor(new Color(4,142,255)); rofl.setStrokeColor(new Color(0,0,0)); rofl.setGravityEffected(false); rofl.setMass(100); rofl.setHealth(10); /* for(int i = 1; i < boxes.length; ++i) { int color1 = (int)random(0, 255); int color2 = (int)random(0, 255); int color3 = (int)random(0, 255); boxes[i] = new PPBox(random(10, 100), random(10, 100)); boxes[i].setPosition(random(width), random(height)); boxes[i].setStrokeWidth(1); boxes[i].setRotation(rotation); boxes[i].setFillColor(new Color(color1, color2, color3)); boxes[i].setStrokeColor(new Color(color1, color2, color3)); boxes[1].setMass(random(10)); world.add(boxes[i]); } */ world.setEdgesRestitution(0); world.setEdgesFriction(0); world.setDamping(2); /* BUTTON SETUP */ /* FREEPLAY BUTTONS */ control = new ControlP5(this); time = new ControlTimer(); time.setSpeedOfTime(0); control.addSlider("Gravity", -600, 600, 10, 10, 40, 10); control.controller("Gravity").setValue(0.00); control.addSlider("force", 0, 100, 10, 21, 40, 10); control.controller("force").setLabel("Shift Force"); control.controller("force").setValue(75); control.addSlider("size", 1, 400, 10, 32, 40, 10); control.controller("size").setLabel("Max Size"); control.controller("size").setValue(100); control.addSlider("friction", 0, 10, 10, 43, 40, 10); control.controller("friction").setValue(0); control.addSlider("rest", 0, 1, 10, 54, 40, 10); control.controller("rest").setLabel("Restitution"); control.controller("rest").setValue(0); control.addSlider("damping", 1, 4, 10, 65, 40, 10); control.controller("damping").setValue(1); control.addSlider("spin", -50, 50, 10, 76, 40, 10); control.controller("spin").setLabel("Angular Velocity"); control.controller("spin").setValue(0); control.addButton("zero", 1, 10, 88, 65, 15); control.controller("zero").setLabel("Gravity == 0"); control.addButton("X", 1, width-18, 8, 11, 11); Radio shapes = control.addRadio("shapes", 10, 104); shapes.add("Square", 1); shapes.add("Circle", 2); label[0] = control.addTextlabel("hits", "Hits: " + hits, 10, height-35); label[1] = control.addTextlabel("kills", "Kills: " + kills, 10, height-25); label[2] = control.addTextlabel("health", "Health: " + rofl.health, 10, height-15); label[3] = control.addTextlabel("time", "Time: " + time.toString(), 10, height-45); label[5] = new Textlabel(this, "GAME OVER!", 350, 100, 400, 400, 0xffff0000, ControlP5.synt24); label[4] = new Textlabel(this, "Total Score: " + (hits + kills), 350, 200, 200, 200, 0xffff0000, ControlP5.synt24); /* MENU BUTTONS */ control.addButton("game", 1, width/2-10, height/2+100, 50, 20); control.controller("game").setLabel("New Game"); control.addButton("free", 1, width/2-10, height/2+121, 50, 20); control.controller("free").setLabel("Free Play"); control.addButton("quit", 1, width/2-10, height/2+142, 50, 20); minim = new Minim(this); hit = minim.loadSample("hit.wav"); on = minim.loadSample("on.wav"); off = minim.loadSample("off.wav"); gameover = minim.loadSample("gameover.wav"); dead = minim.loadSample("dead.wav"); begin = minim.loadSample("begin.wav"); hurt = minim.loadSample("hurt.wav"); on.trigger(); control.controller("Gravity").setVisible(false); control.controller("force").setVisible(false); control.controller("size").setVisible(false); control.controller("friction").setVisible(false); control.controller("rest").setVisible(false); control.controller("damping").setVisible(false); control.controller("spin").setVisible(false); control.controller("zero").setVisible(false); control.controller("shapes").setVisible(false); for(int i = 0; i < 5; ++i) label[i].setVisible(false); } void draw() { background(0); checkKey(); switch(mode) { case 0: setupMenu(); time.setSpeedOfTime(0); time.reset(); break; case 1: time.setSpeedOfTime(75); break; case 2: time.setSpeedOfTime(75); break; case 3: label[4].draw(this); label[5].draw(this); break; } //println(time.minute()); //println("Mouse X: " + mouseX + " Mouse Y: " + mouseY + " " + isMouseOver()); //println("getX(): " + rofl.getX() + " getY(): " + rofl.getY()); //println(shiftForce); //println("X: " + rofl.getVelocityX() + " Y: " + rofl.getVelocityY()); if(!endgame) { for(int i = 0; i <= count; ++i) { if(rofl.isTouchingBody(boxes[i])) { boxes[i].health--; if((rofl.getVelocityX() > 2000 || rofl.getVelocityX() < -2000) || (rofl.getVelocityY() > 2000 || rofl.getVelocityY() < -2000) || keyIsPressed[16]) boxes[1].health -= 4; background(255); System.out.printf("box %d! Health: %d!\n", i, boxes[i].health); if(boxes[i].health <= 0) { world.remove(boxes[i]); ++kills; dead.trigger(); } ++hits; hit.trigger(); } } for(int i = 0; i <= cCount; ++i) { if(rofl.isTouchingBody(circles[i])) { circles[i].health--; if((rofl.getVelocityX() > 2000 || rofl.getVelocityX() < -2000) || (rofl.getVelocityY() > 2000 || rofl.getVelocityY() < -2000) || keyIsPressed[16]) circles[i].health -= 4; background(255); System.out.printf("circle %d! Health: %d!\n", i, circles[i].health); if(circles[i].health <= 0) { world.remove(circles[i]); ++kills; dead.trigger(); } ++hits; hit.trigger(); } } if((rofl.getX() <= 0 || rofl.getX() >= width) || (rofl.getY() <= 0 || rofl.getY() >= height)) { background(255, 0, 0); hurt.trigger(); rofl.health--; rofl.addForce(-rofl.getVelocityX(), -rofl.getVelocityY()); rofl.addForce(-rofl.getVelocityX(), -rofl.getVelocityY()); rofl.addForce(-rofl.getVelocityX()*4000, -rofl.getVelocityY()*4000); rofl.adjustVelocity(0, 0); rofl.resetForces(); rofl.setPosition(width/2, height/2); } } label[0].setValue("Hits: " + hits); label[1].setValue("Kills: " + kills); label[2].setValue("Health: " + rofl.health); label[3].setValue("Time: " + time.toString()); if(rofl.health <= 0 || (time.hour() == 1 && time.minute() == 30) && mode == 1) gameOver(); rofl.setAngularVelocity(rotation); world.draw(this); } void setupMenu() { for(; lol < 20; ++lol) { int color1 = (int)random(0, 255); int color2 = (int)random(0, 255); int color3 = (int)random(0, 255); boxes[lol] = new PPBox(random(10, 100), random(10, 100)); boxes[lol].setPosition(random(width), random(height)); boxes[lol].setStrokeWidth(1); boxes[lol].setRotation(rotation); boxes[lol].setFillColor(new Color(color1, color2, color3)); boxes[lol].setStrokeColor(new Color(color1, color2, color3)); boxes[lol].setMass(random(10)); boxes[lol].setHealth((int)random(20, 100)); world.add(boxes[lol]); count++; boxes[lol].addForce(random(-20000, 20000), random(-20000, 20000)); } for(int i = 1; i < 20; ++i) boxes[i].addForce(random(-50000, 50000), random(-50000, 50000)); world.setEdgesRestitution(0); world.setEdgesFriction(0); world.setDamping(2); } /* KEYBOARD METHODS */ void keyPressed() { keyIsPressed[keyCode] = true; //println(keyCode); if(key == ' ' && mode == 2) { rofl.setPosition(mouseX, mouseY); rofl.setRotation(0); rofl.adjustVelocity(0, 0); } if(key == 'r' && mode == 2) { control.controller("rest").setValue(0); control.controller("damping").setValue(2); control.controller("friction").setValue(0); control.controller("size").setValue(100); control.controller("force").setValue(75); control.controller("Gravity").setValue(0.00); } if(key=='c' && mode == 2) { for(int i = 0; i < count; ++i) world.remove(boxes[i]); for(int i = 0; i < cCount; ++i) world.remove(circles[i]); count = 0; cCount = 0; } if(key == 'g') gameOver(); } void keyReleased() { keyIsPressed[keyCode] = false; } void checkKey() { if(keyIsPressed[38])rofl.addForce(0, -150000); //UP if(keyIsPressed[40])rofl.addForce(0, 150000); //DOWN if(keyIsPressed[37])rofl.addForce(-150000, 0); //LEFT if(keyIsPressed[39])rofl.addForce(150000, 0); //RIGHT if(keyIsPressed[16])//SHIFT { if(keyIsPressed[38])rofl.addForce(0, -shiftForce); //UP if(keyIsPressed[40])rofl.addForce(0, shiftForce); //DOWN if(keyIsPressed[37])rofl.addForce(-shiftForce, 0); //LEFT if(keyIsPressed[39])rofl.addForce(shiftForce, 0); //RIGHT } if(keyIsPressed[83]) world.step(); } /* MOUSE METHODS */ void mousePressed() { if(!isMouseOver() && mode == 2) { switch(shape1) { case 1: if(count == boxes.length) println("too many boxes!"); else if(keyIsPressed[17]) { int color1 = (int)random(0, 255); int color2 = (int)random(0, 255); int color3 = (int)random(0, 255); boxes[count] = new PPBox(maxSize, maxSize); boxes[count].setPosition(mouseX, mouseY); boxes[count].setStrokeWidth(1); boxes[count].setRotation(0.0); boxes[count].setFillColor(new Color(color1, color2, color3)); boxes[count].setStrokeColor(new Color(color1, color2, color3)); boxes[count].setMass(random(10)); boxes[count].setHealth((int)random(100)); world.add(boxes[count]); count++; } else { int color1 = (int)random(0, 255); int color2 = (int)random(0, 255); int color3 = (int)random(0, 255); boxes[count] = new PPBox(random(5, maxSize), random(5, maxSize)); boxes[count].setPosition(mouseX, mouseY); boxes[count].setStrokeWidth(1); boxes[count].setRotation(0.0); boxes[count].setFillColor(new Color(color1, color2, color3)); boxes[count].setStrokeColor(new Color(color1, color2, color3)); boxes[count].setMass(random(5)); boxes[count].setHealth((int)random(100)); world.add(boxes[count]); count++; } break; case 2: if(cCount == circles.length) println("too many Circles!"); else if(keyIsPressed[17]) { int color1 = (int)random(0, 255); int color2 = (int)random(0, 255); int color3 = (int)random(0, 255); circles[cCount] = new PPCircle(maxSize); circles[cCount].setPosition(mouseX, mouseY); circles[cCount].setStrokeWidth(1); circles[cCount].setRotation(0.0); circles[cCount].setFillColor(new Color(color1, color2, color3)); circles[cCount].setStrokeColor(new Color(color1, color2, color3)); circles[cCount].setMass(random(10)); circles[cCount].setHealth((int)random(100)); world.add(circles[cCount]); cCount++; } else { int color1 = (int)random(0, 255); int color2 = (int)random(0, 255); int color3 = (int)random(0, 255); circles[cCount] = new PPCircle(random(5, maxSize)); circles[cCount].setPosition(mouseX, mouseY); circles[cCount].setStrokeWidth(1); circles[cCount].setRotation(0.0); circles[cCount].setFillColor(new Color(color1, color2, color3)); circles[cCount].setStrokeColor(new Color(color1, color2, color3)); circles[cCount].setMass(random(10)); circles[cCount].setHealth((int)random(100)); world.add(circles[cCount]); cCount++; } break; } } } boolean isMouseOver() { /* MAIN BUTTONS */ if(mouseX >= 11 && mouseX <= 50 && mouseY >= 11 && mouseY <= 85) return true; /* GRAVITY BUTTON */ else if(mouseX >= 11 && mouseX <= 75 && mouseY >= 86 && mouseY <= 99) return true; /* CLOSE BUTTON */ else if(mouseX >= (width-17) && mouseX <= (width-6) && mouseY >= 8 && mouseY <= 19) return true; else if((mouseX >= 11 && mouseX <= 19 && mouseY >= 105 && mouseY <= 113) || (mouseX >= 11 && mouseX <= 19 && mouseY >= 119 && mouseY <= 127)) return true; else return false; } /* BUTTON METHODS */ void quit() { off.trigger(); delay(700); exit(); } void game() { time.reset(); time.setSpeedOfTime(75); control.controller("game").setVisible(false); control.controller("free").setVisible(false); control.controller("quit").setVisible(false); rofl = new PPBox(15, 15); rofl.setPosition(250, 100); rofl.setStrokeWidth(1); rofl.setRotation(rotation); rofl.setFillColor(new Color(4,142,255)); rofl.setStrokeColor(new Color(0,0,0)); rofl.setGravityEffected(false); rofl.setMass(100); rofl.setHealth(10); world.add(rofl); for(int i = 0; i < 4; ++i) label[i].setVisible(true); begin.trigger(); mode = 1; println("game!"); } void free() { time.reset(); time.setSpeedOfTime(75); control.controller("game").setVisible(false); control.controller("free").setVisible(false); control.controller("quit").setVisible(false); rofl = new PPBox(15, 15); rofl.setPosition(250, 100); rofl.setStrokeWidth(1); rofl.setRotation(rotation); rofl.setFillColor(new Color(4,142,255)); rofl.setStrokeColor(new Color(0,0,0)); rofl.setGravityEffected(false); rofl.setMass(100); rofl.setHealth(1000); world.add(rofl); for(int i = 0; i < 4; ++i) label[i].setVisible(true); control.controller("Gravity").setVisible(true); control.controller("force").setVisible(true); control.controller("size").setVisible(true); control.controller("friction").setVisible(true); control.controller("rest").setVisible(true); control.controller("damping").setVisible(true); control.controller("spin").setVisible(true); control.controller("zero").setVisible(true); control.controller("shapes").setVisible(true); for(int i = 0; i < count; ++i) world.remove(boxes[i]); for(int i = 0; i < cCount; ++i) world.remove(circles[i]); count = 0; cCount = 0; begin.trigger(); println("Free Play!"); mode = 2; } void Gravity(float num) { world.setGravity(0, num); } void force(int num) { shiftForce = num * 10000; } void size(int num) { maxSize = num; } void zero() { control.controller("Gravity").setValue(0); } void friction(int num) { world.setEdgesFriction(num); } void rest(float num) { world.setEdgesRestitution(num); } void damping(float num) { world.setDamping(num); } /* BACK TO MAIN MENU BUTTON */ void X() { if(mode!=0) { for(int i = 0; i < count; ++i) world.remove(boxes[i]); for(int i = 0; i < cCount; ++i) world.remove(circles[i]); count = 0; cCount = 0; lol = 0; } hits = 0; kills = 0; world.remove(rofl); control.controller("game").setVisible(true); control.controller("free").setVisible(true); control.controller("quit").setVisible(true); control.controller("Gravity").setVisible(false); control.controller("force").setVisible(false); control.controller("size").setVisible(false); control.controller("friction").setVisible(false); control.controller("rest").setVisible(false); control.controller("damping").setVisible(false); control.controller("spin").setVisible(false); control.controller("zero").setVisible(false); control.controller("shapes").setVisible(false); for(int i = 0; i < 4; ++i) label[i].setVisible(false); time.reset(); mode = 0; } void shapes(int num) { switch(num) { case 1: shape1 = 1; break; case 2: shape1 = 2; break; } } void spin(float num) { rotation = num; rofl.setAngularVelocity(rotation); } void gameOver() { endgame = true; label[4].setValue("Total Score: " + (hits + kills)); rofl.setPosition(-10, -10); world.remove(rofl); for(int i = 0; i < count; ++i) world.remove(boxes[i]); for(int i = 0; i < cCount; ++i) world.remove(circles[i]); control.controller("game").setVisible(false); control.controller("free").setVisible(false); control.controller("quit").setVisible(false); control.controller("Gravity").setVisible(false); control.controller("force").setVisible(false); control.controller("size").setVisible(false); control.controller("friction").setVisible(false); control.controller("rest").setVisible(false); control.controller("damping").setVisible(false); control.controller("spin").setVisible(false); control.controller("zero").setVisible(false); control.controller("shapes").setVisible(false); for(int i = 0; i < 4; ++i) label[i].setVisible(false); label[4].setVisible(true); if(!played) gameover.trigger(); time.reset(); mode = 3; rofl.setHealth(10); played = true; } void stop() { // always close Minim audio classes when you are done with them hit.close(); off.close(); on.close(); gameover.close(); //bgm.close(); minim.stop(); println("bye!"); super.stop(); }