Swarm

by hrtsgt
♥0 | Line 89 | Modified 2009-06-04 19:45:58 | MIT License
play

ActionScript3 source code

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

package{
	import flash.display.*;
	import flash.events.*;
	import flash.geom.*;
	[SWF(width=465, height=465, backgroundColor=0x0, frameRate=60)]
	
	public class Swarm extends Sprite{
		public var _canvas:BitmapData;
		public var ary:Array;
		private var ct:ColorTransform = new ColorTransform( 0.99, 0.99, 0.97, 0.9, 0, 0, 0, 0);
	
		public function Swarm(){
			_canvas = new BitmapData( stage.stageWidth, stage.stageHeight, false, 0x0);
			ary = [];
			for(var i:uint=0; i<3000; i++){
				var p:Particle = new Particle( stage.stageWidth, stage.stageHeight);
				ary.push( p );
			}
			addChild( new Bitmap(_canvas) );
			this.addEventListener( Event.ENTER_FRAME, update);//
		}
		
		private function update( ev:Event ):void{
			_canvas.lock();
			_canvas.colorTransform( _canvas.rect, ct);
			for(var i:uint=0; i<ary.length; i++){
				ary[i].jump();
				_canvas.setPixel( ary[i].x, ary[i].y, 0xFFFFFF);
			}
			_canvas.unlock();
		}
	
	}
}


import flash.display.Shape;
import flash.geom.Point;

class Particle extends Shape{
	private var t:Point;
	private var c:Point;
	private var c_:Point;
	private var p:Point;
	private var p_:Point;
	private var p__:Point;
	private var frc:Number = 0.85 + Math.random()/200;
	private static var sw:uint;
	private static var sh:uint;
	public function Particle( _sw:uint, _sh:uint){
		sw = _sw;
		sh = _sh;
		this.graphics.beginFill( 0xFFFFFF );
		this.graphics.drawCircle( 0, 0, 2);
	
		c = new Point( Math.random()*sw, Math.random()*sh);
		c_ = new Point(0, 0);
		t = new Point( c.x + (Math.random()*30 + 30)*(1 - Math.floor(Math.random()*2)*2), c.y + (Math.random()*30 + 30)*(1 - Math.floor(Math.random()*2)*2) );
		p = new Point( (t.x - c.x)*0.02 + c.x, c.y - (Math.random()*15+10) );
		p_ = new Point(0, 0);
		p__ = new Point(0, 0);
	}
	public function jump():void{
		p_.x = (t.x - c.x)*frc/100 + c.x;
		p_.y = (t.y - c.y)*frc/100 + c.y;
		c_.x = c.x - (c.x - p.x) - (c.x - p_.x);
		c_.y = c.y - (c.y - p.y) - (c.y - p_.y);
		p__.x = c_.x - (c.x - c_.x)*frc;
		p__.y = c_.y - (c.y - c_.y)*frc;
		if( c_.x < 0 ){
			c_.x = sw + c_.x;
			p__.x = sw + p__.x;
			t.x = sw + t.x;
		}else if( c_.x > sw ){
			c_.x = c_.x - sw;
			p__.x = p__.x - sw;
			t.x = t.x - sw;
		}
		if( c_.y < 0 ){
			c_.y = sh + c_.y;
			p__.y = sh + p__.y;
			t.y = sh + t.y;
		}else if( c_.y > sh ){
			c_.y = c_.y - sh;
			p__.y = p__.y - sh;
			t.y = t.y - sh;
		}
		x = c_.x;
		y = c_.y;

		p = p__.clone();
		c = c_.clone();
		if( Math.abs(t.x-x) < 10 && Math.abs(t.y-y) < 10 ){
			t = new Point( x + (Math.random()*30 + 30)*(1 - Math.floor(Math.random()*2)*2), y + (Math.random()*30 + 30)*(1 - Math.floor(Math.random()*2)*2) );
			p = new Point( (t.x - c.x)*0.02 + c.x, c.y - (Math.random()*15+10) );
		}
	}
}