// Gyration Air mouse shooter game // Like holodeck phaser practice in star trek // By Les Hall // parameters 2*pi*100 => float sensitivity; 0.1 => float alpha; 1000 => float frequencyMultiplier; pi/8 => float targetSize; // variables int score; float phaserTheta; float phaserPhi; float targetTheta; float targetPhi; float angularDistance; float frequency; // the patch SINEWAVE sinewave; PHASER phaser; sinewave.output => dac; phaser.output => dac; SCOREKEEPER scoreKeeper; scoreKeeper.output => dac; class SCOREKEEPER { SawOsc scoreOsc => Gain output; 0 => scoreOsc.gain; 440 => scoreOsc.freq; function void hitSound(int num) { for (int i; i scoreOsc.gain; 50::ms => now; 0 => scoreOsc.gain; 50::ms => now; } } function void hit(int level) { score++; hitSound(score); <<<"Score: ", score>>>; } } // sine wave target class SINEWAVE { SinOsc targetOsc => Gain output; Noise targetExplode => output; 0 => targetExplode.gain; function void update() { targetTheta - phaserTheta => float dTheta; targetPhi - phaserPhi => float dPhi; Math.sqrt(dTheta*dTheta + dPhi*dPhi) => angularDistance; if (angularDistance < targetSize ) targetSize => angularDistance; frequencyMultiplier / angularDistance => frequency; frequency => targetOsc.freq; } function void shoot() { if (angularDistance <= targetSize) { 1 => targetExplode.gain; 500::ms => now; 0 => targetExplode.gain; scoreKeeper.hit(1); Std.rand2f(-pi/2, pi/2) => targetTheta; Std.rand2f(-pi/4, pi/4) => targetPhi; } } } // Phaser (Air Mouse) class class PHASER { SinOsc phaserOsc => Gain output; 1000 => phaserOsc.freq; 0 => phaserOsc.gain; // variables float dx; float dy; // make HidIn and HidMsg Hid hi; HidMsg msg; // which mouse 0 => int device; // get from command line if( me.args() ) me.arg(0) => Std.atoi => device; // open mouse 0, exit on fail if( !hi.openMouse( device ) ) me.exit(); <<< "mouse '" + hi.name() + "' ready", "" >>>; function void fireSound() { 1000 => phaserOsc.freq; 1 => phaserOsc.gain; 100::ms => now; 0 => phaserOsc.gain; } function void fire() { fireSound(); sinewave.shoot(); } function void loop() { // infinite event loop while( true ) { // wait on HidIn as event hi => now; // messages received while( hi.recv( msg ) ) { // mouse motion if( msg.isMouseMotion() ) { if( msg.deltaX ) { //<<< "mouse motion:", msg.deltaX, "on x-axis" >>>; alpha * msg.deltaX + (1-alpha) * dx => dx; dx / sensitivity +=> phaserTheta; if (phaserTheta > pi) pi -=> phaserTheta; else if (phaserTheta < (-pi) ) pi +=> phaserTheta; } if( msg.deltaY ) { //<<< "mouse motion:", msg.deltaY, "on y-axis" >>>; alpha * msg.deltaY + (1-alpha) * dy => dy; dy / sensitivity +=> phaserPhi; if (phaserPhi > pi/2) 2*pi - phaserPhi => phaserPhi; else if (phaserPhi < (-pi) ) -2*pi -phaserPhi +=> phaserPhi; } // update target sinewave.update(); } // mouse button down else if( msg.isButtonDown() ) { //<<< "mouse button", msg.which, "down" >>>; if (msg.which == 0) 0 => phaserTheta => phaserPhi; else if (msg.which == 2) fire(); } // mouse button up else if( msg.isButtonUp() ) { //<<< "mouse button", msg.which, "up" >>>; } // mouse wheel motion else if( msg.isWheelMotion() ) { if( msg.deltaX ) { //<<< "mouse wheel:", msg.deltaX, "on x-axis" >>>; } if( msg.deltaY ) { //<<< "mouse wheel:", msg.deltaY, "on y-axis" >>>; } } } } } spork ~ loop(); } // time loop while (true) { //<<>>; second => now; }