ActionScript: Red Blood Cells behavior

[swf:/wp-content/uploads/2010/11/Blood1.swf 550 400]

Télécharger la source : Blood.fla

import flash.geom.ColorTransform;

var arrBlood:Array = [];

function createVers():void {
	var b = new Blood();
	b.speedX = 0;
	b.speedY = 0;
	b.coefCollision = 50;
	b.coefRotation = 30;
	var size:Number = Math.random();

	b.scaleX = size * .5 + .5;
	b.scaleY = size * .5 + .5;
	b.scale = b.scaleX;
	b.ray = b.width / 2.5;
	b.acc = true;
	b.acceleration = 1 + size * 1;
	b.coefDecceleration = 0.92 + size * 0.03;
	b.velocityMax = 4 + size * 4;
	b.velocity = 0;
	b.x = 400 * Math.random();
	b.y = 550 * Math.random();
	b.angle = 0;
	var tint:Number = Math.random() * .7 + .3;
	b.transform.colorTransform = new ColorTransform(tint,tint,tint);
	cont.addChild(b);
	b.addEventListener(Event.ENTER_FRAME, versEnterFrame);
	arrBlood.push(b);
}

function getResAngleRad(angle1:Number,angle2:Number):Number {
	var a:Number = angle1 - angle2;
	if (a > Math.PI) {
		a = a - Math.PI * 2;
	} else if (a<(-Math.PI)) { 		a = a + Math.PI * 2; 	} 	return a; } function versEnterFrame(pEvt:Event):void { 	var b:MovieClip = pEvt.currentTarget as MovieClip; 	var angle:Number; 	if (b.acc) {//trace("acc") b.velocity += b.acceleration; //trace(b.velocity) if (b.velocity > b.velocityMax) {
		b.acc = false;
	} else {
		b.velocity *=  b.coefDecceleration;
		if (b.velocity < 0.5) { 			b.acc = true; 			if (stage.mouseX > 550 || stage.mouseY > 400 || stage.mouseX < 0 || stage.mouseY < 0) {
				b.angle = Math.atan2(400/2-b.y,550/2-b.x);
			} else {
				b.angle = Math.atan2(stage.mouseY - b.y,stage.mouseX - b.x);
			}
		}
	}
	b.scaleX = b.scale + (b.scale * 0.4) * b.velocity/b.velocityMax;
	b.scaleY = b.scale - (b.scale * 0.4) * b.velocity/b.velocityMax;
	var angleSpeed:Number = Math.atan2(b.speedY,b.speedX);
	var angleRes:Number = getResAngleRad(angleSpeed,b.angle);
	//trace(angleRes)
	if (Math.abs(angleRes) < 0.1) {//trace("=") } else if (angleRes >0) {
		angleSpeed -=  Math.PI / b.coefRotation * b.velocity / b.velocityMax;
		//trace("+")
	} else if (angleRes <0) {
		angleSpeed +=  Math.PI / b.coefRotation * b.velocity / b.velocityMax;
	}//trace("-")

	b.speedX = Math.cos(angleSpeed) * b.velocity;
	b.speedY = Math.sin(angleSpeed) * b.velocity;

	b.rotation = angleSpeed * 180 / Math.PI;

	b.x +=  b.speedX;
	b.y +=  b.speedY;

	for (var i:String in arrBlood) {
		var mc:MovieClip = arrBlood[i];
		var dist:Number = getDistance(b,mc);
		if (dist < b.ray+mc.ray) {
			//trace("col")
			var a:Number = Math.atan2(b.y - mc.y,b.x - mc.x);
			b.x +=  Math.cos(a) * dist / b.coefCollision;
			b.y +=  Math.sin(a) * dist / b.coefCollision;
			mc.x -=  Math.cos(a) * dist / b.coefCollision;
			mc.y -=  Math.sin(a) * dist / b.coefCollision;
		}
	}
}

function getDistance(a:MovieClip,b:MovieClip):Number {
	return Math.sqrt(Math.pow(a.x - b.x,2) + Math.pow(a.y - b.y,2));
}
for (var i:int = 0; i<100; i++) {
	createVers();
}