In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

固定弾幕回避学習結果再生機

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

// forked from uwi's GAで固定弾幕回避学習
package {
    import com.flashdynamix.utils.SWFProfiler;
    import flash.text.TextField;
    import flash.display.*;
    import flash.filters.*;
    import flash.geom.*;
    import flash.events.*;
    import flash.ui.*;
    import com.bit101.components.*;
    
    [SWF(backgroundColor="#000000", frameRate="30")]
    public class GAReplayer extends Sprite {
        private var _bullets : Array;
        private var _ct : int;
        private var _myx : Point;
        private var _nhit : int;
        private const R_BULLET : Number = 10.0;
        private const R_ME : Number = 5.0;
        private const INFD : Number = (R_BULLET + R_ME) * (R_BULLET + R_ME);
        
        private var _tf : TextField;
        private var _tfinput : TextField;
        private var _tfptn : TextField;
        private var _submit : PushButton;
        private var _stop : PushButton;
        
        private var _space : BitmapData;
        private var _shapeself : Shape;
        
        private var _bmdbullet : BitmapData;
        
        private const W : Number = 400;
        private const H : Number = 465;
        
        private var _shotPattern : Array;
        private var _avptn : String = "301821388880828508726361277570037466645703353656003360004600440005866644444464760284771311111133075666654444455013528828170386770137356640030045664362258806486888777888388773350018840116633800000007882200005132200081000010000008800011000012277205111488835555445550040005028805772211000000300000000000";
                
        private const P0 : Point = new Point(0, 0);
        private const WEAKBLUR : BitmapFilter = new BlurFilter(2.0, 2.0);
        
        public function GAReplayer() {
            Wonderfl.capture_delay(5);
            SWFProfiler.init(this);
            
            // 宇宙
            _space = new BitmapData(W, H, false, 0x000000);
            var bmpspace : Bitmap = new Bitmap(_space);
            addChild(bmpspace);
                                        
            _shapeself = new Shape();
            
            // 弾描画
            var bevel : BitmapFilter = new GradientBevelFilter(4.0, 45, [0xffffff, 0x0000ff], [1.0, 1.0], [70, 255], 4.0, 4.0, 1, 2, "inner");
            var sh : Shape = new Shape();
            var g : Graphics = sh.graphics;
            g.lineStyle(1.0, 0x999999);
            g.beginFill(0xeeeeee);
            g.drawCircle(R_BULLET, R_BULLET, R_BULLET);
            g.endFill();
            _bmdbullet = new BitmapData(R_BULLET * 2, R_BULLET * 2, true, 0x00000000);
            _bmdbullet.draw(sh);
            _bmdbullet.applyFilter(_bmdbullet, _bmdbullet.rect, P0, bevel);
            _bmdbullet.applyFilter(_bmdbullet, _bmdbullet.rect, P0, WEAKBLUR);
            
            // デバッグ用
            _tf = new TextField();
            setParams(_tf, {
                autoSize : "left",
                textColor : 0xffffff,
                borderColor : 0xffffff,
                border : true
            });
            addChild(_tf);
            
            var tfinputhead : TextField = new TextField();
            setParams(tfinputhead, {
                text : "x y v interval",
                textColor : 0xffffff,
                borderColor : 0xffffff,
                border : true,
                x : 370,
                y : 0,
                width : 90,
                height : 20
            });
            addChild(tfinputhead);
            
            _tfinput = new TextField();
            setParams(_tfinput, {
                text : "0 0 10 7\n150 0 10 7\n400 0 10 7",
                textColor : 0xffffff,
                borderColor : 0xffffff,
                border : true,
                x : 370,
                y : 20,
                width : 90,
                height : 150
            });
            addChild(_tfinput);
            
            _tfptn = new TextField();
            setParams(_tfptn, {
                text : "",
                textColor : 0xffffff,
                borderColor : 0xffffff,
                border : true,
                x : 370,
                y : 200,
                width : 90,
                height : 200
            });
            addChild(_tfptn);
            
            _submit = new PushButton(this, 370, 410, "Replay", onSubmit);
            _submit.width = 90;
            
            _stop = new PushButton(this, 370, 430, "Stop/Resume", onStop);
            _stop.width = 90;
            
            _state = 0;
        }
        
        private static function setParams(t : Object, v : Object) : Object
        {
            for(var k : String in v){
                t[k] = v[k];
            }
            return t;
        }
        
        private var _state : int;
        
        private function onSubmit(e : MouseEvent) : void
        {
            init();
            addEventListener(Event.ENTER_FRAME, onEnterFrame);
        }
        
        private function onStop(e : MouseEvent) : void
        {
            if(_state == 0)return;
            if(hasEventListener(Event.ENTER_FRAME)){
                removeEventListener(Event.ENTER_FRAME, onEnterFrame);
            }else{
                addEventListener(Event.ENTER_FRAME, onEnterFrame);
            }
        }
        
        private function init() : void
        {
            _state = 1;
            
            _myx = new Point(W / 2, H / 2);
            _bullets = [];
            _ct = 0;
            _nhit = 0;
            
            if(_tfptn.text.length > 0){
                _avptn = _tfptn.text.replace(/[\r\n ]/g, "");
            }
            
            _shotPattern = [];
            for each(var line : String in _tfinput.text.split(/[\r\n]/)){
                var seg : Array = line.split(' ');
                if(seg.length == 4){
                    if(int(seg[3]) > 0 && Number(seg[2]) > 0){
                        _shotPattern.push({
                            x : Number(seg[0]),
                            y : Number(seg[1]),
                            v : Number(seg[2]),
                            interval : int(seg[3])
                        });
                    }
                }
            }
        }
        
        private function onEnterFrame(e : Event) : void
        {
            draw();
            judge();
            
            for each(var ptn : Object in _shotPattern){
                if(_ct % ptn.interval == 0){
                    addBullet(ptn.x, ptn.y, ptn.v);
                }
            }
            
            moveMe();
            moveBullets();
            _ct++;
            
            _tf.text = 
                "time : " + _ct + 
                "\nhit : " + _nhit + 
                "\nbullets : " + _bullets.length;
        }
        
        private const ST : Array = [
            [0, 0],
            [6, 0], [4, -4], [0, -6], [-4, -4],
            [-6, 0], [-4, 4], [0, 6], [4, 4]
            ];
            
        private function moveMe() : void
        {
            var step : int = _ct / 2;
            if(step >= _avptn.length){
                _state = 1;
                removeEventListener(Event.ENTER_FRAME, onEnterFrame);
                return;
            }
            _myx.x += ST[int(_avptn.charAt(step))][0];
            _myx.y += ST[int(_avptn.charAt(step))][1];
        }
        
        private function moveBullets() : void
        {
            // 弾
            for each(var b : Bullet in _bullets){
                b.xx += b.vx;
                b.xy += b.vy;
            }
        }
        
        // 弾削除
        private function removeBullet(i : int) : void
        {
            if(i < _bullets.length - 1){
                _bullets[i] = _bullets.pop();
            }else{
                _bullets.pop();
            }
        }
        
        // 当たり判定
        private function judge() : void
        {
            for(var i : int = _bullets.length - 1;i >= 0;i--){
                var b : Bullet = _bullets[i];
                if(
                    (b.xx - _myx.x) * (b.xx - _myx.x) + 
                    (b.xy - _myx.y) * (b.xy - _myx.y)
                    < INFD){
                        _nhit++;
                        removeBullet(i);
                        continue;
                }
                if(b.xx < 0 || b.xx > W || b.xy < 0 || b.xy > H){
                    removeBullet(i);
                }
            }
            if(_myx.x < 0 || _myx.x > W || _myx.y < 0 || _myx.y > H){
                _nhit+=10;
                _myx.x = W / 2;
                _myx.y = H / 2;
            }
        }
        
        // 描画
        private function draw() : void
        {
            _space.lock();
            _space.fillRect(_space.rect, 0x000000);
            
            var g : Graphics = _shapeself.graphics;
            g.clear();
            
            // self
            g.lineStyle(1.0, 0x333399);
            g.beginFill(0x3333ff);
            g.drawCircle(_myx.x, _myx.y, 5);
            g.endFill();
            _space.draw(_shapeself, null, null, BlendMode.ADD);
            
            // bullets
            for each(var b : Bullet in _bullets){
                _space.copyPixels(_bmdbullet, _bmdbullet.rect,
                    new Point(b.xx - R_BULLET, b.xy - R_BULLET));
            }
            
            _space.unlock();
        }
        
        // 弾追加
        private function addBullet(x : Number, y : Number, v : Number) : void
        {
            var r : Number = Math.sqrt((_myx.x - x) * (_myx.x - x) + (_myx.y - y) * (_myx.y - y));

            var b : Bullet = new Bullet();
            b.xx = x;
            b.xy = y;
            b.vx = v * (_myx.x - x) / r;
            b.vy = v * (_myx.y - y) / r;
            b.r = R_BULLET;
            _bullets.push(b);
        }        
    }
}

class Bullet
{
    public var xx : Number;
    public var xy : Number;
    public var vx : Number;
    public var vy : Number;
    public var r : Number;
}