スイッチーズ - swithches

by shohei909
ライトをすべてoffにするパズルゲーム
♥13 | Line 263 | Modified 2010-08-16 16:59:01 | MIT License | (replaced)
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/xlbr
 */

//ライトをすべてoffにするパズルゲーム

package {
    import flash.display.Sprite;
    import com.bit101.components.*;
    
    public class FlashTest extends Sprite {
        public function FlashTest() {
             var board:GameBoard = new GameBoard(5,5)
             addChild( board );
        }
    }
}
import net.wonderfl.score.basic.*;
import flash.geom.Rectangle;
import flash.display.BitmapData;
import flash.events.Event;
import flash.display.Bitmap;
import flash.display.Sprite;
import flash.display.Stage;
import flash.text.TextField;
import flash.text.TextFormat;
import flupie.textanim.*;
import caurina.transitions.Tweener;
import net.wonderfl.utils.FontLoader;
import frocessing.color.ColorHSV;
import flash.utils.getTimer;



class GameBoard extends Sprite{
    static private var RELATE:Array = [ [0,0],[0,1],[0,-1],[1,0],[-1,0] ];
    
    static private var OFF_COLOR:uint = 0x222222; 
    static private var ON_COLOR:uint = 0xDDDD22; 
    static private var OVER_COLOR:uint = 0xFFFF22;
    
    private var buttons:Vector.<Vector.<GameButton>> = new Vector.<Vector.<GameButton>>();
    private var text:ClearText = new ClearText() ;
    private var timeText:BitText;
    private var start:Number;
    private var bitmapData:BitmapData;
    private var _width:int = 92;
    private var _height:int = 92;
    
    
    private var _interval:int = 5;
    private var _size:int = 30;
    private var net:Net;
    private var clear:Boolean;
    private var close:Boolean = true;
    
    public function GameBoard( width:int, height:int, size:int = 81, interval:int = 10 ):void{
        for( var x:int=0;x<width;x++ ){
            buttons[x] = new Vector.<GameButton>();
            for( var y:int=0;y<height;y++ ){
                buttons[x][y] = new GameButton( new Rectangle( x*(size+interval)+interval, y*(size+interval)+interval, size, size ) );
            }
        }
        bitmapData = new BitmapData( width*(size+interval)+interval,  height*(size+interval)+interval, false, 0x000000 );

        _size = size;
        _interval = interval;
        _width = width;
        _height = height;
        this.addEventListener("addedToStage",init)
    }
    
    private function init(e:Event):void{
        this.removeEventListener("addedToStage",init)
        this.addEventListener("enterFrame",onFrame)
        stage.addEventListener("mouseDown",onDown)
        addChild( new Bitmap(bitmapData) );
        net = new Net(465,465);
        
        addChild( text );
        timeText = new BitText(stage);
        text.scaleX = text.scaleY = 4;
        text.blendMode = "invert";
        reset();
   }
   
   
    public function remove():void{
        this.removeEventListener("enterFrame",onFrame);
        stage.addEventListener("enterFrame",onDown);
        this.parent.removeChild(this);
    }
    
    private function onFrame(e:Event=null):void{
        if(clear == false){timeText.setText( ((getTimer()-start)/10).toFixed(0) )}
        draw();
    }
    
    
    
    private function onDown(e:Event=null):void{
        for( var x:int=0;x<_width;x++ ){
            for( var y:int=0;y<_height;y++ ){
                var btn:GameButton = buttons[x][y];
                if( btn.value == true){
                    if( btn.rect.contains(mouseX,mouseY) ){ 
                        for each( var arr:Array in RELATE ){
                            if( buttons.hasOwnProperty( x+arr[0] ) && buttons[x+arr[0]].hasOwnProperty(y+arr[1]) ){
                                btn = buttons[x+arr[0]][y+arr[1]];
                                btn.value = (btn.value==false)
                            }
                        }
                    }
                }
            }
        }
        var clear:Boolean = true;
        for( x=0;x<_width;x++ ){
            for( y=0;y<_height;y++ ){
                btn = buttons[x][y];
                if( btn.value == true){
                    clear = false;
                }
            }
        }
        if( this.clear ){ 
            if(stage.focus==null && close == true){reset();}
        }else if(clear){ 
            text.start();
            this.clear = true;
            close = false
            new BasicScoreForm( this, 100, 100, 100000 - (getTimer()-start)/10 , "SAVE GAME SCORE", onClose);
        }
    }
    
