//PEW! PEW! //Create the class blueprint //Declare the arrays class PEW { float x, y, sx, sy, dx, dy; PEW (int startx, int starty, int destx, int desty) { this.x = startx; //Start positions this.y = starty; this.dx = destx; //End positions this.dy = desty; //Calculating the horizontal / vertical speeds //Wechose a speedof 20 pixels per draw because it seemed nice this.sx = 20.0 * (destx - startx) / dist(startx, starty, destx, desty); //Unit vector math this.sy = 20.0 * (desty - starty) / dist(startx, starty, destx, desty); //Dont worry too much about it } void render () { this.x += this.sx; //Move this.y += this.sy; //If it gets nearits destination, teleport away so it will be removed in void draw() if (dist(this.x, this.y, this.dx, this.dy) < 30) this.x = -100; //Draw the laser line line (this.x, this.y, this.x + this.sx, this.y + this.sy); } } //This class is just a box that counts down andruns basically the same //code we used to make lasers on mousepressed class Turret { int x, y; int counter; Turret(int a, int b) { this.x = a; this.y = b; //Counter is used to coutn down until time to fire another PEW this.counter = 10; } void render() { fill(255, 0, 0); rect(this.x, this.y, 20, 20); this.counter--; if (this.counter < 0) { //Count has run out, time for PEW PEW action! this.counter = 10; laserz.add(new PEW(this.x, this.y, mouseX, mouseY)); //PEW!!! } } } ArrayList laserz = new ArrayList(); Turret MrT; //A single instance of a turret Turret[] teaTime = new Turret[30]; //A whole array of turrets cause of course we're doing that void setup() { //The place to: //Populate the arrays with new objects //Set the screen size size(800, 600); MrT = new Turret(100, height - 50); //Look a single turret! //Many turrets!! Nevermind the silly variable name for (int i=0; i < teaTime.length; i++) teaTime[i] = new Turret(floor(random(width)), floor(random(height))); } void draw() { //Run the objects and use their methods //Draw the background fill(0); rect(0, 0, width, height); MrT.render(); //Processing a single Turret for (int i=0; i < teaTime.length; i++) teaTime[i].render(); //Processing alot of turrets stroke(255); //Colour ofthe lasers for (int i=0; i < laserz.size(); i++) laserz.get(i).render(); //Processing all the lasers //Remove lasers that are out of bounds for (int i=0; i < laserz.size(); i++) { if (laserz.get(i).x < 0 || laserz.get(i).x > width || laserz.get(i).y < 0 || laserz.get(i).y > height) laserz.remove(i); } //Just a display to show how many lasers are currently active fill(255, 255, 0); text(laserz.size(), 100, 100); } void mousePressed() { laserz.add(new PEW(width /2, height, mouseX, mouseY)); //PEW!!! }