//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 */ // ----------- GAME SETTINGS ----------------- // int playerSize = 10; // radius in pixels int playerSpeed = 8; // move amount in pixels per frame TODO: make this time based int kickInterval = 5; // spawn a kick pulser every #th kick int kickRadius = 30; // in pixels float kickPulseAmount = 1.3f; // percentage color kickColor = color(255, 0, 0); int snareInterval = 5; // spawn a snare pulser every #th snare int snareRadius = 20; // in pixels float snarePulseAmount = 1.3f; // percentage color snareColor = color(0, 0, 255); int hatInterval = 5; // spawn a hat pulser every #th hat int hatRadius = 10; // in pixels float hatPulseAmount = 1.3f; // percentage color hatColor = color(255, 0, 255); int maxPulsers = 20; // maximum number of pulsers allowed on screen float glowIntensity = 1.3f; float suckStrength = 3f; // larger number means stronger sucking float blowStrength = 100f; // larger number means stronger blowing // ------------------- END GAME SETTINGS ----------------------- // PFont arial; PS2Controller gamepad; ParticleSystem phys = new ParticleSystem(0, .1); Player pl; Pulser puls; Vector pulsers = new Vector(15, 15); Iterator pIter; Iterator pIterb; Nest n; Vector nests = new Vector(3, 0); Iterator nIter; AudioInput in; BeatDetect beat; int kickWait = 0; int snareWait = 0; int hatWait = 0; int totalCaught = 0; boolean debug = false; boolean screenStats = false; boolean drawGlow = true; boolean drawGhost = true; boolean useSmooth = false; boolean useGamepad = false; void setup() { // setup the environment size(1024, 768, P3D); background(0); noCursor(); ellipseMode(CENTER_RADIUS); framerate(30); arial = loadFont("ArialMT-48.vlw"); textFont(arial, 24); 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(phys.makeParticle(1, width/2, height/2, 0), playerSize, playerSpeed); // 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 ( drawGhost ) { fill(0, 90); rect(0, 0, width, height); } else { background(0); } phys.tick(); // Update the player if ( useGamepad ) { pl.move(gamepad.leftX(), gamepad.leftY()); pl.suck( gamepad.L1.pressed() ); pl.blow( gamepad.R1.pressed() ); } else { pl.keyMove(); pl.keySuck(); pl.keyBlow(); } // make new pulsers from beat if there are fewers than maxPulsers on the board if ( pulsers.size() < maxPulsers + 1 ) { if ( beat.isKick() ) { if ( kickWait == 0 ) { if ( debug ) println("Adding a kick."); Pulser k = new Pulser(0, kickRadius, kickColor, kickPulseAmount); pulsers.add(k); kickWait = kickInterval; } else kickWait--; } if ( beat.isSnare() ) { if ( snareWait == 0 ) { if ( debug ) println("Adding a snare."); Pulser s = new Pulser(1, snareRadius, snareColor, snarePulseAmount); pulsers.add(s); snareWait = snareInterval; } else snareWait--; } if ( beat.isHat() ) { if ( hatWait == 0 ) { if ( debug ) println("Adding a hat."); Pulser h = new Pulser(2, hatRadius, hatColor, hatPulseAmount); pulsers.add(h); hatWait = hatInterval; } else hatWait--; } } // make new nests, maybe if ( nests.size() < 3 && random(1) < 0.008) { nests.add(new Nest(frameCount%3, int(random(200, width-200)), int(random(200, height-200)))); } // manage the nests nIter = nests.iterator(); while ( nIter.hasNext() ) { n = (Nest)nIter.next(); if ( n.age() > 8 ) { nIter.remove(); } else { n.render(); } } // pulsing, collision detection, suck/blow, and rendering pIter = pulsers.iterator(); while ( pIter.hasNext() ) { puls = (Pulser)pIter.next(); if ( puls.isOffScreen() ) { pIter.remove(); puls.kill(); continue; } boolean caught = false; nIter = nests.iterator(); while ( nIter.hasNext() ) { n = (Nest)nIter.next(); if ( n.caught(puls) ) { pIter.remove(); puls.render(); puls.kill(); caught = true; totalCaught++; break; } } if ( caught ) continue; puls.rendered = false; switch(puls.type) { case 0: if ( beat.isKick() ) puls.pulse(); break; case 1: if ( beat.isSnare() ) puls.pulse(); break; case 2: if ( beat.isHat() ) puls.pulse(); break; } puls.look(); if ( pl.isSucking() && pl.withinRange(puls) ) puls.attach(); else puls.detach(); puls.playerCollide(); // this needs to happen here because it sets the pulsers velocity to zero. if ( pl.isBlowing() && pl.withinRange(puls) ) { float cx = puls.x() - pl.x(); float cy = puls.y() - pl.y(); float d = puls.distFrom(pl); puls.setVelocity(blowStrength*cx/d, blowStrength*cy/d); } puls.wallBounce(); pIterb = pulsers.iterator(); while ( pIterb.hasNext() ) { puls.pulseBounce((Pulser)pIterb.next()); } puls.render(); } pl.render(); fill(255, 0, 0); textSize(24); text("Total saved: " + totalCaught, 800, 30); if ( true ) { fill(255, 0, 0); textSize(24); 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; } } 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(); }