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 = 15; // radius in pixels int playerSpeed = 8; // move amount in pixels per frame TODO: make this time based int kickInterval = 6; // spawn a kick pulser every #th kick int kickRadius = 40; // in pixels int kickFollowDistance = 50; // in pixels float kickPulseAmount = 1.3f; // percentage color kickColor = color(255, 0, 0, 100); int snareInterval = 6; // spawn a snare pulser every #th snare int snareRadius = 30; // in pixels int snareFollowDistance = 50; // in pixels float snarePulseAmount = 1.3f; // percentage color snareColor = color(0, 0, 255, 100); int hatInterval = 6; // spawn a hat pulser every #th hat int hatRadius = 20; // in pixels int hatFollowDistance = 50; // in pixels float hatPulseAmount = 1.3f; // percentage color hatColor = color(255, 0, 255, 100); int maxAge = 5; // maximum age for free pulsers, in seconds float glowIntensity = 2.5; float suckStrength = 50000f; // larger number means stronger sucking float repulsionStrength = 300f; // larger number means the pulsers move away from each other faster // ------------------- END GAME SETTINGS ----------------------- // PFont arial; PS2Controller gamepad; PulserList all = new PulserList(); PulserList kicks = new PulserList(); PulserList snares = new PulserList(); PulserList hats = new PulserList(); ParticleSystem phys = new ParticleSystem(0, .1); Player pl; Pulser pul; AudioInput in; BeatDetect beat; int kickWait = 0; int snareWait = 0; int hatWait = 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(800, 800, P3D); 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() { background(0); if ( true ) { textSize(24); text("Frame Rate: " + framerate, 5, 20); } phys.tick(); // Update the player if ( useGamepad ) { pl.move(gamepad.leftX(), gamepad.leftY()); pl.aim(gamepad.rightX(), gamepad.rightY()); pl.suck( gamepad.L1.pressed() ); pl.blow( gamepad.R1.pressed() ); } else { pl.keyMove(); pl.keySuck(); pl.keyBlow(); } // remove old pulsers while ( (pul = all.next()) != null ) { if ( pul.a.isOn() ) continue; if ( pul.p.age()/framerate > maxAge ) { all.remove(pul); kicks.remove(pul); snares.remove(pul); hats.remove(pul); pul.kill(); } } // make new pulsers from beat if ( beat.isKick() ) { if ( kickWait == 0 ) { if ( debug ) println("Adding a kick."); Pulser k = new Pulser(phys.makeParticle(1, random(width), random(height), 0), kickRadius, kickColor, kickPulseAmount); k.a = phys.makeAttraction(pl.p, k.p, suckStrength, 0); k.a.turnOff(); while ( (pul = snares.next()) != null ) phys.makeAttraction(pul.p, k.p, -repulsionStrength, 5f); while ( (pul = hats.next()) != null ) phys.makeAttraction(pul.p, k.p, -repulsionStrength, 5f); all.add(k); kicks.add(k); kickWait = kickInterval; } else { kickWait--; } } if ( beat.isSnare() ) { if ( snareWait == 0 ) { if ( debug ) println("Adding a snare."); Pulser s = new Pulser(phys.makeParticle(1, random(width), random(height), 0), snareRadius, snareColor, snarePulseAmount); s.a = phys.makeAttraction(pl.p, s.p, suckStrength, pl.radius); s.a.turnOff(); while ( (pul = kicks.next()) != null ) phys.makeAttraction(pul.p, s.p, -repulsionStrength, 5f); while ( (pul = hats.next()) != null ) phys.makeAttraction(pul.p, s.p, -repulsionStrength, 5f); all.add(s); snares.add(s); snareWait = snareInterval; } else { snareWait--; } } if ( beat.isHat() ) { if ( hatWait == 0 ) { if ( debug ) println("Adding a hat."); Pulser h = new Pulser(phys.makeParticle(1, random(width), random(height), 0), hatRadius, hatColor, hatPulseAmount); h.a = phys.makeAttraction(pl.p, h.p, suckStrength, pl.radius); h.a.turnOff(); while ( (pul = kicks.next()) != null ) phys.makeAttraction(pul.p, h.p, -repulsionStrength, 5f); while ( (pul = snares.next()) != null ) phys.makeAttraction(pul.p, h.p, -repulsionStrength, 5f); all.add(h); hats.add(h); hatWait = hatInterval; } else { hatWait--; } } // pulse the pulser groups while ( (pul = kicks.next()) != null ) { if ( beat.isKick() ) pul.pulse(); } while ( (pul = snares.next()) != null ) { if ( beat.isSnare() ) pul.pulse(); } while ( (pul = hats.next()) != null ) { if ( beat.isHat() ) pul.pulse(); } // collision detection, suck/blow, and rendering if ( debug ) println(" Iterating all: " + all.length()); while ( (pul = all.next()) != null ) { pul.rendered = false; pul.move(); if ( pul.isOffScreen() ) { if ( debug ) println(" Pul is off screen."); all.remove(pul); kicks.remove(pul); snares.remove(pul); hats.remove(pul); pul.kill(); continue; } if ( pl.isSucking() && pl.withinRange(pul) ) pul.a.turnOn(); else pul.a.turnOff(); if ( pl.isBlowing() && pl.withinRange(pul) ) { float cx = pul.x() - pl.x(); float cy = pul.y() - pl.y(); pul.p.setVelocity(cx/2, cy/2, 0); pul.dampVelocity(); } if ( debug ) println(" Wall bounce."); pul.wallBounce(); if ( debug ) println(" Pulse bounce"); Pulser p; while ( (p = kicks.next()) != null ) { pul.pulseBounce(p); } while ( (p = snares.next()) != null ) { pul.pulseBounce(p); } while ( (p = hats.next()) != null ) { pul.pulseBounce(p); } pul.playerCollide(pl); pul.render(); } pl.render(); 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(); }