RPG用Effect

by shohei909
rpg用エフェクト
keyboard
l:lightning
b:bomb
♥2 | Line 184 | Modified 2010-09-10 18:40:26 | MIT License
play

ActionScript3 source code

/**
 * Copyright shohei909 ( http://wonderfl.net/user/shohei909 )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/ltJQ
 */

//rpg用エフェクト
//keyboard
//l:lightning
//b:bomb


package {
    import flash.system.LoaderContext;
    import flash.events.KeyboardEvent;
    import flash.geom.Point;
    import flash.filters.BlurFilter;
    import flash.events.Event;
    import flash.net.URLRequest;
    import flash.display.Loader;
    import flash.display.Sprite;
    
    [SWF(backgroundColor = "#000000")]
    public class FlashTest extends Sprite {
        private var loader:Loader = new Loader();
        private var effect:EffectMap;
        public function FlashTest() {
            // write as3 code here..
            graphics.beginFill(0x000000,1);
            graphics.drawRect(0,0,465,465);
            
            addEventListener("addedToStage",init);
        }
        private function init(e:Event):void{
            removeEventListener("addedToStage",init);
            
            //LoaderContext(true) を設定しないとキャプチャーが取れない。
            loader.load(new URLRequest( "http://farm4.static.flickr.com/3244/2862492791_355a607ef7_m.jpg" ), new LoaderContext(true) );
            
            loader.contentLoaderInfo.addEventListener("complete",onComplete);
            addChild( loader );
            effect = new EffectMap( stage.stageWidth , stage.stageHeight );
            effect.targets[0] = new Point( stage.stageWidth>>1 , stage.stageHeight>>1 );
            
            addChild( effect )
        }
        
        private function onComplete(e:Event):void{
            loader.contentLoaderInfo.removeEventListener("complete",onComplete);
            stage.addEventListener("keyDown",onKeyDown); 
            
            loader.x = (stage.stageWidth - loader.width) >> 1;
            loader.y = (stage.stageHeight - loader.height) >> 1;
            
            effect.effect("bomb");
        }
        
        private function onKeyDown(e:KeyboardEvent):void{
            var c:String = String.fromCharCode(e.charCode);
            switch(c){
                case "l": effect.effect("lightning"); break;
                case "b": effect.effect("bomb"); break;
            }
        }
    }
}
import flash.geom.Matrix;
import flash.display.Shape;
import flash.filters.GlowFilter;
import flash.geom.Rectangle;
import flash.filters.BitmapFilter;
import flash.filters.BlurFilter;
import flash.geom.Point;
import flash.events.Event;
import flash.display.Bitmap;
import flash.display.BitmapData;

class EffectMap extends Bitmap{
    public var TYPES:Array = [ "lightning","ice" ]  
    
    private var backmapData:BitmapData;
    private var moving:Boolean = false;
    private var type:String = "none";
    private var pixels: Vector.<Vector.<Pixel>> = new Vector.<Vector.<Pixel>>();
    private var count:int = 0;
    private var time:int = 0;
    
    public var targets:Vector.<Point> = new Vector.<Point>();  
    public var bitmapFilters:Array = []
    
    private var red:int=10,green:int=10,blue:int=10,mergeAlpha:int=10;
    
    private var circle:Shape = new Shape();
    
    function EffectMap( w:int, h:int ){
        super(new BitmapData(w,h,true,0) );
        backmapData = new BitmapData(w,h,true,0);
        
        this.addEventListener("enterFrame", onFrame);
        
        with(circle.graphics){
            beginFill(0xFFFFFF,0.2)
            drawCircle(0,0,10)
        }
    }
    
    private function onFrame(e:Event):void{
        var b:BitmapData = bitmapData;
        var back:BitmapData = backmapData;
        b.lock();
        
        
        for each(var filter:BitmapFilter in bitmapFilters){
            b.applyFilter(b,b.rect,new Point,filter);
        }
        if(moving && count >= 0){
            switch(type){
                case "lightning": lightning(); break;
                case "bomb": bomb(); break;
            }
        }
        b.merge(back, b.rect, new Point, red, green, blue, mergeAlpha ); 
        
        b.unlock();
        if(time == count){
            moving = false;
        }
        count++;
    }
    
    
    public function effect(str:String):void{
        moving = true;
        type = str;
        count = 0;
        switch(type){
            case "lightning":
                blue = 1;
                red = 5;
                green = 5;
                mergeAlpha = 2;
                alpha = 2;
                bitmapFilters = [ new BlurFilter(8,8) ];
                time = 60;
                break;
                
            case "bomb":
                blue = 50;
                red = 7;
                green = 20;
                mergeAlpha = 20;
                alpha = 50;
                bitmapFilters = [ new BlurFilter(16,16) ];
                time = 60;
                break;    
        }
    }
    
    private function lightning():void{
        var b:BitmapData = bitmapData;
        if(count == time ){ b.fillRect(b.rect,0x88FFFFFF) }
        for each(var t:Point in targets){
            for(var j:int=0;j<5;j++){
                var point:Point = t.clone();
                var nextPoint:Point = t.clone();
                var dir:Number = 2*Math.PI*Math.random();
                var d:Number = 6;
                for(var i:int = 0;i< 20;i++){
                    dir += (1-2*Math.random())*1.5;
                    nextPoint.x += Math.sin(dir)*d; 
                    nextPoint.y += Math.cos(dir)*d; 
                    bitmapUtil.drawLine(b, point, nextPoint, 0xAAFFFFFF );
                    point = nextPoint.clone();
                }
            }
        }
    } 
    
    private function bomb():void{
        var b:BitmapData = bitmapData;
        if(count == time ){ b.fillRect(b.rect,0xFFFFFFFF) }
        if(count % 2 == 0 ){
            for each(var t:Point in targets){
                for(var j:int=0;j<3;j++){
                    var x:int = (1 - 2*Math.random())*50 + t.x; 
                    var y:int = (1 - 2*Math.random())*50 + t.y; 
                    var size:int = 5 *Math.random(); 
                    b.draw( circle, new Matrix(size,0,0,size,x,y)  );
                }
            }
        }
        for each(var filter:BitmapFilter in bitmapFilters){
            b.applyFilter(b,b.rect,new Point,filter);
        }
    }

}


class Pixel{
    private var x:int = 0, y:int = 0;
}
class bitmapUtil{
    static public function drawLine(data:BitmapData, start:Point, end:Point, color:uint=0xFFFFFFFF):void{
        var x:Number =  start.x < end.x ? start.x : end.x
        var y:Number = start.x < end.x ? start.y : end.y
        var endX:Number = start.x < end.x ? end.x : start.x
        var endY:Number= start.x < end.x ? end.y : start.y
        if( 0 == int(endX - x) ){
            for(var i:int=y; i <= endY; i++ ){
                data.setPixel32(x,i,color);
            }
        }else{ 
            var dy:Number = (endY - y) / (endX - x)
            do{
                var y2:int = y < y+dy ? y : y+dy
                var endY2:int = y > y+dy ? y : y+dy
                do{    
                    data.setPixel32(x,y2,color)
                    y2++;
                }while(y2 <= endY2)
                x++;
                y += dy
            }while( x <= endX )
        }
    }
}