void resetGame(){ guys = new HashSet(); guys.add(new downer(3,10)); guys.add(new upper(17,3)); eggs = new HashSet(); // eggs.add(new egg(11,10)); additionalRotateBackground = 0.0; upcomingRotateBackgroundOffset = 0.0; world = getWorld(); lifepoints = 0; framecount = 0; numberBorn = 1; numberKilled = 0; finalanimation = 0; nextTurn = NOTURN; } void setup(){ frameRate(FPS); size(600 ,600); soundAtSetup(); guiAtSetup(); imgBg = loadImage("rocket2andhull.png"); imgTitle = loadImage("TitleScreen.png"); imgEnding = loadImage("ending.png"); } void draw(){ background(0,0,30); if(gameState == START){ image(imgTitle,0,0); return ; } //ENDING!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! if(gameState == ENDING){ finalanimation--; pushMatrix(); translate(WORLDCENTERX,WORLDCENTERY+finalanimation); image(imgBg,imgBg.width / -2.0+2,imgBg.height / -2.0+40); popMatrix(); drawFinalScore(); return ; } framecount++; if(GAMELASTS - (framecount / FPS) <= 0){ gameState = ENDING; return; } //FIRST TICK OF ROUND: WE PLUG IN THE CLICKED ON ROTATION //WE ALSO TELL EACH GUY THAT IF THEY MOVED VIA ANIMATION, THEY SHOULD //'KNOW' THAT THEY ARE AT THEIR NEW LOCATION if(framecount % FRAMESPERROUND == 0){ activeTurn = nextTurn; if(nextTurn != NOTURN){ if(activeTurn == TURNCW){ upcomingRotateBackgroundOffset = PI/2; } if(activeTurn == TURNCCW){ upcomingRotateBackgroundOffset = -PI/2; } } nextTurn = NOTURN; Iterator i = guys.iterator(); while(i.hasNext()){ guy g = (guy)i.next(); if(g.registerMoveOrDie()){ //true if the guy is dead i.remove(); numberKilled++; } } } //TICKS BEFORE 'TURNSTEP', WE ARE MOSTLY JUST *visually* ROTATING //TURNSTEP is how many frames we do the turn in (should be less than that FRAMESPERROUND!) if((activeTurn != NOTURN)&&(framecount % FRAMESPERROUND) < TURNSTEP){ rot += STEPAMOUNT * activeTurn; //isn't that clever, our constants for activeTurn are +- 1... } //AT TURNSTEP EXACTLY, WE MENTALLY ROTATE THE BLOCKS IN THE GRAPH AND PUT THE GUYS IN NEW ROTATED SPOTS //WE ALSO HAVE EACH GUY THINK ABOUT HIS MOVE, FALLING OR MOVING.... if((framecount % FRAMESPERROUND) == TURNSTEP){ if((activeTurn != NOTURN)) { rot = 0.0; if(activeTurn == TURNCW){ world = rotateWorldCW(world); Iterator i = guys.iterator(); while(i.hasNext()){ guy g = (guy)i.next(); g.shiftCW(); } i = eggs.iterator(); while(i.hasNext()){ egg e = (egg)i.next(); e.shiftCW(); } } else { world = rotateWorldCCW(world); Iterator i = guys.iterator(); while(i.hasNext()){ guy g = (guy)i.next(); g.shiftCCW(); } i = eggs.iterator(); while(i.hasNext()){ egg e = (egg)i.next(); e.shiftCCW(); } } } //we're kind of resetting the turning... //the trouble is we need to keep track of how much rotation of the //background we're doing.... additionalRotateBackground += upcomingRotateBackgroundOffset; upcomingRotateBackgroundOffset = 0.0; Iterator i = eggs.iterator(); while(i.hasNext()){ egg e = (egg)i.next(); if(random(2) < 1){ guys.add(new upper(e.x,e.y)); } else { guys.add(new downer(e.x,e.y)); } i.remove(); } //now lets maybe add in a guy, eh? if(random(chance) < 1){ intXandY xy = findABlankSquare(); numberBorn++; /*if(random(2) < 1){ guys.add(new upper(xy.x,xy.y)); } else { guys.add(new downer(xy.x,xy.y)); }*/ eggs.add(new egg(xy.x,xy.y)); } i = guys.iterator(); while(i.hasNext()){ guy g = (guy)i.next(); g.calcMove(); } } //end in MENTALLY ROTATE AND AND GUYS THINK TIME //AT GREATER THAN TURNSTEP IN THE ROUND, WE DO THE ANIMATIONS if((framecount % FRAMESPERROUND) > TURNSTEP){ //START MOVE ANIMATION TIME Iterator i = guys.iterator(); while(i.hasNext()){ guy g = (guy)i.next(); g.move(); } } //END IF MOVE ANIMATION TIME //record the world rotation etc pushMatrix(); //move the "camera" middle of the world (for rotation) translate(WORLDCENTERX,WORLDCENTERY); if(rot != 0.0){ rotate(rot); } pushMatrix(); rotate(additionalRotateBackground); image(imgBg,imgBg.width / -2.0+2,imgBg.height / -2.0+40); popMatrix(); drawWorld(); Iterator i = guys.iterator(); while(i.hasNext()){ guy g = (guy)i.next(); g.animate(); g.draw(); } i = eggs.iterator(); while(i.hasNext()){ egg e = (egg)i.next(); e.animate(); e.draw(); } popMatrix(); doGUI(); } void keyPressed() { if(gameState == START || gameState == ENDING){ if(key == ' '){ resetGame(); if(gameState == START){ switchMusic(); } gameState = RUNNING; } } else { if(key == 'q' || key == 'Q'){ gameState = ENDING; } if ( key == 'z' || key == 'Z' || keyCode == LEFT) { nextTurn = TURNCCW; } if ( key == 'x' || key == 'X' || keyCode == RIGHT) { nextTurn = TURNCW; } } } void stop() { soundAtStop(); super.stop(); }