In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

MotionTrack パーティクル崩し forked from: パーティクル崩し

//http://wonderfl.net/c/ovlEのコード「Arpeggiatorを使う」と合わせてみました。
// http://wonderfl.net/c/7ZmRの魔法もどきパーティクル(MotionTrack Particle)のMotionTrackクラスを使う練習
/**
 * Copyright yabuchany ( http://wonderfl.net/user/yabuchany )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/sfPm
 */

// forked from yabuchany's forked from: パーティクル崩し
// forked from coppieee's パーティクル崩し
// forked from coppieee's パーティクル崩し
//コードはめっちゃ汚いよ!
//ブロックの数465*100
//がんばってクリアしてください
//http://wonderfl.net/c/ovlEのコード「Arpeggiatorを使う」と合わせてみました。
// http://wonderfl.net/c/7ZmRの魔法もどきパーティクル(MotionTrack Particle)のMotionTrackクラスを使う練習
package 
{
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Sprite;
    import flash.events.Event;
    //import flash.events.MouseEvent;
    import flash.geom.ColorTransform;
    import flash.geom.Matrix;
    import flash.geom.Rectangle;
    import flash.text.TextField;
    import flash.text.TextFieldAutoSize;
    //import net.hires.debug.Stats;
    //sion----------------------------------------------------------
    import org.si.sion.SiONData;
    import org.si.sion.SiONDriver;
    import org.si.sion.events.SiONEvent;
    import org.si.sion.utils.Scale;
    import org.si.sion.utils.SiONPresetVoice;
    import org.si.sound.Arpeggiator;    
    //sion----------------------------------------------------------
    
    // MotionTracker
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.TimerEvent;
    import flash.media.Camera;
    import flash.media.Video;
    import flash.utils.Timer;    
    // MotionTracker
    
    
    [SWF(width = "465", height = "465", frameRate = "30")]
    public class BlockBreaker extends Sprite 
    {
        private static const HEIGHT:Number = 465;
        private static const WIDTH:Number = 465;
        private var _canvas:BitmapData;
        private var _blocks:Blocks;
        private var _fallBlocks:Vector.<Particle>;
        private var _balls:Vector.<Particle>;
        private var _bar:Bitmap;
        //sion----------------------------------------------------------
        private var arpeggiator:Arpeggiator;
        public var driver:SiONDriver = new SiONDriver();
        private var rythmLoop:SiONData;
        private var presetVoice:SiONPresetVoice = new SiONPresetVoice();
        
        private var px:Number=0;
        private var py:Number=0;
        //sion----------------------------------------------------------
        
        // MotionTracker
        private var _cam:Camera;
        private var _tracker:Tracker;
        private var _px:Number = 465 * .5
        private var _py:Number = 465 * .5

        private var _isMotion:Boolean;        
        // MotionTracker
        
        public function BlockBreaker()
        {
             initVideo();
            _canvas = new BitmapData(WIDTH, HEIGHT, false, 0x000000);
            
            addChild(new Bitmap(_canvas));
            
            _blocks = new Blocks(WIDTH, 100);
            
            _fallBlocks = new Vector.<Particle>();
            
            var b:BitmapData = new BitmapData(200, 10, false, 0x00FF00);
            addChild(_bar = new Bitmap(b));
            //_bar.y = WIDTH -b.width;
            _bar.y = 450;
            var _ball:Particle = new Particle(WIDTH / 2, HEIGHT / 2);
            _ball.vx = Math.random() *10;
            _ball.vy = -Math.random() *9 -1;
            _ball.color = 0xFFFFFF;
            
            _balls = new Vector.<Particle>();
            _balls.push(_ball);
            
            //var stats:Stats = new Stats();
            //stats.y = 200;
            //addChild(stats);
            //sion----------------------------------------------------------
            initSound();
            soundStart();
            //sion----------------------------------------------------------
            addEventListener(Event.ENTER_FRAME, update1);
        }
        private function update1(e:Event):void
        {
            _canvas.lock();
            _canvas.colorTransform(_canvas.rect, new ColorTransform (0.9, 0.5, 0.9));
            
            for each(var block:Particle in _blocks.values)
            {
                if (block)
                {
                    _canvas.setPixel(block.x, block.y, block.color);
                }
            }
            var removeBalls:Vector.<Particle> = new Vector.<Particle>();
             for each(var ball:Particle in _balls)
             {
                var bvx:Number = ball.vx;
                var bvy:Number = ball.vy;
                var bspeed:Number = Math.sqrt(bvx * bvx + bvy * bvy);
                var bradius:Number = Math.atan2(bvy, bvx);
                for (var i:int = 0; i < bspeed;i++)
                {
                    ball.x += ball.vx/bspeed;
                    ball.y += ball.vy/bspeed;
                    var hitParticle:Particle = _blocks.getParticle(ball.x, ball.y);
                    if(hitParticle)
                    {
                        var removedP:Particle = _blocks.removeParticle(ball.x, ball.y);
                        removedP.vx = Math.cos(bradius+Math.PI*2/(30*Math.random())-15)*3;
                        removedP.vy = 1;
                        removedP.color = hitParticle.color;
                        _fallBlocks.push(removedP);
                        ball.vy = -ball.vy;
                    }
                    
                    if ((ball.x < 0 && ball.vx < 0) || (ball.x > WIDTH && ball.vx > 0))
                    {
                        ball.vx = -ball.vx;
                    }
                    if (ball.y < 0 && ball.vy < 0)
                    {
                        ball.vy = -ball.vy;
                    }
                    if (ball.y > HEIGHT)
                    {
                        removeBalls.push(ball);
                    }
                    if (_bar.hitTestPoint(ball.x, ball.y))
                    {
                        ball.vy = -Math.abs(ball.vy);
                    }
                    _canvas.setPixel(ball.x, ball.y, ball.color);
                }
            }
            removeBalls.forEach(function(b:Particle, ...args):void {
                var index:int = _balls.indexOf(b);
                if (index != -1)
                {
                    _balls.splice(index, 1);
                }
            });
            
            var removeFallBs:Vector.<Particle> = new Vector.<Particle>();
            _fallBlocks.forEach(function(fallP:Particle, ...args):void {
                fallP.vy += 0.1;
                fallP.x += fallP.vx;
                fallP.y += fallP.vy;
                _canvas.setPixel(fallP.x, fallP.y, fallP.color);
                if (_bar.hitTestPoint(fallP.x,fallP.y))
                {
                    var newball:Particle = new Particle(fallP.x,fallP.y);
                    newball.vx = Math.random() * 10;
                    newball.vy = Math.random() * 9 + 1;
                    newball.color = fallP.color;
                    _balls.push(newball);
                    removeFallBs.push(fallP);
                }else if (fallP.y > HEIGHT)
                {
                    removeFallBs.push(fallP);
                }
            });
            
            removeFallBs.forEach(function(b:Particle,...args):void{
                var index:int = _fallBlocks.indexOf(b);
                if (index != -1)
                {
                    _fallBlocks.splice(index, 1);
                }
            });
            //_bar.x = stage.mouseX;
            _bar.x = _tracker.px;
            _canvas.unlock();
            
            if (_blocks.count == 0)
            {
                removeEventListener(Event.ENTER_FRAME, update1);
                var clearTF:TextField = new TextField();
                clearTF.text = "CLEAR!\nおめでと";
                clearTF.textColor = 0xFFFFFF;
                clearTF.autoSize = TextFieldAutoSize.LEFT;
                _canvas.draw(clearTF,new Matrix(5,0,0,5,WIDTH/2-clearTF.width*5/2,HEIGHT/2-clearTF.height*5/2));
            }
            
        }
        //sion----------------------------------------------------------
        private function initSound():void {
            var mml:String = "t132;";
            rythmLoop = driver.compile(mml);
            rythmLoop.setVoice(0, presetVoice["valsound.percus1"]);
            //rythmLoop.setVoice(0, presetVoice["valsound.percus13"]);
            
            arpeggiator = new Arpeggiator(new Scale("o1Ajap"), 1, [0,1,2,5,4,3]);
            //arpeggiator.voice = presetVoice["valsound.piano8"];
            //arpeggiator.voice = presetVoice["valsound.wind1"];
            //arpeggiator.voice = presetVoice["valsound.guitar2"];
            //arpeggiator.voice = presetVoice["valsound.percus25"];
            //arpeggiator.voice = presetVoice["valsound.percus1"];
            //arpeggiator.voice = presetVoice["valsound.lead25"];
            arpeggiator.voice = presetVoice["valsound.percus13"];
            arpeggiator.quantize = 4;
            arpeggiator.volume = 0.3;
            arpeggiator.noteQuantize = 8;
            
            
            driver.addEventListener(SiONEvent.STREAM, onStreamHandler);
            driver.play(rythmLoop);
            
            //bug??
            //arpeggiator.noteOn();
            //arpeggiator.noteOff();
            arpeggiator.pattern = [0,1,2,5,4,3];
        }
        
        
        private function onStreamHandler(e:SiONEvent):void {
            // update arpeggiator pitch and length
            setPxPy();
            arpeggiator.scaleIndex = px * 32;
            arpeggiator.noteLength = [0.5,1,1,2,4][int(py * 4 + 0.99)];
        }
        
        public function soundStart():void {
            setPxPy();
            arpeggiator.scaleIndex = px*32;
            arpeggiator.noteLength = [0.5,1,1,2,4][int(py * 4 + 0.99)];
            // start arpeggio
            arpeggiator.play();
        }
        
        private function setPxPy():void {
            //px = _balls.length / 465;
            px =0.2;
            py = _balls.length / 465;
            if(px<0)px=0;
            if(px>0.9)px=0.9;
            if(py<0)py=0;
            if(py>0.9)py=0.9;
        }
        //sion----------------------------------------------------------
        
        private function initVideo():void {
            var video:Video = new Video(465, 465);
            _cam = Camera.getCamera();
            _cam.setMode(video.width, video.height, 25);
            video.attachCamera(_cam);
   
            _tracker = new Tracker(video);
            addEventListener(Event.ENTER_FRAME, update);
            _tracker.addEventListener("isMotion", motion);
            
            // Check Tracking
            //addChild(_tracker)
        }
        
        private function update(e:Event):void{
            _tracker.track();
            _px = _tracker.px;
            _py = _tracker.py;
    
            if(_px){

                if(_isMotion){
                    //_bar.x = stage.mouseX;
                    _bar.x = _px*0.9;
                    //trace("Motion");
                    trace(_px);
                }
            }
        }
        
        private var _timer:Timer
        
        private function motion(e:Event):void{
            if(_timer) return;
            _isMotion = true;
            //
            _timer = new Timer(1000, 1);
            _timer.addEventListener(TimerEvent.TIMER_COMPLETE, timerComp);
            _timer.start();
            //
            function timerComp():void{
                _isMotion = false;
                _timer = null;
            }
        }        
    }
}
import frocessing.color.ColorHSV;
class Blocks
{
    public function get count():int { return _count;}
    private var _count:int;
    public function get width():Number { return _width; }
    private var _width:Number;
    public function get height():Number { return _height; }
    private var _height:Number;
    public var values:Vector.<Particle>;
    function Blocks(width:Number,height:Number)
    {
        _width = width;
        _height = height;
        _count = width * height;
        values = new Vector.<Particle>(width * height, false);
        var c:ColorHSV = new ColorHSV();
        for (var i:int = 0; i < _width; i++)
        {
            c.h = 360 * i / _width;
            for (var j:int = 0 ; j < _height; j++ )
            {
                var p:Particle = new Particle(i, j);
                p.color = c.value;
                values[i + j * _width] = p;
            }
        }
    }
    public function getParticle(x:int, y:int):Particle
    {
        var index:int = x + y * _width;
        if (index >= values.length || index < 0)
        {
            return null;
        }
        return values[x + y * _width];
    }
    public function removeParticle(x:int, y:int):Particle
    {
        var p:Particle = values[x + y * _width];
        if (p)
        {
            _count--;
            values[x + y * _width] = undefined;
        }
        return p;
    }
}
class Particle
{
    public var x:Number;
    public var y:Number;
    public var vx:Number = 0;
    public var vy:Number = 0;
    public var color:uint;
    public function Particle(x:Number=0,y:Number=0 )
    {
        this.x = x;
        this.y = y;
    }
}
// MotionTrack
import flash.display.BitmapData;
import flash.display.BlendMode;
import flash.display.Sprite;
import flash.events.Event;
import flash.filters.BlurFilter;
import flash.geom.Matrix;
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.media.Video;

