/**
* Copyright mutantleg ( http://wonderfl.net/user/mutantleg )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/sTcp
*/
package {
import flash.utils.Dictionary;
import flash.display.Graphics;
import flash.events.Event;
import flash.display.Sprite;
public class FlashTest extends Sprite {
public function FlashTest() {
var num:int; var i:int;
mw = 32;
mh = 32;
num = mw * mh;
vecGrid = new Vector.<int>(num, false);
for (i = 0; i < num; i++) { vecGrid[i] = 0; }
dictBall = new Dictionary();
var a:xBall;
vecBall = new Vector.<xBall>(0,false);
for (i = 0; i < 32; i++)
{
a = new xBall();
a.cx = Math.random()*400;
a.cy = Math.random()*400;
a.vx = Math.random()*4-2;
a.vy = Math.random()*4-2;
a.id = i+1;
vecBall.push(a );
dictBall[a.id] = a;
}//nexti
stage.addEventListener(Event.ENTER_FRAME, onEnter);
}//ctor
public var dictBall:Dictionary;
public var mw:int = 0;
public var mh:int = 0;
public var vecGrid:Vector.<int>;
public var cw:Number = 16;
public var ch:Number = 16;
public var icw:Number = 1/16;
public var ich:Number = 1/16;
public var cw2:Number = 8;
public var ch2:Number = 8;
public var mx:Number = 0;
public var my:Number = 0;
public var vecBall:Vector.<xBall>;
public function onEnter(e:Event):void
{
//setWall(int(mx/cw),int(my/ch), 0);
setBlock(mx,my,0);
mx = stage.mouseX;
my = stage.mouseY;
setBlock(mx,my,1);
//setWall(int(mx/cw),int(my/ch), 1);
graphics.clear();
graphics.lineStyle(2,0);
drawMap(graphics);
var i:int; var num:int; var a:xBall;
num = vecBall.length;
for (i =0; i < num; i++)
{
a = vecBall[i];
setBlock(a.cx,a.cy,0);
if (a.cx > 460 && a.vx>0) {a.vx*=-1;}
if (a.cx < 0 && a.vx<0) {a.vx*=-1;}
if (a.cy > 460 && a.vy>0) {a.vy*=-1;}
if (a.cy < 0 && a.vy<0) {a.vy*=-1;}
if (a.vx > 0 && isWall(a.cx+16,a.cy)) {a.vx *=-1;}
if (a.vx < 0 && isWall(a.cx-16,a.cy)) {a.vx *=-1;}
if (a.vy > 0 && isWall(a.cx,a.cy+16)) {a.vy *=-1;}
if (a.vy < 0 && isWall(a.cx,a.cy-16)) {a.vy *=-1;}
a.cx+=a.vx;
a.cy+=a.vy;
setBlock(a.cx,a.cy, a.id);
graphics.beginFill(0,1);
graphics.drawCircle(a.cx,a.cy, 16);
graphics.endFill();
}//nexti
graphics.beginFill(0xFF0000,0.5);
graphics.drawCircle(mx,my, 16);
graphics.endFill();
}//onenter
public function setBlock(ax:Number, ay:Number, t:int):void
{
setWall(((ax-cw2)*icw)|0, ((ay-ch2)*ich)|0 , t);
setWall(((ax-cw2)*icw)|0, ((ay+ch2)*ich)|0 , t);
setWall(((ax+cw2)*icw)|0, ((ay-ch2)*ich)|0 , t);
setWall(((ax+cw2)*icw)|0, ((ay+ch2)*ich)|0 , t);
}//setblock
public function isWall(ax:Number,ay:Number):Boolean
{
return getTile((ax*icw)|0,(ay*ich)|0) > 0;
}
public function getTile(ax:int, ay:int):int
{
if (ax < 0) { return 1; } if (ax >= mw) { return 1; }
if (ay < 0) {return 1; } if (ay >= mh) {return 1;}
return vecGrid[ay*mw+ax];
}//getwall
public function setWall(ax:int, ay:int, t:int):void
{
if (ax < 0) { return; } if (ax >= mw) { return; }
if (ay < 0) {return; } if (ay >= mh) {return;}
vecGrid[ay*mw+ax]=t;
}//setwall
public function drawMap(g:Graphics):void
{
var i:int; var k:int; var yt:int;
var t:int;
for (i = 0; i < mh; i++)
{ yt = i*mw;
for (k = 0; k < mw; k++)
{
t = vecGrid[yt+k];
if (t <= 0) { continue; }
g.beginFill(0xFF, 0.5);
g.drawRect(k*cw,i*ch,cw,ch);
g.endFill();
}//nextk
}//nexti
}//drawmap
}//classend
}
internal class xBall
{
public var cx:Number = 0;
public var cy:Number = 0;
public var vx:Number = 0;
public var vy:Number = 0;
public var id:int = 0;
}//xball