    private function onClose(e:Boolean):void{
        removeChildAt( numChildren-1 );
        new BasicScoreRecordViewer( this, 100, 100, "SCORE RANKING", 20, true, onClose2 );
    }
    private function onClose2():void{
        removeChildAt( numChildren-1 );
        close = true;
    }
    
    
    
    private function reset():void{
        start = getTimer();
        RELATE = [[0,0]];
        var arr:Array = [ 0, 1 ];
        RELATE.push(arr);
        for(var i:int = 0; i<3; i++){
            var arr2:Array = [ -arr[1]  , arr[0] ]
            arr = arr2;
            RELATE.push(arr);
        }
        for( var j:int=0;j<_width*_height;j++ ){
            var x:int =  Math.floor(Math.random()*_width);
            var y:int =  Math.floor(Math.random()*_height);
            for each( arr in RELATE ){
                if( buttons.hasOwnProperty( x+arr[0] ) && buttons[x+arr[0]].hasOwnProperty(y+arr[1]) ){
                    var btn:GameButton = buttons[x+arr[0]][y+arr[1]];
                    btn.value = (btn.value==false)
                }
            }
        }
        this.clear = false;
        text.reset()
    }
    private function draw():void{
        var b:BitmapData = bitmapData
        b.lock();
        b.fillRect( b.rect, 0x000000 );
        b.draw(net);
        for( var x:int=0;x<_width;x++ ){
            for( var y:int=0;y<_height;y++ ){
                var btn:GameButton = buttons[x][y];
                if( btn.value ){ 
                    b.fillRect( btn.rect, btn.color.value );
                    if( btn.rect.contains(mouseX,mouseY) ){
                        for each( var arr:Array in RELATE ){
                            if( buttons.hasOwnProperty( x+arr[0] ) && buttons[x+arr[0]].hasOwnProperty(y+arr[1]) ){
                                btn = buttons[x+arr[0]][y+arr[1]];
                                btn.over();
                            }
                        }
                    }
                    else{
                        btn.change();
                    }
                }
                else{ 
                     b.fillRect( btn.rect, OFF_COLOR ); 
                };
                
            }
        }
        b.draw(net);
        b.unlock();
    }
}

class Net extends Bitmap{
    public function Net(w:int,h:int){
        var bit:BitmapData = new BitmapData(w,h,true,0x0);
        for( var x:int=0;x<w;x++ ){
            for( var y:int=0;y<h;y++ ){
                if( (x-y)%3==0 ){ bit.setPixel32( x, y, 0x66AAAAAA ) }
            }
        }
        super(bit); 
    }
}
class GameButton{
    public var rect:Rectangle;
    public var value:Boolean = false;
    public var color:ColorHSV = new ColorHSV();
    public function GameButton(r:Rectangle):void{
        rect = r;
        color.h = Math.random() * 360;
        color.s = 0.3;
        color.v = 1.0;
    }
    public function change():void{
        color.s = 0.35;
        color.v = 0.9;
        color.h += Math.random() * 5;
    }
    public function over():void{
        color.s = 0.9;
        color.v = 1.0;
        color.h += Math.random() * 15;
    }
}

class ClearText extends Sprite{
    private var text:String = "CLEAR"
    private var font:String = "Bebas"
    private var anim :TextAnim;
    public function ClearText():void{
        var fl :FontLoader = new FontLoader;
        fl.load( "Bebas" );
        fl.addEventListener( Event.COMPLETE, function(ev :Event) :void {
            var tf :TextField = new TextField;
            addChild(tf);
            tf.embedFonts = true;
            tf.defaultTextFormat = new TextFormat( "Bebas", 44, 0x000000);
            tf.text = text;
            
            anim = new TextAnim(tf);
            anim.effects = myEffect;
            anim.blocksVisible = false;
        });
    }
    private function myEffect(block:TextAnimBlock):void {
        block.scaleX = block.scaleY = 0;
        block.rotation = 0;
        Tweener.addTween(block, {rotation:0, scaleX:1, scaleY:1, time:2, transition:"easeOutBounce"});
    }
    public function start():void {
        if(anim != null){ anim.start(); }
    }
    public function reset():void {
        if(anim != null){ anim.blocksVisible = false; }
    }
}