class Tracker extends Sprite {
    public var _previous:BitmapData;
    public var _current:BitmapData;
    public var px:Number;
    public var py:Number;
    private var _blur:BlurFilter = new BlurFilter(64,64);
    private var _vid:Video;
    private var _mirror:Matrix;
    private var _point:Point = new Point();
    private var _area:Rectangle;
    private var _isMotion:Boolean = false;

    public function Tracker(vid:Video) {
        _vid = vid;
        _mirror = new Matrix();
        _mirror.scale( -1, 1);
        _mirror.translate(_vid.width, 0);
        _current = new BitmapData(_vid.width,_vid.height,false,0x000000);
        _previous = _current.clone();
    }

    public function track():void {
        _current.draw(_vid, _mirror);
        _current.draw(_previous, null, null, BlendMode.DIFFERENCE);
        _current.applyFilter(_current, _current.rect, _point, _blur);
        _current.threshold(_current, _current.rect, _point, ">",  0xFF333333, 0xFFFFFFFF);
        _previous.draw(_vid, _mirror);

        _area = _current.getColorBoundsRect(0xFFFFFFFF,0xFFFFFFFF,true);
        _isMotion = ( _area.width > ( _vid.width / 100) * 10 || _area.height > (_vid.height / 100) * 10 );

        if (_isMotion) {
            px = _area.x + _area.width * .5;
            py = _area.y + _area.height * .5;
            graphics.clear();
            graphics.lineStyle(1, 0xFF0000, .5);
            graphics.drawRect(_area.x, _area.y, _area.width, _area.height);
            dispatchEvent(new Event("isMotion"));
        }
    }
}