float GRAV = .1; Heart hp1 ;//= new Heart(false); Heart hp2 ;//= new Heart(true); ArrayList arrows;// = new ArrayList(); boolean virgin = true; ArrayList obstacles = new ArrayList(); ArrayList ammos ;//= new ArrayList(); PImage arrowimg,arrowimgplain; PImage bgimg; PImage ammoimg; PImage tailimg; PImage lifeheartimg,lifeheartimg_flip,bumperimg ; PImage winimg,loseimg; PImage rematchimg, titlescreen; void resetGame(){ ammos = new ArrayList(); arrows = new ArrayList(); hp1 = new Heart(false); hp2 = new Heart(true); setupButtons(); addAmmos(PI/2); addAmmos(PI/4); addAmmos(PI*3/4); } void setup() { // size(700, 400); size(document.documentElement.clientWidth, document.documentElement.clientHeight); obstacles.add(new Obstacle(width/4,height/2,width/8)); obstacles.add(new Obstacle(width/2,height/2,width/8)); obstacles.add(new Obstacle(width*3/4,height/2,width/8)); frameRate(20); bgimg = loadImage("bgclouds.jpg"); arrowimg = loadImage("arrow.png"); arrowimgplain = loadImage("arrowplain.png"); ammoimg = loadImage("floatingammo.png"); tailimg = loadImage("floatingammosmall.png"); lifeheartimg = loadImage("lifeheart.png"); lifeheartimg_flip = loadImage("lifeheart_flip.png"); bumperimg = loadImage("bumper.png"); winimg = loadImage("ui/victory.png"); loseimg = loadImage("ui/ohno.png"); rematchimg = loadImage("ui/rematchnew.png"); titlescreen = loadImage("ui/cover.png"); resetGame(); } void draw() { image(bgimg,0,0,width,height); checkToAddAmmo(); drawButtons(); drawSliders(); fill(0); // text(frameRate, 200, 200); hp1.move(); hp2.move(); for (Arrow a : arrows) { a.move(); a.draw(false); for(Obstacle o: obstacles){ o.checkArrowStop(a); } if(a.hit(hp1)){ a.getStuck(hp1); hp1.stuckArrow(a); arrows.remove(a); } if(a.hit(hp2)){ a.getStuck(hp2); hp2.stuckArrow(a); arrows.remove(a); } if(a.offscreen()) arrows.remove(a); } for(Obstacle o:obstacles){ hp1.checkObstacleBounce(o); hp2.checkObstacleBounce(o); o.draw(); } doWinLose(); for(Ammo a:ammos){ a.move(); a.draw(); if(a.hit(hp1)){ hp1.addAmmo(); ammos.remove(a); lowLag.play("heartcher_ammoget.wav"); } if(a.hit(hp2)){ hp2.addAmmo(); ammos.remove(a); lowLag.play("heartcher_ammoget.wav"); } } hp1.drawControlPointer(); hp2.drawControlPointer(); hp1.draw(); hp2.draw(); if(virgin){ imageMode(CENTER); image(titlescreen,width/2,height/2); imageMode(CORNER); } // showmsg(); } void checkToAddAmmo(){ if( (ammos.size()*10) + hp1.ammo + hp2.ammo < 20){ addRandomAmmo(); } } void addRandomAmmo(){ float r = random(PI/8,PI*7/8); addAmmos(r); } void addAmmos(ang){ float r = width/3; ammos.add(new Ammo(width/2+cos(ang)*r,height/2+sin(ang)*r)); ang += PI; ammos.add(new Ammo(width/2+cos(ang)*r,height/2+sin(ang)*r)); } float rematcha = 0.0; void doWinLose(){ imageMode(CENTER); if(hp1.health <= 0 || hp2.health <= 0){ pushMatrix(); translate(width/2,height/2); rotate(rematcha); rematcha += 0.04; image(rematchimg,0,0); popMatrix(); PImage img1,img2; if(hp1.health <= 0) img1 = loseimg; else img1 = winimg; if(hp2.health <= 0) img2 = loseimg; else img2 = winimg; pushMatrix(); translate(width/2,height*3/4); image(img1,0,0); popMatrix(); pushMatrix(); translate(width/2,height/4); rotate(PI); image(img2,0,0); popMatrix(); } imageMode(CORNER); }int AMMOPER = 10; class Heart { float x = width/2; float y = height/2; PImage imguu,imgud,imgdu,imgdd; PImage bowimg; PImage sliderImg; int health = 3; int ammo = 0; boolean flipped; int flipway = 1; PImage idle[40] = new PImage[40]; PImage dead; Heart(boolean pflipped) { flipped = pflipped; if(flipped) flipway = -1; if(!flipped) { sliderImg = loadImage("ui/arrowhead.png"); } else { sliderImg = loadImage("ui/arrowhead_flip.png"); } x = width/2; y = height/2 + (flipway * height/4) ; addAmmo(); imguu = loadImage("wing_png/wings1.png"); imgud = loadImage("heartud.png"); imgdu = loadImage("heartdu.png"); imgdd = loadImage("heartdd.png"); bowimg = loadImage("bow.png"); for(int i = 0; i < 40; i++){ idle[i] = loadImage("wing_png/wings"+(i+1)+".png"); } dead = loadImage("wing_png/dead.png") changeAngle(.5); } float xs, ys; float sz = 50; void move() { ys += GRAV * flipway; x += xs; y += ys; if ((health > 0 ) && y > (height*7/8)-sz/2 ) { y = (height*7/8)-sz/2; ys = -abs(ys)/2; } if ((health > 0 ) && y < sz/2 + (height/8)) { y = sz/2 + (height/8); ys = abs(ys)/2; } if (x < sz/2) { x = sz/2; xs = abs(xs)/2; } if (x > width-sz/2) { x = width-sz/2; xs = -abs(xs)/2; } /* if (mousePressed) { a = atan2(mouseDownY-mouseY, mouseDownX-mouseX); stroke(225); line( mouseDownX, mouseDownY, mouseX, mouseY); }*/ moveSegments(); if(newarrow != null) newarrow.move(); for(Arrow a : stuckArrows){ a.move(); } } ArrayList stuckArrows = new ArrayList(); void stuckArrow(Arrow a){ stuckArrows.add(a); } float a; float vizrot; int idlepos = 0; void draw() { if(newarrow != null && ammo > 0) newarrow.draw(true); for(Arrow a : stuckArrows){ a.draw(false); } stroke(0); drawSegments(); pushMatrix(); translate(x, y); idlepos++; if(idlepos >= 40) idlepos = 0; Image img = idle[idlepos]; if(wingLeftDown || wingRightDown) img = imgdd; if(health <= 0) img = dead; pushMatrix(); rotate(vizrot); if(flipped) rotate(PI); vizrot *= .9; image(img, -75, -75); //fill(255,128); //ellipse(0,0,sz*3/4,sz*3/4); popMatrix(); rotate(this.a); translate(sz*2/3,0); image(bowimg,0,-25); popMatrix(); } void flapLeft() { if(health <= 0) return; ys -= GRAV *5 * flipway; xs -= GRAV *4 * flipway; vizrot -= .3; wingLeftDown = false; lowLag.play('heartcher_flap.wav'); } void flapRight() { if(health <= 0) return; ys -= GRAV * 20 * flipway; xs += GRAV *4 * flipway; vizrot += .3; wingRightDown = false; lowLag.play('heartcher_flap.wav'); } boolen wingLeftDown = false; boolen wingRightDown = false; void wingRight(){ wingRightDown = true; } void wingLeft(){ wingLeftDown = true; } Arrow newarrow = null; float rawa; void changeAngle(p){ //between 0 and 1 if(flipped) p = 1 - p; rawa = p; this.a = (p * PI * 2)+PI/2; if(flipped) this.a += PI; if(newarrow == null){ newarrow = new Arrow(this,false); } } int FAKESEGS = 2; void addAmmo(){ ammo += AMMOPER; int numsegsWanted = ceil(ammo / AMMOPER); while(segx.length < numsegsWanted + FAKESEGS){ float newx = this.x; float newy = this.y; segx.push(newx); segy.push(newy); } } void fire() { if(ammo <= 0 || health <= 0) return; ammo--; arrows.add(new Arrow(this,true)); newarrow = null; int num = int(random(3))+1; lowLag.play("heartcher_shoot"+num+".wav"); int numsegsWanted = ceil(ammo / AMMOPER); // msg(numsegsWanted); if(segx.length > numsegsWanted + FAKESEGS){ segx.pop(); segy.pop(); } } float[] segx = new float[0]; float[] segy = new float[0]; float segLength = 20; void moveSegments() { dragSegment(0, x, y); for (int i=0; i= FAKESEGS-1){ //ellipse(segx[i],segy[i],10,10); image(tailimg,segx[i]-5,segy[i]-5); } else { stroke(0,255,0); } //line(width/2,height/2,segx[i],segy[i]); } } void drawControlPointer(){ // text(rawa,500,500); if(!flipped){ for(int i = 0; i < health; i++){ image(lifeheartimg,(width * 6 / 10) + (i * 20),height*57/64); } } else { for(int i = 0; i < health; i++){ image(lifeheartimg_flip,(width * 4 / 10) - (i * 20)-14,(height*7/64)-14); } } fill(255); if(!flipped){ pushMatrix(); translate(width / 2 + (rawa * width / 2),height*7/8+51); rotate(-.5 + rawa); image(sliderImg,-30,-51); popMatrix(); } else { // rect(width / 2 - (rawa * width / 2) - 10,0,20,height/8); pushMatrix(); translate(width / 2 - (rawa * width / 2) , 0); rotate(-.5 + rawa); image(sliderImg,-30,0); popMatrix(); } } void checkObstacleBounce(Obstacle o){ if(dist(o.x,o.y,this.x,this.y) <= (o.sz + this.sz)/2){ //line(o.x,o.y,this.x,this.y); float d = (o.sz + this.sz)/2; float ang = atan2(this.y - o.y, this.x-o.x); this.x = o.x + cos(ang) * d; this.y = o.y + sin(ang) * d; this.xs = -this.xs/2; this.ys = -this.ys/2; } } void hitByArrow(){ int num = int(random(3))+1; lowLag.play("ows_0"+num+".wav"); health--; if(health == 0){ lowLag.play('heartcher_fanfare.wav'); } } } class Arrow { float a; float x, y; float xs, ys; //these are the ones from heart ,added to the other motion float SPEED = 10; boolean going; boolean stuck; boolean stuckInObstacle = false; Heart source; Arrow(Heart h, boolean isGoing) { source = h; resetSpeedPos(); going = isGoing; } void resetSpeedPos(){ x = source.x + (source.sz*cos(source.a)); y = source.y+(source.sz*sin(source.a)); a = source.a; xs = source.xs; ys = source.ys; } void go(){ going = true; } boolean hit(Heart h){ if(dist(x,y,h.x,h.y) < (h.sz*3/4)/2){ h.hitByArrow(); return true; } return false; } float offX; float offY; void getStuck(Heart h){ source = h; offX = x - h.x; offY = y - h.y; going = false; stuck = true; x += 40*cos(a); y += 40*sin(a); } void move() { if(stuckInObstacle) return; if(going){ if(x < 0){ a = PI -a; } if(x > width){ a = PI -a; } x += 10*cos(a);// + xs; y += 10*sin(a);//+ ys; } else { if(!stuck) resetSpeedPos(); else { x = source.x +offX; y = source.y +offY; } } } void draw(isHot) { pushMatrix(); translate(x,y); rotate(a); if(isHot)image(arrowimg ,-52,-8); else image(arrowimgplain,-52,-8); popMatrix(); } boolean offscreen(){ if(y < -100 || y > height+100){ return true; } return false; } } ArrayList