import procontroll.*; import net.java.games.input.*; import traer.physics.*; import krister.Ess.*; // ----------- GAME SETTINGS ----------------- // int playerSize = 10; // radius in pixels int playerSpeed = 5; // between 1 and 10 int kickInterval = 4; // spawn a kick pulser every #th kick int kickRadius = 20; // in pixels int kickFollowDistance = 50; // in pixels float kickPulseAmount = 1.3f; // percentage color kickColor = color(255, 0, 0, 100); int snareInterval = 4; // spawn a snare pulser every #th snare int snareRadius = 15; // in pixels int snareFollowDistance = 50; // in pixels float snarePulseAmount = 1.3f; // percentage color snareColor = color(0, 0, 255, 100); int hatInterval = 4; // spawn a hat pulser every #th hat int hatRadius = 10; // in pixels int hatFollowDistance = 50; // in pixels float hatPulseAmount = 1.3f; // percentage color hatColor = color(255, 0, 255, 100); int maxMove = 10; // sets the fastest move speed for the controller in pixels per frame int maxAge = 3; // in seconds boolean ageOutFollowers = false; // remove followers older than make age? float repulsionStrength = 300f; // larger number means the pulser move away from each other faster float springStrength = .5f; // Larger numbers make for stiffer springs. float springDamping = 1f; // If springs have high damping they don't overshoot and they settle down quickly, // with low damping springs oscillate. // ------------------- END GAME SETTINGS ----------------------- // PS2Controller gamepad; PulserList all = new PulserList(); PulserList kicks = new PulserList(); PulserList snares = new PulserList(); PulserList hats = new PulserList(); PulserList floaters = new PulserList(); PulserList followers = new PulserList(); ParticleSystem phys = new ParticleSystem(0, .1); Particle[] partPair = new Particle[2]; Player pl; Pulser pul; Spring tmpSpr; AudioFile daft; AudioStream out; BeatDetect beat; int kickWait = 0; int snareWait = 0; int hatWait = 0; void setup() { size(800, 800); noCursor(); rectMode(CENTER); framerate(24); smooth(); // gamepad = new PS2Controller(this); // gamepad.leftStick.setMultiplier(maxMove); // gamepad.leftStick.setTolerance(0.3f); pl = new Player(phys.makeParticle(1, width/2, height/2, 0), playerSize, playerSpeed); pul = null; Ess.start(this); daft = new AudioFile("funny_break.mp3", 44100, Ess.READ, Ess.LEFT); out = new AudioStream(1024); out.sampleRate(daft.sampleRate); beat = new BeatDetect(out.size, out.sampleRate); beat.detectMode("FREQ_ENERGY"); beat.detectDetail(64); out.start(); } void draw() { println("Start draw, frame " + frameCount); background(0); phys.tick(); // remove old pulsers for ( pul = all.next(); pul != null; pul = all.next() ) { if ( pul.follow && !ageOutFollowers ) continue; if ( pul.p.age()/framerate > maxAge ) { all.remove(pul); floaters.remove(pul); kicks.remove(pul); snares.remove(pul); hats.remove(pul); if ( pul.follow ) { followers.remove(pul); int partCnt = 0; int n = phys.numberOfSprings(); for ( int i = 0; i < n && partCnt < 2; i++) { tmpSpr = phys.getSpring(i); if ( tmpSpr.getOneEnd() == pul.p ) { partPair[partCnt] = tmpSpr.getOneEnd(); partCnt++; } else if ( tmpSpr.getTheOtherEnd() == pul.p ) { partPair[partCnt] = tmpSpr.getTheOtherEnd(); partCnt++; } } phys.makeSpring(partPair[0], partPair[1], springStrength, springDamping, pul.followDist); } pul.kill(); } } // make new pulsers from beat if ( beat.isKick() ) { if ( kickWait == 0 ) { println("Adding a kick."); Pulser k = new Pulser(phys.makeParticle(1, random(width), random(height), 0), kickRadius, kickFollowDistance, kickPulseAmount, kickColor); while ( (pul = all.next()) != null ) phys.makeAttraction(pul.p, k.p, -repulsionStrength, 5f); all.add(k); kicks.add(k); floaters.add(k); kickWait = kickInterval; } else { kickWait--; } } if ( beat.isSnare() ) { if ( snareWait == 0 ) { println("Adding a snare."); Pulser s = new Pulser(phys.makeParticle(1, random(width), random(height), 0), snareRadius, snareFollowDistance, snarePulseAmount, snareColor); while ( (pul = all.next()) != null ) phys.makeAttraction(pul.p, s.p, -repulsionStrength, 5f); all.add(s); floaters.add(s); snares.add(s); snareWait = snareInterval; } else { snareWait--; } } if ( beat.isHat() ) { if ( hatWait == 0 ) { println("Adding a hat."); Pulser h = new Pulser(phys.makeParticle(1, random(width), random(height), 0), hatRadius, hatFollowDistance, hatPulseAmount, hatColor); while ( (pul = all.next()) != null ) phys.makeAttraction(pul.p, h.p, -repulsionStrength, 5f); all.add(h); floaters.add(h); hats.add(h); hatWait = hatInterval; } else { hatWait--; } } // pulse the pulser groups for ( pul = kicks.next(); pul != null; pul = kicks.next() ) { if ( beat.isKick() ) pul.pulse(); } for ( pul = snares.next(); pul != null; pul = snares.next() ) { if ( beat.isSnare() ) pul.pulse(); } for ( pul = hats.next(); pul != null; pul = hats.next() ) { if ( beat.isHat() ) pul.pulse(); } // collision detection, pickup, and rendering println(" Iterating all: " + all.length()); for ( pul = all.next(); pul != null; pul = all.next() ) { pul.rendered = false; if ( pul.isOffScreen() ) { println(" Pul is off screen."); all.remove(pul); followers.remove(pul); floaters.remove(pul); kicks.remove(pul); snares.remove(pul); hats.remove(pul); if ( pul.follow ) { int partCnt = 0; int n = phys.numberOfSprings(); for ( int i = 0; i < n && partCnt < 2; i++) { tmpSpr = phys.getSpring(i); if ( tmpSpr.getOneEnd() == pul.p ) { partPair[partCnt] = tmpSpr.getOneEnd(); partCnt++; } else if ( tmpSpr.getTheOtherEnd() == pul.p ) { partPair[partCnt] = tmpSpr.getTheOtherEnd(); partCnt++; } } phys.makeSpring(partPair[0], partPair[1], springStrength, springDamping, pul.followDist); } pul.kill(); continue; } // println(" Check distance."); if ( pul.isFree() && pul.distFrom(pl) < 50 ) { println(" Follow."); pul.follow(); println(" Remove from floaters."); floaters.remove(pul); println(" Floaters length = " + floaters.length() + " All length = " + all.length()); println(" Make the spring."); if ( followers.isEmpty() ) { phys.makeSpring(pl.p, pul.p, springStrength, springDamping, pul.followDist); followers.add(pul); } else { phys.makeSpring(followers.last.puls.p, pul.p, springStrength, springDamping, pul.followDist); followers.add(pul); } } //println(" Wall bounce."); pul.wallBounce(); Pulser f; //println(" Iterating floaters."); for ( f = floaters.next(); f != null; f = floaters.next() ) { pul.pulseBounce(f); } //println(" Iterating followers."); for ( f = followers.next(); f != null; f = followers.next() ) { pul.pulseBounce(f); } pul.render(); } //println(" Rendering player."); //pl.move( round(gamepad.leftX()), round(gamepad.leftY()) ); pl.render(); //beat.drawGraph(); //println(Runtime.getRuntime().freeMemory()); println("End draw, frame " + frameCount); } void keyPressed() { if ( key == CODED ) { //println("moving in frame " + frameCount); pl.keyMove(); } else { if ( key == 'a' ) pl.speedUp(); else if ( key == 's' ) pl.slowDown(); } } void audioStreamWrite(AudioStream theStream) { beat.detect(out); // read the next chunk int samplesRead = daft.read(out); if (samplesRead == 0) { // start over daft.close(); daft.open("funny_break.mp3", out.sampleRate, Ess.READ, Ess.LEFT); samplesRead = daft.read(out); } } public void stop() { Ess.stop(); super.stop(); }