Menu menu; Game game; int mode; final int MENU=0, GAME=1; PFont font; SkanaarKey sk; void setup(){ size(600,350,P3D); sk = new SkanaarKey(); menu = new Menu(); font = loadFont("silkscreen.vlw"); textFont( font ); frameRate(20); } void initGame(){ game = new Game(); game.loadMedia(); game.loadBoard(0); game.loadTemplates(); game.initGUI(); } void startGame(){ mode = GAME; } void draw(){ if(mode==MENU) menu.visualize(); else{ game.update(); game.visualize(); } } void mouseReleased(){ if(mode==MENU) menu.mouseReleased( mouseX, mouseY ); else game.mouseReleased( mouseX, mouseY ); } void keyPressed(){ sk.keyPressed(key,keyCode); } void keyReleased(){ if(mode==MENU) menu.keyReleased(key, keyCode); if(mode==GAME) game.keyReleased(key, keyCode); sk.keyReleased(key,keyCode); } //========================== class Game{ Terrain terrain; EntityList objs; MediaBank mediabank; Button pauseButton, selectButton, defenseCategoryButton, toolCategoryButton, researchCategoryButton, nextBoardButton, restartButton; Button[] buttons, masterButtons, defenseButtons, toolButtons, researchButtons; Defense transmitter, tesseract, trinary, constant; PartialFormatRangeDrawer partialFormat; int timer, resourceTimer=0, messageTimer=0, messageFlashTimer=0; String message=""; float rotZ = -PI/8-12*PI/16; float rotX = PI/4; int cycles, memory, cpus=0, enemySum, enemyCount, boardNumber; boolean gameover, victory, paused, lastboard; int toolbarX = 580, toolbarY = 35; //--------------- Game(){ } //--------------- void loadMedia(){ mediabank = new MediaBank(); mediabank.loadGeometries(); mediabank.loadEnemies(); } //--------------- void initGUI(){ masterButtons = new Button[7]; pauseButton = masterButtons[0] = new PauseButton(490,5); selectButton = masterButtons[1] = new SelectButton(1580,5); defenseCategoryButton = masterButtons[2] = new DefenseButton(toolbarX-20,toolbarY-5); toolCategoryButton = masterButtons[3] = new ToolButton(toolbarX-20,toolbarY+16); researchCategoryButton = masterButtons[4] = new ResearchButton(toolbarX-20,toolbarY+37); nextBoardButton = masterButtons[5] = new NextBoardButton(width/2+75,height/2-11); restartButton = masterButtons[6] = new RestartButton(width/2+75,height/2-11); restartButton.hide(); nextBoardButton.hide(); defenseButtons = new Button[4]; defenseButtons[0] = new TransmitterButton(toolbarX,toolbarY); defenseButtons[1] = new TrinaryButton(toolbarX,toolbarY+20); defenseButtons[2] = new TesseractButton(toolbarX,toolbarY+40); defenseButtons[3] = new ConstantButton(toolbarX,toolbarY+60); toolButtons = new Button[3]; toolButtons[0] = new RecycleButton(toolbarX,toolbarY); toolButtons[1] = new PrioritizeButton(toolbarX,toolbarY+20); toolButtons[2] = new PartialFormatButton(toolbarX,toolbarY+40); researchButtons = new Button[3]; researchButtons[0] = new UpgradeAttackButton(toolbarX,toolbarY); researchButtons[1] = new UpgradeRangeButton(toolbarX,toolbarY+20); researchButtons[2] = new UpgradeFirerateButton(toolbarX,toolbarY+40); buttons = new Button[ masterButtons.length + defenseButtons.length + toolButtons.length + researchButtons.length ]; int i,j,k; for(i=0;i", 1000 ); } //--------------- boolean attemptConstruction(Defense d, int gridx, int gridy){ if(game.cycles >= d.cycleCost && game.memory >= d.memoryCost){ if( game.terrain.isCellInsideMap( gridx,gridy ) ){ if( game.terrain.isCellBuildable( gridx,gridy ) ){ game.objs.add( d ); game.terrain.groundEntity[ d.gridx ][ d.gridy ] = d; game.cycles -= d.cycleCost; game.memory -= d.memoryCost; return true; } else postMessage("construction impossible at designated coordinates"); } } else postMessage("insufficent resources availible for construction"); return false; } //--------------- void pause(){ paused = true; postMessage( "game paused", 15000 ); } //--------------- void unpause(){ paused = false; postMessage( "game commencing", 30 ); } //--------------- void gameover(){ gameover = true; restartButton.show(); } //--------------- void victory(){ victory = true; if(!lastboard) nextBoardButton.show(); } //--------------- void rewardResources(int cycles, int memory){ this.cycles += cycles; this.memory += memory; // the resourceTimer allows the resource-HUD to flash if(cycles>0 || memory>0) resourceTimer = 20; } //--------------- void postMessage(String msg){ postMessage(msg, 40); } //--------------- void postMessage(String msg, int duration){ messageFlashTimer = 20; messageTimer = duration+20; message = msg; } //--------------- void update(){ if(!gameover && !paused && !victory){ objs.updateAll(); for(int i=0;i0) resourceTimer--; if(messageTimer>0) messageTimer--; if(messageFlashTimer>0) messageFlashTimer--; draw3D(); if(gameover) drawPopup( "game over" ); if(victory) drawPopup( "victory" ); //if(paused) drawPopup( "paused" ); drawHUD(); } //--------------- void draw3D(){ float[] planePoint = game.screenToPlaneCoords(mouseX,mouseY); int xGrid = terrain.xGrid(planePoint[0]); int yGrid = terrain.yGrid(planePoint[1]); pushMatrix(); translate(width/2,height/2,0); rotateX(rotX); rotateZ(rotZ); //translate(0,0,50); background(0); terrain.visualize(); objs.visualizeAll(); if( terrain.isCellInsideMap(xGrid,yGrid) ){ if( terrain.isCellBuildable(xGrid,yGrid) ){ if(defenseButtons[0].selected || defenseButtons[1].selected || defenseButtons[2].selected || defenseButtons[3].selected ){ float x2 = terrain.xPos( xGrid ); float y2 = terrain.yPos( yGrid ); fill(255,128); stroke(255,128); pushMatrix(); translate(x2,y2,terrain.cellSize/2-1); box(game.terrain.cellSize); popMatrix(); } if(defenseButtons[0].selected) transmitter.drawRange(planePoint[0],planePoint[1]); else if(defenseButtons[1].selected) trinary.drawRange(planePoint[0],planePoint[1]); else if(defenseButtons[2].selected) tesseract.drawRange(planePoint[0],planePoint[1]); if(defenseButtons[3].selected) constant.drawRange(planePoint[0],planePoint[1]); } if(toolButtons[2].selected) partialFormat.drawRange(planePoint[0],planePoint[1]); else if( terrain.groundEntity[xGrid][yGrid] != null ) if( terrain.groundEntity[xGrid][yGrid].isDefense ) ((Defense) terrain.groundEntity[xGrid][yGrid]).drawRange(); } popMatrix(); } //--------------- void drawHUD(){ // this function presumes that the transform matrix has been reset // hint the renderer to show the hud on top of everything else hint(DISABLE_DEPTH_TEST); textMode(SCREEN); fill(192); textAlign( LEFT ); //HUD level info readout roundRect(-1,-1,82,27); fill(192); text("Board "+boardNumber,5,10); text("progress "+(int)(100-enemyCount*100/enemySum)+"%",5,20); //message popup if(messageTimer>0){ int msgY = constrain( messageTimer-21, -21, -1 ); roundRect(130, msgY ,width-2*130,20, lerpColor(color(64), color(192,192,10), messageFlashTimer/20f), color(128) ); fill(192); textAlign( CENTER ); text(message,width/2,msgY+13); } //HUD data readout textAlign( LEFT ); roundRect(width-115,-1,150,27, lerpColor(color(64), color(192,192,10), resourceTimer/20f), color(128)); fill(192); text("cycles: "+cycles,width-80,10); text("memory: "+memory,width-80,20); // the tab menu if(defenseCategoryButton.selected) roundRect(toolbarX-5,toolbarY-5,27,6+20*defenseButtons.length); if(toolCategoryButton.selected) roundRect(toolbarX-5,toolbarY-5,27,6+20*toolButtons.length); if(researchCategoryButton.selected) roundRect(toolbarX-5,toolbarY-5,27,6+20*researchButtons.length); for(int i=0;i