//import processing.opengl.*; import procontroll.*; import net.java.games.input.*; import traer.physics.*; import krister.Ess.*; /** Use the arrow keys for movement, 'a' for the sucking force, 's' for the blowing force
'g' to toggle the glow effect if the game is sluggish */ // ----------- GAME SETTINGS ----------------- // int playerSize = 10; // radius in pixels int playerSpeed = 8; // move amount in pixels per frame TODO: make this time based float nestSize = 110; // radius in pixels int kickInterval = 5; // spawn a kick pulser every #th kick int kickRadius = 31; // in pixels float kickWeight = 1f; color kickColor = color(255, 179, 40); int snareInterval = 5; // spawn a snare pulser every #th snare int snareRadius = 23; // in pixels float snareWeight = 1.5f; color snareColor = color(210, 41, 42); int hatInterval = 5; // spawn a hat pulser every #th hat int hatRadius = 17; // in pixels float hatWeight = 2f; color hatColor = color(255, 45, 139); float pulseAmt = 1.3f; // percentage of how much larger they will get int maxPulsers = 20; // maximum number of pulsers allowed on screen float glowIntensity = 128; float suckStrength = 3f; // larger number means stronger sucking float blowStrength = 0.8f; // larger number means stronger blowing // ------------------- END GAME SETTINGS ----------------------- // PFont arial; PS2Controller gamepad; ParticleSystem phys = new ParticleSystem(0, .2f); Player pl; Pulser puls; Vector pulsers = new Vector(15, 15); Nest n; Vector nests = new Vector(3, 0); Deathskull skull; Vector skulls = new Vector(2, 0); SuckBlow sucker; Vector suckers = new Vector(2, 0); AudioInput in; BeatDetect beat; Countdown count; int kickWait = 0; int snareWait = 0; int hatWait = 0; int totalCaught = 0; int score = 0; int playTime = 0; boolean debug = false; boolean screenStats = true; boolean playing = false; boolean pause = false; boolean drawGlow = false; boolean drawGhost = true; boolean useGamepad = false; boolean time = false; int timer = 0; void setup() { // setup the environment size(1024, 768, P3D); background(0); noCursor(); //smooth(); ellipseMode(CENTER_RADIUS); frameRate(30); arial = loadFont("ArialMT-24.vlw"); textFont(arial); if ( useGamepad ) { // setup the gamepad gamepad = new PS2Controller(this); gamepad.leftStick.setMultiplier(playerSpeed); gamepad.leftStick.setTolerance(0.3f); gamepad.rightStick.setTolerance(0.15f); gamepad.invertRightY = true; } // create the player pl = new Player(); // create the countdown count = new Countdown(3); // setup the sound Ess.start(this); in = new AudioInput(1024, 44100); beat = new BeatDetect(in.size, in.sampleRate); beat.detectMode("FREQ_ENERGY"); beat.detectDetail(64); in.start(); } void draw() { if ( pause ) { delay(5000); pause = false; } if ( !playing && ( keyPressed || ( useGamepad && gamepad.Start.pressed() ) ) ) { pulsers.clear(); suckers.clear(); skulls.clear(); nests.clear(); totalCaught = 0; score = 0; playTime = 0; playing = true; count.start(); } if ( count.running ) { background(0); count.update(); } else { if ( drawGhost ) { fill(0, 90); rect(0, 0, width, height); } else { background(0); } game(); playTime++; if ( playing && playTime > 300*frameRate ) { fill(255, 0, 0); textSize(40); textAlign(CENTER); text("Time's Up!", width/2, 300); text("Pulsers saved: " + totalCaught, width/2, 400); text("Score: " + score, width/2, 500); textSize(24); textAlign(LEFT); playing = false; pl.reset(); pause = true; } } } void game() { phys.tick(); // Update the player if ( playing ) { if ( useGamepad ) { pl.move(gamepad.leftX(), gamepad.leftY()); pl.suck( gamepad.X.pressed() ); pl.blow( gamepad.O.pressed() ); } else { pl.keyMove(); pl.keySuck(); pl.keyBlow(); } } // make new pulsers from beat if there are fewer than maxPulsers on the board if ( pulsers.size() < maxPulsers ) { if ( beat.isKick() ) { if ( kickWait == 0 ) { if ( debug ) println("Adding a kick."); Pulser k = new Pulser(KICK); pulsers.add(k); kickWait = kickInterval; } else kickWait--; } if ( beat.isSnare() ) { if ( snareWait == 0 ) { if ( debug ) println("Adding a snare."); Pulser s = new Pulser(SNARE); pulsers.add(s); snareWait = snareInterval; } else snareWait--; } if ( beat.isHat() ) { if ( hatWait == 0 ) { if ( debug ) println("Adding a hat."); Pulser h = new Pulser(HAT); pulsers.add(h); hatWait = hatInterval; } else hatWait--; } } // make new nest, maybe if ( nests.size() < 2 && random(1) < 0.008) { // generate a possible position float x = random(nestSize+10, width-(nestSize+10)); float y = random(nestSize+10, height-(nestSize+10)); // check for overlap with existing nests for (int i = 0; i < nests.size(); i++) { Nest tmp = (Nest)nests.get(i); if ( dist(x, y, tmp.x(), tmp.y()) < 200 ) { x = random(nestSize+10, width-(nestSize+10)); y = random(nestSize+10, height-(nestSize+10)); i = -1; } } nests.add(new Nest(x, y)); } // manage the nests for (int i = 0; i < nests.size(); i++) { n = (Nest)nests.get(i); if ( n.age() > 15 ) { n.kill(); nests.remove(i); i--; } else { n.update(); n.render(); } } // make new deathskull, maybe if ( skulls.size() + suckers.size() < 2 && random(1) < 0.008 ) { skulls.add(new Deathskull()); } // manage the deathskulls for (int i = 0; i < skulls.size(); i++) { skull = (Deathskull)skulls.get(i); if ( skull.isOffScreen() ) skull.kill(); if ( skull.isDead() ) { skulls.remove(i); i--; } else { pl.interact(skull); skull.update(); skull.render(); } } // make a new sucker/blower, maybe if ( skulls.size() + suckers.size() < 2 && random(1) < 0.008 ) { suckers.add(new SuckBlow()); } // manage the sucker/blowers for (int i = 0; i < suckers.size(); i++) { sucker = (SuckBlow)suckers.get(i); if ( sucker.isOffScreen() ) sucker.kill(); if ( sucker.isDead() ) { suckers.remove(i); i--; } else { pl.interact(sucker); sucker.update(); sucker.render(); } } if ( time ) timer = millis(); // pulsing, nest interaction, player interaction, enemy interaction, // collision and rendering for (int i = 0; i < pulsers.size(); i++) { puls = (Pulser)pulsers.get(i); if ( puls.isDead() ) { pulsers.remove(i); i--; continue; } if ( puls.isOffScreen() ) { puls.kill(); } if ( puls.dying ) { puls.render(); puls.deathTick(); continue; } for (int j = 0; j < nests.size(); j++) { if ( !puls.caught ) ((Nest)nests.get(j)).tryCatch(puls); } if ( puls.caught ) { puls.caughtTick(); puls.setMood(GLEE); } puls.rendered = false; puls.look(puls.x(), puls.y()); if ( puls.isMoving() ) puls.setMood(HAPPY); else puls.resetMood(); // pulse according to type switch(puls.type) { case KICK: if ( beat.isKick() ) puls.pulse(); break; case SNARE: if ( beat.isSnare() ) puls.pulse(); break; case HAT: if ( beat.isHat() ) puls.pulse(); break; } // interact with player pl.interact(puls); puls.interact(pl); // interact with enemies for (int j = 0; j < skulls.size(); j++) { skull = (Deathskull)skulls.get(j); puls.interact(skull); if ( skull.awareOf(puls) ) skull.target(puls); } for (int j = 0; j < suckers.size(); j++) { sucker = (SuckBlow)suckers.get(j); puls.interact(sucker); if ( sucker.awareOf(puls) ) sucker.interact(puls); } puls.wallBounce(); // collide with other pulsers for (int j = 0; j < pulsers.size(); j++) { Pulser tmp = (Pulser)pulsers.get(j); if ( tmp != puls ) puls.pulseBounce(tmp); } puls.render(); } if ( time ) println(frameCount + ": managing pulsers took " + (millis()-timer)); pl.render(); fill(255, 0, 0); if ( playing ) { text("Total saved: " + totalCaught, 800, 30); text("Score: " + score, 800, 60); } if ( screenStats ) { fill(255, 0, 0); text("Frame Rate: " + frameRate, 5, 30); text("Total pulsers: " + pulsers.size(), 5, height - 15); } if ( debug ) { println(Runtime.getRuntime().freeMemory()); println("End draw, frame " + frameCount); } } void keyPressed() { if ( key == CODED ) { switch(keyCode) { case UP: pl.up = true; break; case RIGHT: pl.right = true; break; case DOWN: pl.down = true; break; case LEFT: pl.left = true; break; } } else { if ( key == 'a' ) pl.suck = true; else if ( key == 's' ) pl.blow = true; else if ( key == 'g' ) drawGlow = !drawGlow; } } void keyReleased() { if ( key == CODED ) { switch(keyCode) { case UP: pl.up = false; break; case RIGHT: pl.right = false; break; case DOWN: pl.down = false; break; case LEFT: pl.left = false; break; } } else { if ( key == 'a' ) pl.suck = false; else if ( key == 's' ) pl.blow = false; } } public void audioInputData(AudioInput in) { beat.detect(in); } public void stop() { Ess.stop(); super.stop(); }