Ukigumo!

by Jacky.Riawan
click to jump
♥2 | Line 246 | Modified 2013-08-21 01:45:30 | MIT License
play

ActionScript3 source code

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

package 
{
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Loader;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.geom.Point;
    import flash.geom.Rectangle;
    import flash.net.URLRequest;
    import flash.system.Security;
    /**
     * ...
     * @author Jacky Riawan
     */
    public class Main extends Sprite 
    {
        private const ukigumoFrame1:Array = [[0, 0, 1, 1, 1, 1, 1, 1, 0, 1],
                                            [0, 0, 1, 1, 1, 1, 1, 1, 0, 1],
                                            [1, 1, 1, 0, 1, 1, 0, 1, 1, 1],
                                            [1, 0, 1, 1, 1, 1, 1, 1, 0, 0],
                                            [1, 0, 1, 1, 1, 1, 1, 1, 0, 0],
                                            [0, 0, 1, 1, 1, 1, 1, 1, 0, 0],
                                            [0, 0, 1, 1, 1, 1, 1, 1, 0, 0],
                                            [0, 0, 0, 1, 0, 0, 1, 0, 0, 0],
                                            [0, 0, 0, 1, 0, 0, 1, 0, 0, 0],
                                            [0, 0, 0, 1, 0, 0, 0, 0, 0, 0],
                                            [0, 0, 0, 1, 0, 0, 0, 0, 0, 0]];
        private const ukigumoFrame2:Array = [[1, 0, 1, 1, 1, 1, 1, 1, 0, 0],
                                            [1, 0, 1, 1, 1, 1, 1, 1, 0, 0],
                                            [1, 1, 1, 0, 1, 1, 0, 1, 1, 1],
                                            [0, 0, 1, 1, 1, 1, 1, 1, 0, 1],
                                            [0, 0, 1, 1, 1, 1, 1, 1, 0, 1],
                                            [0, 0, 1, 1, 1, 1, 1, 1, 0, 0],
                                            [0, 0, 1, 1, 1, 1, 1, 1, 0, 0],
                                            [0, 0, 0, 1, 0, 0, 1, 0, 0, 0],
                                            [0, 0, 0, 1, 0, 0, 1, 0, 0, 0],
                                            [0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
                                            [0, 0, 0, 0, 0, 0, 1, 0, 0, 0]];
        private var bitmapData:BitmapData
        private const SCALING:int = 4;
        private var ukigumoSprites:Array = [];
        private var ukigumoFrame:int = 0;
        private var ukiJump:int = 0;
        private var ukiPoint:Point = new Point();
        private var step:int = 0;
        private var mouseIsDown:Boolean = false;
        private var fillRect:Rectangle;
        private var obstacleRect:Rectangle = new Rectangle(0, 0, 10, 10);
        private var obstacles:Array = new Array();
        private var obstacleTimer:int = 0;
        private var damageTimer:int = 0;
        private var gameSpeed:int = 10;
        private var gameSpeedMax:int = 10;
        private var timeElapsed:int = 0;
        private var loader:Loader = new Loader();
        public function Main():void 
        {
            renderBitmap(ukigumoFrame1, ukigumoSprites);
            renderBitmap(ukigumoFrame2, ukigumoSprites);
            bitmapData = new BitmapData(stage.stageWidth / SCALING, stage.stageHeight / SCALING, false);
            var screen:Bitmap=new Bitmap(bitmapData)
            addChild(screen);
            fillRect = new Rectangle(0, 0, bitmapData.width, bitmapData.height / 2 + 11);
            obstacleRect.y = bitmapData.height/2+1;
            screen.scaleX = screen.scaleY = SCALING;
            stage.addEventListener(MouseEvent.CLICK, mouseDownHandler);
            update(null);
            timeElapsed = 0;
            Security.allowDomain("www.youtube.com");
            loader.contentLoaderInfo.addEventListener(Event.INIT, onLoaderInit);
            loader.load(new URLRequest("http://www.youtube.com/v/3q2LsMRtXMw?version=3"));
        }
        private function onLoaderInit(e:Event):void 
        {
            loader.content.addEventListener("onReady", onPlayerReady);
            loader.contentLoaderInfo.removeEventListener(Event.INIT, onLoaderInit);
        }        
        private function onPlayerReady(e:Event):void 
        {
            stage.frameRate=60;
            addEventListener(Event.ENTER_FRAME, update);
            Object(loader.content).loadVideoById("3q2LsMRtXMw", 0, "small");
        }
        private function mouseDownHandler(e:MouseEvent):void 
        {
            if(!mouseIsDown){
                mouseIsDown = true;
            }
        }
        private function renderBitmap(data:Array, target:Array):void {
            var sprite:BitmapData = new BitmapData(data[0].length, data.length, false);
            for (var _y:int = 0; _y < data.length; _y++) {
                for (var _x:int = 0; _x < data[_y].length; _x++) {
                    sprite.setPixel(_x, _y, data[_y][_x]*0xFFFFFF);
                }
            }
            target.push(sprite);
        }
        private function updateUkigumo():void {
            if(step%2==0){
                ukigumoFrame = (ukigumoFrame + 1) % 2;
            }
            var jumpOffset:int = obstacleRect.height+3;
            if (ukiJump <= 0) {
                if (mouseIsDown) {
                    mouseIsDown = false;
                    ukiJump = 30;
                }else {
                    jumpOffset = 0;
                }
            }else {
                ukiJump-=gameSpeedMax+1;
            }
            ukiPoint.x = bitmapData.width * .3;
            ukiPoint.y = bitmapData.height / 2-jumpOffset;
            bitmapData.copyPixels(ukigumoSprites[ukigumoFrame], ukigumoSprites[ukigumoFrame].rect, ukiPoint);
        }
        private function renderObstacles():void {
            for (var i:int = 0; i < obstacles.length; i++) {
                if (obstacles[i] <= -40) {
                    obstacles.splice(i, 1);
                    i--;
                }else {
                    obstacles[i] = obstacleRect.x = obstacles[i] - 10;
                    if (ukiJump <= 0) {
                        if(damageTimer<=0){
                            if (Math.abs(ukiPoint.x-obstacleRect.x)<5){
                                damageTimer=30;
                            }
                        }
                    }
                    bitmapData.fillRect(obstacleRect, 0xFFFFFF);
                }
            }
            if (obstacleTimer == 0) {
                obstacles.push(bitmapData.width + obstacleRect.width);
                obstacleTimer = 40 + Math.random() * 50;
            }else {
                obstacleTimer--;
            }
        }
        private function update(e:Event):void 
        {
            if(gameSpeed==0){
                bitmapData.lock();
                fillRect.y = 0;
                bitmapData.fillRect(fillRect, 0);
                fillRect.y = bitmapData.height / 2+11;
                bitmapData.fillRect(fillRect, 0xFFFFFF);
                updateUkigumo();
                renderObstacles();
                if (damageTimer > 0) {
                    bitmapData.paletteMap(bitmapData, bitmapData.rect, new Point(), null, [0], [0]);
                    damageTimer-=gameSpeedMax+1;
                }
                bitmapData.unlock();
                step++;
                gameSpeed = gameSpeedMax;
            }else {
                gameSpeed--;
            }
            timeElapsed++;
            switch(timeElapsed) {
                case 840:
                    gameSpeedMax = 8;
                    break;
                case 960:
                    gameSpeedMax = 7;
                    break;
                case 1260:
                    gameSpeedMax = 6;
                    break;
                case 1320:
                    gameSpeedMax = 5;
                    break;
                case 1920:
                    gameSpeedMax = 1;
                    break;
                case 2460:
                    gameSpeedMax = 4;
                    break;
                case 3180:
                    gameSpeedMax = 2;
                    break;
                case 3780:
                    gameSpeedMax = 2;
                    break;
                case 4500:
                    gameSpeedMax = 1;
                    break;
                case 4740:
                    gameSpeedMax = 0;
                    break;
                case 5160:
                    gameSpeedMax = 2;
                    break;
                case 5280:
                    gameSpeedMax = 4;
                    break;
                case 5320:
                    gameSpeedMax = 0;
                    break;
                case 5340:
                    gameSpeedMax = 9;
                    break;
                case 5460:
                    gameSpeedMax = 0;
                    break;
                case 6540:
                    gameSpeedMax = 4;
                    break;
                case 6600:
                    gameSpeedMax = 8;
                    break;
                case 7200:
                    gameSpeedMax = 6;
                    break;
                case 7920:
                    gameSpeedMax = 3;
                    break;
                case 8520:
                    gameSpeedMax = 4;
                    break;
                case 9160:
                    gameSpeedMax = 6;
                    break;
                case 9160:
                    gameSpeedMax = 6;
                    break;
                case 9840:
                    gameSpeedMax = 0;
                    break;
                case 11980:
                    gameSpeedMax = 2;
                    break;
                case 12120:
                    gameSpeedMax = 4;
                    break;
                case 12420:
                    gameSpeedMax = 7;
                    break;
                case 12660:
                    gameSpeedMax = int.MAX_VALUE;
                    break;
                }
        }

    }
}