SwordLight
...
@author lizhi http://game-develop.net/
/**
* Copyright lizhi ( http://wonderfl.net/user/lizhi )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/o3QmN
*/
package
{
import adobe.utils.ProductManager;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Matrix;
import flash.geom.Point;
import flash.utils.ByteArray;
/**
* ...
* @author lizhi http://game-develop.net/
*/
public class SwordLight extends Sprite
{
private var sword:Sprite;
private var v:Number = 0;
private var light:Vector.<P> = new Vector.<P>;
private var lightMap:BitmapData = new BitmapData(256, 256);
public function SwordLight():void
{
sword = new Sprite;
sword.graphics.lineStyle(0);
sword.graphics.beginFill(0,.2);
sword.graphics.curveTo(0, 130, 20, 200);
sword.graphics.lineTo(20, 0);
sword.graphics.lineTo(0, 0);
addChild(sword);
lightMap.perlinNoise(100, 10, 2, 100 * Math.random(), true, true);
var bytes:ByteArray = lightMap.getPixels(lightMap.rect);
for (var x:int = 0; x < lightMap.width;x++ ) {
var a:Number = x/lightMap.width;
for (var y:int = 0; y < lightMap.height;y++ ) {
var p:Number = (1-Math.sin(y/(lightMap.height-1)*Math.PI)*.8);
bytes[4 * (x + y * lightMap.width)] = a * p * 0xff;
bytes[4 * (x + y * lightMap.width)+1] = a * p * 0xff;
}
}
bytes.position = 0;
lightMap.setPixels(lightMap.rect, bytes);
addEventListener(Event.ENTER_FRAME, enterFrame);
//addChild(new Bitmap(lightMap));
stage.addEventListener(MouseEvent.MOUSE_MOVE, stage_mouseMove);
}
private function enterFrame(e:Event):void
{
sword.rotation += v;
v *= 0.9;
var p:Point = sword.transform.matrix.transformPoint(new Point(20, 200));
var pw:P = new P;
pw.p = p;
var pw2:P = new P;
pw2.p = new Point(sword.x, sword.y);
light.push(pw, pw2);
var vin:Vector.<Number> = new Vector.<Number>;
var uv:Vector.<Number> = new Vector.<Number>;
var index:Vector.<int> = new Vector.<int>;
for (var i:int = light.length - 1; i >= 0;i-=2 ) {
var p0:P = light[i];
var p1:P = light[i - 1];
vin.push(p0.p.x, p0.p.y);
vin.push(p1.p.x, p1.p.y);
var u:Number = i / (light.length - 1);
uv.push(u, 0);
uv.push(u, 1);
p0.pwoer--;
if (p0.pwoer<0) {
light.splice(i, 2);
}
if(i>3){
var p2:P = light[i-2];
var p3:P = light[i - 3];
var vi:int = vin.length / 2-2;
index.push(vi,vi+1,vi+2,vi+1,vi+2,vi+3);
}
}
graphics.clear();
graphics.beginBitmapFill(lightMap);
graphics.drawTriangles(vin, index,uv);
}
private function stage_mouseMove(e:MouseEvent):void
{
sword.x = mouseX ;
sword.y = mouseY ;
v = 20;
}
}
}
class P {
public var p:flash.geom.Point;
public var pwoer:int = 10;
}