package{
import flash.display.Sprite;
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.system.LoaderContext;
import flash.geom.Matrix;
import flash.display.GradientType;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.DisplayObject;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.events.Event;
public class RayCast extends Sprite{
public var range:Number = 100;
public var steps:Number = 100;
public var rate:Number = range/steps;
public var angle:Number = Math.PI*2;
public var shots:Number = 180;
public var arate:Number = angle/shots;
public var url:URLRequest = new URLRequest("http://i47.tinypic.com/15ogcnk.jpg");
public var loader:Loader = new Loader();
public var Points:Vector.<Vector.<Number>> = new Vector.<Vector.<Number>>();
public var map:BitmapData;
public var light:Sprite = new Sprite();
public var shadow:Sprite = new Sprite();
public function RayCast(){
map = new BitmapData(465,465,true,0xffffffff);
loader.load(url);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE,loaded);
}
public function main(e:Event):void{
Points= new Vector.<Vector.<Number>>();
for(var i:Number=0;i<=shots;i+=1){
var a:Number = i*arate;
var p:Vector.<Number>;
var A:Vector.<Number> = new Vector.<Number>();
var X:Number = mouseX;
var Y:Number = mouseY;
var DX:Number = Math.cos(a)*rate;
var DY:Number = Math.sin(a)*rate;
for(var j:Number=rate;j<=range;j+=rate){
X += DX;
Y += DY;
p = Channels(X,Y,0);
if(p[0] == 0){
j = range;
X -= DX;
Y -= DY;
}
}
A.push(X);
A.push(Y);
Points.push(A);
}
var matr:Matrix = new Matrix();
matr.createGradientBox(2*range,2*range,0,mouseX-range,mouseY-range);
with(light.graphics){
clear();
beginGradientFill(GradientType.RADIAL,[0x000000,0x000000],[1,0],[100,255],matr);
moveTo(mouseX,mouseY);
}
for(i=0;i<Points.length;i++){
light.graphics.lineTo(Points[i][0],Points[i][1]);
}
light.graphics.endFill();
}
public function loaded(e:Event):void{
addChild(loader);
addChild(shadow);
shadow.blendMode = "layer";
shadow.cacheAsBitmap = true;
shadow.addChild(light);
light.blendMode = "erase";
light.cacheAsBitmap = true;
with(shadow.graphics){
clear();
beginFill(0x000000,0.5);
drawRect(0,0,stage.stageWidth,stage.stageHeight);
endFill();
}
stage.addEventListener(Event.ENTER_FRAME,main);
var byteLoader:Loader = new Loader;
byteLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, function ():void {
map.draw(byteLoader.content);
});
byteLoader.loadBytes(loader.contentLoaderInfo.bytes);
}
public function Channels(X:int,Y:int,t:Number=-1):Vector.<Number>{
var A:Vector.<Number> = new Vector.<Number>();
var source:uint = map.getPixel32(X,Y);
for(var i:int=0;i<3;i++){
if(i == t || t == -1){
var j:int = 8*(2-i);
var u:uint = (source >> j) & 0xff;
if(u == 255){
A.push(2);
}else if(u == 0){
A.push(0);
}else{
A.push(1);
}
}
}
return A;
}
}
}