class BitText{
    static public const COMPLETE:String = "complete";
    public var loaders:Vector.<Object> = new Vector.<Object>;
    private var text:Bitmap;
    private var LETTER:Object = {
        "1":[[0,1,1],[0,0,1],[0,0,1],[0,0,1],[0,0,1]],"2":[[1,1,1],[0,0,1],[0,1,1],[1,0,0],[1,1,1]],"3":[[1,1,1],[0,0,1],[1,1,1],[0,0,1],[1,1,1]],"4":[[1,0,1],[1,0,1],[1,0,1],[1,1,1],[0,0,1]],"5":[[1,1,1],[1,0,0],[1,1,1],[0,0,1],[1,1,1]],
        "6":[[1,1,1],[1,0,0],[1,1,1],[1,0,1],[1,1,1]],"7":[[1,1,1],[0,0,1],[0,0,1],[0,0,1],[0,0,1]],"8":[[1,1,1],[1,0,1],[1,1,1],[1,0,1],[1,1,1]],"9":[[1,1,1],[1,0,1],[1,1,1],[0,0,1],[0,0,1]],"0":[[1,1,1],[1,0,1],[1,0,1],[1,0,1],[1,1,1]],
        ".":[[0],[0],[0],[0],[1]]," ":[[0],[0],[0],[0],[0]],"n":[[0,0,0],[0,0,0],[1,1,1],[1,0,1],[1,0,1]],"w":[[0,0,0,0,0],[0,0,0,0,0],[1,0,1,0,1],[1,0,1,0,1],[1,1,1,1,1]],"o":[[0,0,0],[0,0,0],[1,1,1],[1,0,1],[1,1,1]],
        "a":[[0,0,0],[0,0,1],[1,1,1],[1,0,1],[1,1,1]],"l":[[1],[1],[1],[1],[1]],"i":[[1],[0],[1],[1],[1]],"d":[[0,0,1],[0,0,1],[1,1,1],[1,0,1],[1,1,1]],"g":[[0,0,0],[0,0,0],[1,1,1],[1,0,1],[1,1,1],[0,0,1],[1,1,1]],
        "C":[[1,1,1],[1,0,0],[1,0,0],[1,0,0],[1,1,1]],"O":[[1,1,1],[1,0,1],[1,0,1],[1,0,1],[1,1,1]],"M":[[1,1,1,1,1],[1,0,1,0,1],[1,0,1,0,1],[1,0,1,0,1],[1,0,1,0,1]],"P":[[1,1,1],[1,0,1],[1,1,1],[1,0,0],[1,0,0]],
        "T":[[1,1,1],[0,1,0],[0,1,0],[0,1,0],[0,1,0]],"L":[[1,0,0],[1,0,0],[1,0,0],[1,0,0],[1,1,1]],"E":[[1,1,1],[1,0,0],[1,1,1],[1,0,0],[1,1,1]]
    }
    //ステージと関数を渡す
    public function BitText(stage:Stage){
        text = new Bitmap( new BitmapData(100,7,true,0x0) );
        stage.addChild(text);
        with(text){scaleX=scaleY=2; x=stage.stageWidth-text.width; y=stage.stageHeight-text.height;}
    }
    public function setText(str:String):void{
        var b:BitmapData = text.bitmapData; var l:int = str.length; var position:int = b.width;
        b.lock();b.fillRect(b.rect,0x000000);
        for(var i:int=0;i<l;i++){
            var letterData:Array = LETTER[str.substr(l-i-1,1)];position-=letterData[0].length+1;
            for(var n:int=0;n<letterData.length;n++){ for(var m:int=0;m<letterData[n].length;m++){ 
                if(letterData[n][m]==1){b.setPixel32(m+position,n+1,0xFFFFFFFF);} 
            } }
        }
        b.unlock();
    }
}