flash on 2015-9-7
/**
* Copyright mutantleg ( http://wonderfl.net/user/mutantleg )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/cbHy
*/
package {
import flash.events.Event;
import flash.display.Sprite;
public class FlashTest extends Sprite {
public function FlashTest() {
vecAct = new Vector.<xActor>(0, false);
mw = 16; mh = 16;
gridAct = new Vector.<xActor>(mw*mh, false);
var i:int; var num:int; var a:xActor;
for (i=0;i<64;i+=1)
{
a = new xActor();
a.cx = Math.random()*235+64;
a.cy = Math.random()*235+64;
vecAct.push(a);
var ta:Number;
ta = Math.random() * 6.28;
a.vx = Math.cos(ta) * 1;
a.vy = Math.sin(ta) * 1;
//setAct(a.cx/cw, a.cy/ch, a);
addAct(a.cx/cw, a.cy/ch, a);
}//nexti
stage.addEventListener(Event.ENTER_FRAME, onEnter);
}//ctor
public function onEnter(e:Event):void
{
graphics.clear();
graphics.lineStyle(2, 0);
var num:int;
var i:int; var a:xActor;
var k:int; var yt:int;
num = vecAct.length;
for(i=0;i<num;i+=1)
{
a = vecAct[i];
remAct(a);
if (a.vx > 0 && a.cx > 465) { a.vx = -a.vx; }
else if (a.vx < 0 && a.cx < 0) { a.vx = -a.vx; }
if (a.vy > 0 && a.cy > 465) { a.vy = -a.vy; }
else if (a.vy < 0 && a.cy < 0) { a.vy = -a.vy; }
a.cx += a.vx;
a.cy += a.vy;
addAct(a.cx/cw, a.cy/cw, a);
graphics.drawCircle(a.cx, a.cy, 8);
}//nexti
for (i=0;i<mh;i++)
{
yt = i * mw;
for (k=0;k<mw;k++)
{
a = gridAct[k+yt];
if (a == null) { continue; }
graphics.beginFill(0, 0.2);
graphics.drawRect(k*cw,i*ch,cw,ch);
graphics.endFill();
graphics.moveTo(a.cx,a.cy);
var w:int;
for (w = 0;w<256;w+=1)
{
a = a.next;
if (a == null) { break; }
graphics.lineTo(a.cx,a.cy);
}//nextw
}//nextk
}//nexti
}//onenter
public var vecAct:Vector.<xActor>;
public var mw:int = 0; public var mh:int = 0;
public var cw:Number = 64; public var ch:Number = 64;
public var gridAct:Vector.<xActor>;
public function getAct(ax:int, ay:int):xActor
{ if (ax<0||ax>=mw||ay<0||ay>=mh){ return null; } return gridAct[ay*mw+ax]; }
public function setAct(ax:int, ay:int, a:xActor):void
{ if (ax < 0 || ax >= mw || ay < 0 || ay >= mh) { return; } gridAct[ay * mw + ax] = a ; }
public function remAct(a:xActor):void
{
var m:xActor;
m = getAct(a.tx, a.ty);
if (m == a)
{ setAct(a.tx, a.ty, a.next);
a.next = null; a.prev = null;
a.tx =-1; a.ty = -1;
return;
}//endif
if (a.prev != null) { a.prev.next = a.next; }
if (a.next != null) { a.next.prev = a.prev; }
a.next = null; a.prev = null;
a.tx =-1; a.ty = -1;
}//remact
public function addAct(ax:int, ay:int, a:xActor):void
{
if (ax < 0 || ax >= mw || ay < 0 || ay >= mh) { return; }
if (a.tx == ax && a.ty == ay) { return; }
var fa:xActor; var t:int;
t = (ay * mw) + ax;
fa = gridAct[t];
if (a.prev != null) { a.prev.next = a.next; }
if (a.next != null) { a.next.prev = a.prev; }
a.next = null; a.prev = null;
a.tx = ax; a.ty = ay;
if (fa == null) { gridAct[t] = a; return; }
fa.prev = a; a.next = fa; gridAct[t] = a;
}//addact
}//classend
}
internal class xActor
{
public var next:xActor = null; public var prev:xActor = null;
public var cx:Number = 0; public var cy:Number = 0;
public var tx:int = -1; public var ty:int = -1;
public var vx:Number = 0; public var vy:Number = 0;
}//xactor