/**
* Copyright mutantleg ( http://wonderfl.net/user/mutantleg )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/KHG4
*/
package {
import flash.events.Event;
import flash.display.Sprite;
public class FlashTest extends Sprite {
public function FlashTest() {
vecRect = new Vector.<xRect>(0,false);
var i:int; var a:xRect;
for (i=0;i<64;i+=1)
{
a = new xRect();
a.cx = Math.random()*430;
a.cy = Math.random()*430;
a.vx = Math.random()*4-2;
a.vy = Math.random()*4-2;
vecRect.push(a);
}//nexti
stage.addEventListener(Event.ENTER_FRAME, onEnter);
}//ctor
public var vecRect:Vector.<xRect>;
public function isWall(ax:Number,ay:Number):Boolean
{ if (ax <0||ay<0||ax>=465 || ay>=465){ return true;} return false; }
public function onEnter(e:Event):void
{
graphics.clear();
graphics.lineStyle(2, 0);
var i:int; var num:int; var a:xRect;
var k:int; var o:xRect;
var ta:Number; var ms:Number;
num = vecRect.length;
for (i=0;i<num;i+=1)
{
a = vecRect[i];
graphics.drawRect(a.cx-a.rad,a.cy-a.rad,a.rad*2,a.rad*2);
ta = Math.atan2(stage.mouseY-a.cy, stage.mouseX-a.cx);
ms =0.05;
a.vx += Math.cos(ta)*ms;
a.vy += Math.sin(ta)*ms;
a.vy+=0.2;
a.vx *= 0.99; a.vy *= 0.99;
if (a.vx>0&&isWall(a.cx+a.rad,a.cy)) {a.vx*=-0.5;}
else if (a.vx<0&&isWall(a.cx-a.rad,a.cy)) {a.vx*=-0.5;}
if (a.vy>0&&isWall(a.cx,a.cy+a.rad)) {a.vy*=-0.5;}
else if (a.vy<0&&isWall(a.cx,a.cy-a.rad)) {a.vy*=-0.5;}
for (k=0;k<num;k+=1)
{
if (k == i) { continue; }
o = vecRect[k];
if (a.vx>0&&o.isWall(a.cx+a.rad,a.cy)) {a.vx*=-0.5;}
else if (a.vx<0&&o.isWall(a.cx-a.rad,a.cy)) {a.vx*=-0.5;}
if (a.vy>0&&o.isWall(a.cx,a.cy+a.rad+a.vy)) {a.vy = a.vy<0.5? 0: a.vy*-0.5;}
else if (a.vy<0&&o.isWall(a.cx,a.cy-a.rad)) {a.vy*=-0.5;}
if (o.isWall(a.cx+a.rad+1,a.cy)) {a.cx-=1; }
else if (o.isWall(a.cx-a.rad-1,a.cy)) {a.cx+=1; }
if (o.isWall(a.cx,a.cy+a.rad+1)) {a.cy-=1;}
//else if (o.isWall(a.cx,a.cy-a.rad-1)) {a.cy+=1;}
}//nextk
a.cx +=a.vx; a.cy+=a.vy;
if (isWall(a.cx+a.rad+1,a.cy)) {a.cx-=1; }
else if (isWall(a.cx-a.rad-1,a.cy)) {a.cx+=1; }
if (isWall(a.cx,a.cy+a.rad+1)) {a.cy-=1;}
else if (isWall(a.cx,a.cy-a.rad-1)) {a.cy+=1;}
}//nexti
}//onenter
}//classend
}
internal class xRect
{
public var cx:Number = 0;
public var cy:Number = 0;
public var vx:Number = 0;
public var vy:Number = 0;
public var rad:Number = 16;
public function isWall(ax:Number, ay:Number):Boolean
{
if (ax < cx-rad) { return false;}
if (ay < cy-rad) { return false;}
if (ax > cx+rad) { return false;}
if (ay > cy+rad) { return false;}
return true;
}//iswall
}//xrect