color BACKGROUND; color BACKGROUNDGRID; color DARKBACKGROUND; color DARKBACKGROUNDGRID; color GRID; color BLUE; color BLUEOUTLINE; color RED; color REDOUTLINE; color CANNONFILL; color CANNONOUTLINE; color SQUAREFILL; color SQUAREOUTLINE; color TRIANGLEFILL; color TRIANGLEOUTLINE; color PENTAGONFILL; color PENTAGONOUTLINE; color HEALTH; color HEALTHBACKGROUND; //--=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=--=-==-=-=-==-==-=-=-=-=-=-=-=-=-=-==-==-=-=-=-=-=-=-=-=-=-=-=-=- Player P; Shapes shapes; int rendermargin = 50; PVector fieldsize = new PVector(2000,2000); int fieldBorder = 250; int gridSize = 30; void setup() { fullScreen(); smooth(); frameRate(60); translate(width/2, height/2); colorMode(HSB, 360,100,100); //--=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=--=-==-=-=-==-==-=-=-=-=-=-=-=-=-=-==-==-=-=-=-=-=-=-=-=-=-=-=-=- BACKGROUND = color(0,0,80); BACKGROUNDGRID = color(0,0,75); DARKBACKGROUND = color(0,0,72); DARKBACKGROUNDGRID = color(0,0,67); GRID = color(0,0,0,15); BLUE = color(193,100,88); BLUEOUTLINE = color(193,100,66); RED = color(358,68,95); REDOUTLINE = color(358,68,71); CANNONFILL = color(0,0,60); CANNONOUTLINE = color(0,0,45); SQUAREFILL = color(51,59,100); SQUAREOUTLINE = color(51,59,75); TRIANGLEFILL = color(360,53,99); TRIANGLEOUTLINE = color(359,53,74); PENTAGONFILL = color(230,53,99); PENTAGONOUTLINE = color(230,53,74); HEALTH = color(115,45,89); HEALTHBACKGROUND = color(0,0,33); //--=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=--=-==-=-=-==-==-=-=-=-=-=-=-=-=-=-==-==-=-=-=-=-=-=-=-=-=-=-=-=- P = new Player(0); shapes = new Shapes(); } void draw() { translate(width/2-P.pos.x,height/2-P.pos.y); //background background(DARKBACKGROUND); noStroke(); fill(BACKGROUND); rect(0,0,fieldsize.x,fieldsize.y); //grid lines stroke(BACKGROUNDGRID); strokeWeight(3); for(float i = floor((P.pos.x-width/2)/gridSize)*gridSize; i < P.pos.x+width/2; i+=gridSize) { line(i, P.pos.y-height/2, i, P.pos.y+height/2); } for(float i = floor((P.pos.y-height/2)/gridSize)*gridSize; i < P.pos.y+height/2; i+=gridSize) { line(P.pos.x-width/2, i, P.pos.x+width/2, i); } shapes.show(); P.update(); P.show(); //text(P.pos.x, P.pos.x-width/2+30,P.pos.y-height/2+30); //text(P.pos.y, P.pos.x-width/2+30,P.pos.y-height/2+60); } void keyPressed() { if (key != CODED) { if (key == 'a') { P.xdir = -1; } if (key == 'd') { P.xdir = 1; } if (key == 'w') { P.ydir = -1; } if (key == 's') { P.ydir = 1; } if (key == ' ') { P.firing = true; } } } void keyReleased() { if(key != CODED) { if (key == 'a') { P.xdir = 0; } if (key == 'd') { P.xdir = 0; } if (key == 'w') { P.ydir = 0; } if (key == 's') { P.ydir = 0; } if (key == ' ') { P.firing = false; } } } void mousePressed() { if(mouseButton == LEFT) { P.firing = true; } } void mouseReleased() { if(mouseButton == LEFT) { P.firing = false; } } class Bullet { boolean dead = false; boolean actuallydead = false; int alpha = 360; int size, health, damage; int t = 0; PVector vel = new PVector(0, 0); PVector pos = new PVector(0, 0); Bullet(PVector position, float direction, int sp, int s, int h, int d) { pos.set(position.x,position.y); vel.set(cos(direction) * sp, sin(direction) * sp); size = s; health = h; damage = d; } void show() { fill(P.body, alpha); stroke(P.outline, alpha); strokeWeight(5); ellipse(pos.x,pos.y,size,size); } void update() { pos.set(pos.x+vel.x,pos.y+vel.y); t++; //collision for (int i = shapes.shapes.size() - 1; i >= 0; i--) { Shape shape = shapes.shapes.get(i); if(!dead) { if(dist(shape.pos.x,shape.pos.y,pos.x,pos.y) < (shape.s+size)/2) { shape.health-=constrain(damage,0,health); if(shape.health>0) { health = 0; } else { health-=damage; } } } } if(health <= 0) { dead = true; } if(t>100) { dead = true; } if(dead) { if(alpha<20) { actuallydead = true; } else { alpha -= 20; } } } } class Cannon { int w; int l; int bsize; int bspeed; int reload; int bhealth, bdamage; float dir; float dirOffset = 0; PVector pos = new PVector(); //pos of tip of cannon PVector recoil = new PVector(); Cannon(int wide, int len, float doffset, int bsi, int bsp, int relode, int bh, int bd) { w = wide; l = len; bsize = bsi; bspeed = bsp; reload = relode; dirOffset = doffset; bhealth = bh; bdamage = bd; } int reloadcnt = reload; void show() { fill(CANNONFILL); stroke(CANNONOUTLINE); strokeWeight(5); pushMatrix(); translate(P.pos.x + P.vel.x, P.pos.y + P.vel.y); rotate(dir); rect(P.size/2, -w/2, l, w); popMatrix(); } void update() { this.pos.set(P.pos.x + cos(P.facing) * (l+P.size/2), P.pos.y + sin(P.facing) * (l+P.size/2)); dir = P.facing + dirOffset; if(reloadcnt > 0) { reloadcnt--; } } void fire() { recoil.set(cos(dir+PI) * bsize/20, sin(dir+PI) * bsize/20); P.bullets.add(new Bullet(pos,dir,bspeed,bsize,bhealth, bdamage)); P.vel.set(P.vel.x + recoil.x, P.vel.y + recoil.y); reloadcnt = reload; } } class Player { int team; //0 = blue 1 = red Cannon C; Player(int t) { C = new Cannon(25, 28, 0, 25, 7, 30, 15, 6); team = t; if (team == 0) { body = BLUE; outline = BLUEOUTLINE; } else if (team == 1) { body = RED; outline = REDOUTLINE; } } color body; color outline; ArrayList bullets = new ArrayList(); PVector acc = new PVector(0, 0); PVector vel = new PVector(0, 0); PVector pos = new PVector(fieldsize.x/2, fieldsize.y/2); int xdir = 0; int ydir = 0; float facing; int size = 60; int speed = 4; float acceleration = 0.2; int friction = -2; boolean firing = false; void show() { facing = atan2(mouseY-height/2, mouseX-width/2); for (Bullet part : bullets) { part.show(); } C.show(); //fill(75, 200, 255); fill(body); stroke(outline); strokeWeight(5); ellipse(pos.x + vel.x, pos.y + vel.y, size, size); } void update() { for (int i = bullets.size() - 1; i >= 0; i--) { Bullet bull = bullets.get(i); bull.update(); if (bull.actuallydead) { bullets.remove(i); } } if (xdir == -1) { if (vel.x > -speed) { vel.x -= acceleration; } else { vel.x = -speed; } } else if (xdir == 1) { if (vel.x < speed) { vel.x += acceleration; } else { vel.x = speed; } } else if (xdir == 0) { if (vel.x<-0.05) { vel.x += acceleration/4; } else if (vel.x > 0.05) { vel.x -=acceleration/4; } else { vel.x = 0; } } if (ydir == -1) { if (vel.y > -speed) { vel.y -= acceleration; } else { vel.y = -speed; } } else if (ydir == 1) { if (vel.y < speed) { vel.y += acceleration; } else { vel.y = speed; } } else if (ydir == 0) { if (vel.y < -0.05) { vel.y += acceleration/4; } else if (vel.y > 0.05) { vel.y -= acceleration/4; } else { vel.y = 0; } } pos.set(pos.x + vel.x, pos.y + vel.y); C.update(); if (firing) { if (C.reloadcnt == 0) { C.fire(); } } // constraining position to the field size if (pos.x<-fieldBorder+size/2) { pos.x = -fieldBorder+size/2; if (vel.x<0) { vel.x = 0; } } if (pos.x>fieldsize.x+fieldBorder-size/2) { pos.x = fieldsize.x+fieldBorder-size/2; if (vel.x>0) { vel.x = 0; } } if (pos.y<-fieldBorder+size/2) { pos.y = -fieldBorder+size/2; if (vel.y<0) { vel.y = 0; } } if (pos.y>fieldsize.y+fieldBorder-size/2) { pos.y = fieldsize.y+fieldBorder-size/2; if (vel.y>0) { vel.y = 0; } } } } class Shapes { ArrayList shapes = new ArrayList(); Shapes() { for(int i = 0;i<50;i++) { //squares float r = random(0,2*PI); shapes.add(new Shape(4, int(random(0,fieldsize.x)),int(random(0,fieldsize.y)),r,r,0.15)); } for(int i = 0;i<30;i++) { //triangles float r = random(0,2*PI); shapes.add(new Shape(3, int(random(0,fieldsize.x)),int(random(0,fieldsize.y)),r,r,0.15)); } for(int i = 0;i<10;i++) { //pentagons float r = random(0,2*PI); shapes.add(new Shape(5, int(random(0,fieldsize.x)),int(random(0,fieldsize.y)),r,r,0.15)); } } void show() { for (int i = shapes.size() - 1; i>=0; i--) { Shape shape = shapes.get(i); if(!shape.dead) { shape.update(); shape.show(); } else { shapes.remove(i); } } } } class Shape { int s; int sides; float speed; PVector pos = new PVector(); PVector vel = new PVector(); float rotation, direction; float rotationspeed = 0.005; int maxHealth, health; float healthBarCo; color outline, fill; boolean dead = false; Shape(int si, int posx, int posy, float dir, float movedir, float sp) { pos.set(posx,posy); rotation = dir; direction = movedir; speed = sp; sides = si; vel.set(cos(direction)*speed, sin(direction)*speed); if (int(random(0,2)) == 1) { rotationspeed *= -1; } if(si == 4) { s = 28; maxHealth = 10; fill = SQUAREFILL; outline = SQUAREOUTLINE; healthBarCo = 4; } else if(si == 3) { s = 27; maxHealth = 25; fill = TRIANGLEFILL; outline = TRIANGLEOUTLINE; healthBarCo = 1.5; } else if(si == 5) { s = 35; maxHealth = 65; fill = PENTAGONFILL; outline = PENTAGONOUTLINE; healthBarCo = 0.65; } health = maxHealth; } void show() { if(pos.xP.pos.x-width/2-rendermargin && pos.yP.pos.y-height/2-rendermargin) { stroke(outline); strokeWeight(5); fill(fill); pushMatrix(); translate(pos.x,pos.y); rotate(rotation); beginShape(); for (float i = 0; i< 2*PI; i+= 2*PI/sides) { vertex(cos(i)*s, sin(i)*s); } endShape(CLOSE); popMatrix(); //health bar if(health0.15) { speed -= 0.05; } vel.set(cos(direction)*speed, sin(direction)*speed); pos.set(pos.x+vel.x,pos.y+vel.y); for (int i = shapes.shapes.size() - 1; i >= 0; i--) { Shape shape = shapes.shapes.get(i); if(shape == this) continue; if(!dead) { if(dist(shape.pos.x,shape.pos.y,pos.x,pos.y) < s+shape.s) { pushMatrix(); translate(pos.x,pos.y); float atan2 = atan2(shape.pos.y,shape.pos.x); popMatrix(); if (shape.direction - direction <= .001) { println("shape direction = " + atan2); println("my direction = " + direction); } shape.direction = atan2; shape.speed = 1.2; direction = atan2+PI; speed = 1.2; } } } if(health <= 0) { dead = true; } } }