flash on 2013-6-14
♥0 |
Line 100 |
Modified 2013-06-14 21:06:53 |
MIT License
archived:2017-03-30 22:51:50
ActionScript3 source code
/**
* Copyright mutantleg ( http://wonderfl.net/user/mutantleg )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/x9wi
*/
package {
import flash.text.TextField;
import flash.events.MouseEvent;
import flash.utils.Dictionary;
import flash.events.Event;
import flash.display.Sprite;
public class FlashTest extends Sprite {
public function FlashTest() {
vecAct = new Vector.<xActor>(0,false);
var a:xActor;
var i:int;
for ( i = 0; i < 16; i++)
{
a = new xActor();
a.cx = Math.random()*465;
a.cy = Math.random()*465;
a.id = i;
vecAct.push(a);
}//nexti
deb = new TextField();
deb.width = 320;
deb.height = 240;
deb.mouseEnabled = false;
addChild(deb);
stage.addEventListener(MouseEvent.MOUSE_DOWN, mdown);
stage.addEventListener(Event.ENTER_FRAME, onEnter);
}//ctor
public var deb:TextField;
public var wit:int = 0;
public function mdown(e:MouseEvent):void
{
var a:xActor;
a = vecAct[ wit ];
wit += 1; if (wit >= vecAct.length) { wit = 0; }
a.gx = stage.mouseX;
a.gy = stage.mouseY;
a.move = 1;
a.gotpath = 0;
a.needpath = 1;
//deb.text = " a: " + a.id + " gx " + a.gx + " gy " + a.gy + " qleft " + qleft;
//addReq(a);
}//mdown
public function onEnter(e:Event):void
{
graphics.clear();
graphics.lineStyle(2, 0);
//procQue();
procPath();
var i:int;
var num:int;
var a:xActor;
num = vecAct.length;
for (i = 0; i < num; i++)
{
a = vecAct[i];
if (a.move > 0 && a.needpath > 0)
{
getPath(a);
}
if (a.move > 0)
{
graphics.moveTo(a.cx, a.cy);
graphics.lineTo(a.gx, a.gy);
}
if (a.move > 0 && a.gotpath > 0)
{
graphics.drawCircle(a.cx,a.cy,4);
a.cx += (a.gx - a.cx) *0.1;
a.cy += (a.gy - a.cy) *0.1;
if (Math.abs(a.cx-a.gx)<2 && Math.abs(a.cy-a.gy) <2) { a.move = 0; }
}
graphics.drawCircle(a.cx, a.cy, 8);
}//nexti
}//onenter
public var vecAct:Vector.<xActor>;
//so the idea is
//path finding and stuff is slow and need to be distributed to multiple frames
//so actors need to stand in queue to get their path
//and instead of storing and maintaining a long array of which ones
//are waiting in line
//there is just a holder for a single actor
//and whenever one needs a path just keeps nagging the path maker to get it one
//and the one that nags at the right time starts to get its path processed
//(kind of like baby birds in a nest screaming away for food or something)
public var mwait:int = 0;
public var mcur:xActor = null;
public function getPath(a:xActor):void
{
if (mcur != null) { return; }
//todo -- if the path is requested by a currently processed one
//halt the process
mcur = a;
mwait = 12;
a.needpath = 0;
}//getpath
public function procPath():void
{
if (mcur == null) { return; }
//todo -- if actor changed its mind
//halt the process (only need to check at start)
//path finding is not done
//we just pretend we do something by waiting some frames
//(aka coding meets bureaucracy)
mwait -= 1;
if (mwait <= 0) { mcur.gotpath = 1; mcur = null; }
}//procpath
//old try that didn't quite worked well (too complex)
/*
public var dict:Dictionary = new Dictionary();
public var vecQue:Vector.<xActor> = new Vector.<xActor>(512, false);
public var qleft:int = 0;
public var qlast:int = 1;
public var qit:int = 0;
public var qfin:int = 1;
public var qwait:int = 0;
public function remReq(a:xActor):void
{
var k:int;
k = dict[a.id];
if(k <= 0) { return; }
dict[a.id] = 0;
vecQue[k] = null;
}//remreq
public function addReq(a:xActor):void
{
if (qleft >= 500) { return; }
var k:int;
k = dict[a.id];
if (k == qit) { qfin = 1; }
else { if (k > 0) { return; } }
dict[a.id] = qlast;
vecQue[qlast] = a;
qlast += 1;
if (qlast > 511) { qlast = 1;}
qleft += 1;
}//addreq
public function procQue():void
{
var a:xActor;
if (qfin > 0)
{
if (qleft <= 0) { return; }
else
{
qit += 1;
if (qit > 511) { qit = 1; }
qleft -= 1;
qfin = 0;
qwait = 10;
deb.text = "qleft " + qleft + " qit " + qit;
}//endif2
}//endif
a = vecQue[qit];
if (a == null) { qfin = 1; return;}
qwait -=1;
if (qwait > 0) { return;}
//deb.appendText("qwait " + qwait);
a.gotpath = 1;
qfin = 1;
}//procque
*/
}//classend
}
internal class xActor
{
public var cx:Number = 0;
public var cy:Number = 0;
public var gx:Number = 0;
public var gy:Number = 0;
//storing 3 ints for this is a waste but code is more readable
public var needpath:int = 0;
public var gotpath:int = 0;
public var move:int = 0;
public var id:int = 0;
}//ctor