particle-in-cell physics testing

by hemingway
♥0 | Line 58 | Modified 2014-11-20 01:27:50 | MIT License
play

ActionScript3 source code

/**
 * Copyright hemingway ( http://wonderfl.net/user/hemingway )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/io0R
 */

package {
    import flash.display.AVM1Movie;
    import flash.display.Sprite;
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.events.Event;
    import flash.events.MouseEvent;
    
    [SWF(frameRate=60, width=465, height=465)]
    public class WFL extends Sprite {
        public static const X:int = 0;
        public static const Y:int = 1;
        public static const VX:int = 2;
        public static const VY:int = 3;
        public static const TX:int = 4;
        public static const TY:int = 5;
        public static const C:int = 6;
        
        private var _canvas:Bitmap;
        private var _canvasData:BitmapData; 
        private var _particle:Vector.<int>;
        private var _particleMap:Vector.<Vector.<int>>;
        
        public function WFL() {
            this._canvasData = new BitmapData(465, 465, false, 0xDCDCDC);
            this._canvas = new Bitmap(this._canvasData);
            this._particle = new Vector.<int>(7, true);
            this._particleMap = new Vector.<Vector.<int>>;
            
            for (var $y:int = 0; $y < 465; $y++) {
                this._particleMap[$y] = new Vector.<int>;
                
                for (var $x:int = 0; $x < 465; $x++) {
                    ($x < 1 || $y < 1 || $x > 465 || $y > 465) ? this._particleMap[$y][$x] = 0 : this._particleMap[$y][$x] = 1;
                }
            }

            
            this._particle[X] = 1;
            this._particle[Y] = 1;
            this._particle[VX] = 3;
            this._particle[VY] = 2;
            this._particle[TX] = 12;
            this._particle[TY] = 12;
            this._particle[C] = 0;
            
            this.addEventListener(Event.ADDED_TO_STAGE, this.addedToStageHandler);
        }
        
        private function addedToStageHandler($event:Event):void {
            this.removeEventListener(Event.ADDED_TO_STAGE, this.addedToStageHandler);
            
            this.addChild(this._canvas);

            this.addEventListener(Event.ENTER_FRAME, this.enterFrameHandler);
        }
        
        private function enterFrameHandler($event:Event):void {
            if (this._particleMap[this._particle[Y]][this._particle[X]+this._particle[VX]] == 0) {
                this._particle[VX] = ~(this._particle[VX] - 1);
            }
            
            if (this._particleMap[this._particle[Y]+this._particle[VY]][this._particle[X]] == 0) {
                this._particle[VY] = ~(this._particle[VY] - 1);
            }

            this._particle[X] += this._particle[VX];
            this._particle[Y] += this._particle[VY];
            
            this._canvasData.setPixel(this._particle[X], this._particle[Y], 0xFF0000);
        }
    }
}