flash on 2011-12-22
...
@author vtodo
/**
* Copyright thisismee ( http://wonderfl.net/user/thisismee )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/t1nc
*/
package
{
import flash.display.*;
import flash.events.*;
import flash.filters.*;
import flash.geom.*;
import net.hires.debug.*;
/**
* ...
* @author vtodo
*/
[SWF(frameRate = "60", backgroundColor="0")]
public class Test extends Sprite
{
private const NUM_RAYS :int = 250;
private const HALF_RAYS :int = NUM_RAYS/2;
private const LENGTH_RAY :int = 600;
private const NUM_OBSTACLES :int = 100;
private const OBSTACLE_AREA :int = 1000;
private var _bmp_light :Bitmap;
private var _bmp_light_data :BitmapData;
private var _obstacles :BitmapData;
private var _hash_sin :Vector.<Number>;
private var _hash_cos :Vector.<Number>;
private var _distance_color :Vector.<uint>
public function Test():void
{
init();
stage.addEventListener(Event.ENTER_FRAME, mainLoop);
}
private function init():void
{
createBackground();
createHashMaps();
createDistanceMaps();
_bmp_light_data = new BitmapData(LENGTH_RAY, LENGTH_RAY, true, 0);
_bmp_light = new Bitmap(_bmp_light_data);
addChild(_bmp_light);
_bmp_light.filters = [new BlurFilter(20, 20, 1)];
drawObstacles();
drawLight();
addChild(new Stats());
}
private function createBackground():void
{
var t:Shape = new Shape();
t.graphics.beginFill(0);
t.graphics.drawRect(0,0,1000,1000);
addChild(t);
}
private function drawLight():void
{
_bmp_light_data.lock();
_bmp_light_data.fillRect(new Rectangle(0, 0, LENGTH_RAY, LENGTH_RAY), 0);
var i :int = 0;
var x_ :Number;
var y_ :Number;
for (i; i < NUM_RAYS; i ++)
{
y_ = _hash_cos[i];
x_ = _hash_sin[i];
for (var j:int = 0 ; j < LENGTH_RAY/2 ; j += 2)
{
if (_obstacles.getPixel(mouseX + x_ * j, mouseY + y_ * j ) != 0)
{
break;
}
else
{
//trace(int(j / (LENGTH_RAY / 2) * 16));
_bmp_light_data.setPixel32(LENGTH_RAY / 2 + x_ * j , LENGTH_RAY / 2 +y_ * j, _distance_color[int((j / (LENGTH_RAY / 2)) * 16)]); //
}
}
}
_bmp_light.x = mouseX - LENGTH_RAY/2;
_bmp_light.y = mouseY - LENGTH_RAY/2;
_bmp_light_data.unlock();
}
private function drawObstacles():void
{
_obstacles = new BitmapData(OBSTACLE_AREA, OBSTACLE_AREA, true, 0);
var temp_shape :Shape;
for (var i:int = 0 ; i < NUM_OBSTACLES ; ++i)
{
temp_shape = new Shape();
temp_shape.graphics.beginFill(0xff00f0);
temp_shape.graphics.drawRect(0, 0, 50, 50);
temp_shape.graphics.endFill();
_obstacles.draw(temp_shape, getRandomMatrix);
}
addChild(new Bitmap(_obstacles));
_obstacles.lock();
}
private function createDistanceMaps():void
{
_distance_color = new Vector.<uint>(16, 0);
for (var i:int = 0 ; i < 16 ; ++i)
{
_distance_color[i] = (0xffffffff - i * 0x11010101);
}
}
private function get getRandomMatrix():Matrix
{
return new Matrix(Math.random()*2, 0, 0, 1 , Math.random() * OBSTACLE_AREA, Math.random() * OBSTACLE_AREA);
}
private function createHashMaps():void
{
_hash_sin = new Vector.<Number>(NUM_RAYS,true);
_hash_cos = new Vector.<Number>(NUM_RAYS,true);
for (var i:int; i < NUM_RAYS; i ++)
{
//[0;2]
var angle:Number = Number((i / HALF_RAYS) * Math.PI);
var cos_:Number = Math.cos(angle);
var sin_:Number = Math.sin(angle);
_hash_sin[i]=sin_;
_hash_cos[i]= cos_;
}
}
private function mainLoop(e:Event=null):void
{
drawLight();
}
}
}