/**
 * Created by Largoyle on 12/04/2014.
 */

/// <reference path="H:/Webpages/LudumDare/LD30/_assets/phaser/build/phaser.d.ts" />
/// <reference path="./Player.ts" />

module LEesLD30 {

    export class Level1 extends Phaser.State {

        background: Phaser.Sprite;

        playerspeed = 200;
        playerjumpspeed = 400;
        playerspeed_rotation = 0.05;
        playergravity = 200;
        playerstartx = 300;
        playerstarty = 300;
        jumpTimer = 0;

        framecount = 0;

        // FILTER
        usefilter = true;
        filter = [];
        FILTER_VIGNETTE     = 0;
        FILTER_FILMGRAIN    = 1;
        FILTER_SNOISE       = 2;
        FILTER_SEPIA        = 3;
        FILTER_PIXELATE     = 4;

        scoreboard: {
            time:            number

            blockcount:      number
            gemcount:        number

            bkgrimg:         Phaser.Sprite
            blockimg:        Phaser.Sprite
            gemimg:          Phaser.Sprite

            blocktext:       Phaser.Text
            gemtext:         Phaser.Text
            timetext:        Phaser.Text

            group:           Phaser.Group

            score4block:     number
            score4gem:       number
        }

        endscreen:{
            bkgrimg:         Phaser.Sprite
            text:            Phaser.Text
            group:           Phaser.Group
            active:          Boolean
        }
        connect = false;

        scanlines;
        onFloor = false;

    // BACKGROUND
        bkgr1;

        orange_block_walk: {
            movespeed:      number
            jumpspeed:      number
            sprite:         Phaser.Sprite
            touching:       Phaser.Sprite[]
            gravity:        number
            bounce:         number
            anims: {
                walk_right:    Phaser.Animation
                walk_left:     Phaser.Animation
            }
        }

        tiledata: {
            tilearray:  number[]
            collarray:  number[]
            objarray:   number[]
            tilewidth:  number
            tileheight: number
            tmapheight: number
            tmapwidth:  number
        }

        sounds: {
            walk:      Phaser.Sound
            jump:      Phaser.Sound
            collect:   Phaser.Sound
        }

        cursors;
        map;


        levelmap: {
            bkgr:           Phaser.Sprite
            tiles:          Phaser.Sprite[][]
            colls:          Phaser.Sprite[][]
            group:          Phaser.Group
            gimmicks:       Phaser.Group
            collectables:   Phaser.Group
            width:          number
            height:         number
        }
        spacedown;

        spacekey;

        jumpbutton;

        topdown;

        tokill = null;

        level : number;

        preload() {

            this.game.stage.backgroundColor = '#00a8ff';

        }

        create() {

            // init physics system
            this.game.physics.startSystem(Phaser.Physics.ARCADE);

            //this.game.physics.startSystem(Phaser.Physics.P2JS);
            //this.game.physics.p2.gravity.y  = 500;
            //this.game.physics.p2.restitution= 0.1;
            //this.game.physics.p2.friction   = 10;
            //game.physics.p2.restitution = 0.5;
            //game.physics.p2.gravity.y = 300;

            this.jumpbutton = this.game.input.keyboard.addKey(Phaser.Keyboard.UP);

            // SOUNDS
            this.sounds = {
                walk:       this.game.add.audio('snd_walk', 1, true),
                jump:       this.game.add.audio('snd_jump', 1, true),
                collect:    this.game.add.audio('snd_collect', 1, true)
                //ducks:      this.game.add.audio('snd_ducks', 1, true),
            }

            // INIT TILEDATA
            // TODO: how to do that in the declaration
            this.tiledata = {
                // tilearray:  [7, 6, 3, 2, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 6, 6, 3, 2, 1, 1, 1, 1, 1, 1, 1, 2, 4, 4, 4, 4, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 3, 2, 1, 1, 1, 1, 1, 1, 1, 2, 2, 4, 5, 3, 3, 3, 3, 2, 1, 1, 1, 1, 1, 2, 2, 2, 3, 3, 3, 2, 2, 2, 2, 2, 2, 1, 1,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 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 123, 124, 104, 104, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 26, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 123, 104, 89, 84, 84, 84, 88, 82, 125, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 103, 104, 124, 124, 0, 0, 0, 0, 0, 0, 0, 0, 0, 83, 84, 85, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 103, 82, 82, 104, 82, 82, 82, 104, 105, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 83, 84, 85, 0, 0, 0, 0, 0, 0, 123, 124, 104, 104, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 123, 104, 82, 104, 82, 104, 104, 82, 125, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 103, 104, 124, 104, 0, 0, 26, 0, 0, 0, 0, 0, 0, 26, 0, 0, 28, 0, 0, 0, 24, 0, 0, 0, 28, 0, 0, 103, 82, 104, 82, 104, 82, 82, 104, 125, 0, 0, 29, 0, 0, 0, 24, 0, 0, 0, 0, 28, 0, 0, 0, 0, 0, 0, 29, 0, 0, 24, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 123, 124, 104, 124, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 88, 82, 82, 104, 82, 82, 104, 82, 89, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 85, 0, 0, 0, 0, 29, 0, 0, 0, 0, 0, 0, 0, 0, 28, 0, 0, 83, 84, 88, 104, 124, 104, 104, 124, 104, 124, 104, 124, 104, 124, 104, 124, 104, 124, 104, 124, 104, 124, 104, 124, 104, 124, 104, 104, 104, 104, 82, 104, 82, 82, 104, 82, 82, 82, 82, 82, 82, 82, 82, 82, 104, 124, 104, 124, 104, 124, 104, 124, 104, 124, 104, 124, 104, 124, 104, 124, 104, 124, 104, 89, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 88, 124, 104, 124, 104, 124, 124, 104, 124, 104, 124, 104, 124, 104, 124, 104, 124, 104, 124, 104, 124, 104, 124, 104, 124, 104, 124, 104, 124, 104, 104, 82, 104, 104, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 124, 104, 124, 104, 124, 104, 124, 104, 124, 104, 124, 104, 124, 104, 124, 104, 124, 104, 124, 104, 124, 104, 124, 104, 124, 104, 124, 104, 124, 104, 124, 104, 124, 104, 124, 104, 124, 104, 124, 104, 124, 104],
                collarray:  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 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 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                objarray:   [],
                tilewidth:  40,
                tileheight: 40,
                tmapheight: 20,
                tmapwidth : 80
            };


        // LEVEL levelmap
            this.levelmap = {
                bkgr:       this.game.add.sprite(0, 0, 'bkgr0'),
                waves:      null,
                width:      this.tiledata.tmapheight,
                height:     this.tiledata.tmapheight,
                group:      null,
                gimmicks:   null,
                collectables: null,
                tiles:      [],
                colls:      []
            };
            for (var i = 0; i<=this.levelmap.height;i++) {
                this.levelmap.tiles[i] = [];
                    this.levelmap.colls[i] = [];
            }

                // BACKGROUND
                this.bkgr1 = this.game.add.tileSprite(0, 0, 800, 600, 'bkgr1');
                this.bkgr1.fixedToCamera = true;
                this.levelmap.bkgr.fixedToCamera = true;
                this.levelmap.bkgr.alpha = 1;



        // FILTERS
        // ==============
            if (this.usefilter) {
                this.filter[this.FILTER_VIGNETTE] = this.game.add.filter('Vignette');
                this.filter[this.FILTER_VIGNETTE].size = 0.5;
                this.filter[this.FILTER_VIGNETTE].amount = 0.45;
                this.filter[this.FILTER_VIGNETTE].alpha = 0.9;

                //filter[FILTER_SNOISE] = game.add.filter('SNoise');

                this.filter[this.FILTER_FILMGRAIN] = this.game.add.filter('FilmGrain');
                this.filter[this.FILTER_FILMGRAIN].color = 0.6;
                this.filter[this.FILTER_FILMGRAIN].amount = 0.08;
                this.filter[this.FILTER_FILMGRAIN].luminance = 0.8;

                this.filter[this.FILTER_SEPIA] = new PIXI.SepiaFilter();
                this.filter[this.FILTER_SEPIA].sepia = 0.8;

                //this.filter[this.FILTER_PIXELATE] = this.game.add.filter('Pixelate');
                //this.filter[this.FILTER_PIXELATE].size.y = 20;
                //this.filter[this.FILTER_PIXELATE].size.x = 20;

                this.game.stage.filters = [
                    this.filter[this.FILTER_FILMGRAIN],
                    this.filter[this.FILTER_SEPIA],
                    this.filter[this.FILTER_VIGNETTE]
                ];
            }


            // ORANGE BLOCK
            this.orange_block_walk = {
                sprite:         null,
                movespeed:      140,
                jumpspeed:      450,
                gravity:        800,
                bounce:         0.25,
                touching:       null,
                anims:          {
                    walk_right:  null,
                    walk_left:   null
                }
            };

            this._createLevel(this.playerstartx, this.playerstarty);


            // CRT SCANLINES
            this.scanlines = this.add.sprite(0,0,'scanlines');
            this.scanlines.fixedToCamera = true;
            this.scanlines.blendMode = PIXI.blendModes.SCREEN;


            // this.game.camera.follow(this.sirwalter_boat);
            this.game.camera.y = 0;
            this.game.camera.x = 0;

            this.cursors = this.game.input.keyboard.createCursorKeys();

            this.spacekey = this.game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);




            // SCOREBOARD
            this.scoreboard = {
                time: 0,

                blockcount:  0,
                gemcount:   0,
                // sprite:     this.game.add.sprite(660, 5, 'scoreboard'),

                bkgrimg:   this.game.add.sprite(670, 7, 'lvl1_tiles', 39),
                blockimg:  this.game.add.sprite(682, 15, 'lvl1_tiles', 40),
                gemimg:    this.game.add.sprite(682, 40, 'lvl1_tiles', 41),

                blocktext: this.game.add.text(710, 15, "", {
                    font: "16px Verdana",
                    fill: "#F0B449",
                    align: "center"
                }),
                gemtext: this.game.add.text(710, 40, "", {
                    font: "16px Verdana",
                    fill: "#F0B449",
                    align: "center"
                }),
                timetext: this.game.add.text(675, 63, "", {
                    font: "16px Verdana",
                    fill: "#009933",
                    align: "center"
                }),

                score4block: 10,
                score4gem:  40,

                group:      this.game.add.group()
            };

            this.scoreboard.bkgrimg.scale.x = 3;
            this.scoreboard.bkgrimg.scale.y = 2;

            this.scoreboard.group.enableBody = false;

            this.scoreboard.group.add(this.scoreboard.bkgrimg);
            this.scoreboard.group.add(this.scoreboard.blockimg);
            this.scoreboard.group.add(this.scoreboard.gemimg);

            this.scoreboard.group.add(this.scoreboard.blocktext);
            this.scoreboard.group.add(this.scoreboard.gemtext);
            this.scoreboard.group.add(this.scoreboard.timetext);

            this.scoreboard.group.fixedToCamera = true;

            this.scoreboard.gemcount    = 0;
            this.scoreboard.blockcount  = 0;

            this._updateScore();


            this.endscreen = {
                bkgrimg:   this.game.add.sprite(20, 20, 'lvl1_tiles', 39),
                text: this.game.add.text(70, 80, "", {
                    font: "42px Verdana",
                    fill: "#F0B449",
                    align: "center"
                }),
                group:      this.game.add.group(),
                active:     false
            };
            this.endscreen.bkgrimg.scale.x = 19;
            this.endscreen.bkgrimg.scale.y = 10;

            this.endscreen.group.add(this.endscreen.bkgrimg);
            this.endscreen.group.add(this.endscreen.text);


            this.endscreen.group.enableBody = false;
            this.endscreen.group.fixedToCamera = true;

            this.endscreen.group.setAll('alpha', 0);



        }   // create
        _createLevel(tmx: number, tmy: number) {

                // TILE GROUP
                this.levelmap.group = this.game.add.group();
                this.levelmap.group.enableBody = true;
                //this.levelmap.group.physicsBodyType = Phaser.Physics.P2JS;
                this.levelmap.group.physicsBodyType = Phaser.Physics.ARCADE;

                // GIMMICS GROUP
                this.levelmap.gimmicks = this.game.add.group();
                this.levelmap.gimmicks.enableBody = false;

                // COLLECTABLE GROUP
                this.levelmap.collectables = this.game.add.group();
                this.levelmap.collectables.enableBody = true;

                // ADD TILES
                for (var y=0; y<this.tiledata.tmapheight; y++) {
                    for (var x=0; x<this.tiledata.tmapwidth; x++) {

                        var tileid =  this.tiledata.tilearray[ y * this.tiledata.tmapwidth + x] - 1;

                        // console.log('tileid: ' + tileid);

                        // if not empty, create sprite for the current block
                        if (tileid>0) {
                            this.levelmap.tiles[y][x] =
                                this.game.add.sprite(
                                    x * this.tiledata.tilewidth,
                                    y * this.tiledata.tileheight,
                                    'lvl1_tiles',
                                    tileid);
                            // add the block to the group to make collision possible

                            if (tileid<20) {
                                this.levelmap.collectables.add(this.levelmap.tiles[y][x]);
                            }
                            if (tileid>=20 && tileid<80) {
                                this.levelmap.gimmicks.add(this.levelmap.tiles[y][x]);
                            }
                            if (tileid>=80) {
                                this.levelmap.group.add(this.levelmap.tiles[y][x]);
                                this.levelmap.tiles[y][x].body.immovable = true;
                            }
                        }

                        // COLLECTABLES
                        //var collectable = this.leveldata.collectables[y][tmx];
                        //if (collectable!=null) {
                        //    var temp =
                        //        this.game.add.sprite(
                        //            // same x pos
                        //            this.camera.x + xoffs + (11 - y) * this.tiledata.tilewidth,
                        //
                        //            yoffs,
                        //            'tiles_collectables',
                        //            collectable.frame);
                        //    temp.name = "coll_" + tmx.toString() + "-" + y.toString();
                        //    this.levelmap.group.add(temp);
                        //    temp.body.fixedRotation = true;
                        //    // temp.body.static = true;
                        //}

                    }

                }

                // ORANGE BLOCK
                // SPRITES
                this.orange_block_walk.sprite =
                    this.game.add.sprite(
                        tmx,
                        tmy,
                        'anim_orangeblock');
                this.orange_block_walk.anims.walk_right =
                    this.orange_block_walk.sprite.animations.add('right', [0,1,2,3,4,5,6,7], 30, true);
                this.orange_block_walk.anims.walk_left =
                    this.orange_block_walk.sprite.animations.add('left', [10,11,12,13,14,15,16,17], 30, true);

                // PHYSICS
                //this.game.physics.enable(this.orange_block_walk.sprite, Phaser.Physics.P2JS);
                this.game.physics.enable(this.orange_block_walk.sprite, Phaser.Physics.ARCADE);

                this.orange_block_walk.sprite.body.gravity.y = this.orange_block_walk.gravity;
                this.orange_block_walk.sprite.body.bounce.y  = this.orange_block_walk.bounce;
                this.orange_block_walk.sprite.body.collideWorldBounds = true;
                this.orange_block_walk.sprite.alpha = 1;

                //this.orange_block_walk.sprite.body.friction = 100;
                //this.orange_block_walk.sprite.body.fixedRotation = true;


                // console.log(this.camera.x + xoffs + (10-tmy) * this.tiledata.tilewidth);

                // make the camera follow our guy
                this.game.camera.follow(this.orange_block_walk.sprite, Phaser.Camera.FOLLOW_PLATFORMER);

                this.game.world.setBounds(
                    0, 0,
                    this.tiledata.tmapwidth  * this.tiledata.tilewidth,
                    this.tiledata.tmapheight * this.tiledata.tileheight);

        }

        _updateScore() {
            // this.scoreboard.text.setText(this.scoreboard.string + this.scoreboard.score.toString());
            this.scoreboard.blocktext.setText(" x " + this.scoreboard.blockcount.toString());
            this.scoreboard.gemtext.setText(" x " + this.scoreboard.gemcount.toString());
            var val = this.scoreboard.gemcount * this.scoreboard.score4gem + this.scoreboard.blockcount * this.scoreboard.score4block;
            this.scoreboard.timetext.setText(" Points: " + val.toString());
        }

        update() {

            if (!this.endscreen.active) {

                this.framecount++;
                this.framecount = this.framecount % 3;

                this._updatelevelmap();

                // background
                // console.log(this.game.camera.x + ' / ' + this.game.camera.y);
                this.bkgr1.tilePosition.y = Math.floor(-this.game.camera.y * 150 / 350);
                this.bkgr1.tilePosition.x = Math.floor(-this.game.camera.x * 0.8);

                if (this.usefilter && this.framecount == 0)
                    this.filter[this.FILTER_FILMGRAIN].update();

            }
        }

        _testFloor(msprite, gsprite) {
            // console.log(gsprite.world.y + ' - ' + msprite.world.y);
            //gsprite.alpha = 0.5;
            var mleftx  = msprite.world.x;
            var mrightx = msprite.world.x + this.tiledata.tilewidth;
            var gleftx  = gsprite.world.x;
            var grightx = gsprite.world.x + this.tiledata.tilewidth;
            if (   (gsprite.world.y > msprite.world.y)
                && ( // mleftx inside of block dim
                    ( (gleftx < mleftx) && (grightx > mleftx))
                    ||// mrightx inside of block dim
                    ( (gleftx < mrightx) && (grightx > mrightx))
                    )
                ) {
                this.onFloor = true;
            } else {
                this.onFloor = false;
            }
        }

        _connectPlayer() {
            var nublock =
                this.game.add.sprite(
                    this.orange_block_walk.sprite.world.x,
                    this.orange_block_walk.sprite.world.y,
                    'lvl1_tiles',
                    120);
            // add the block to the group to make collision possible
            this.levelmap.group.add(nublock);
            nublock.body.immovable = true;

            // detatch camera
            this.game.camera.follow(null);

            // reposition player
            this.orange_block_walk.sprite.body.gravity.y = 0;
            this.orange_block_walk.sprite.body.velocity.x = 0;
            this.orange_block_walk.sprite.body.velocity.y = 0;
            this.orange_block_walk.sprite.x = this.playerstartx;
            this.orange_block_walk.sprite.y = this.playerstarty;
            this.orange_block_walk.sprite.alpha = 1;

            // wait 2 sec, then zoom to player
            var thot = this;
            setTimeout(function(){
                var thut = thot;
                var nutween = thut.game.add.tween(thut.game.camera);
                nutween.to(
                    {
                        //x: this.orange_block_walk.sprite.x - (this.game.camera.width / 2),
                        //y: this.orange_block_walk.sprite.y - (this.game.camera.height / 2)
                        x: thut.playerstartx - (thut.game.camera.width / 2),
                        y: thut.playerstarty - (thut.game.camera.height / 2)
                    },
                    500, Phaser.Easing.Quadratic.InOut, true);
                nutween.start();
                // after the camera reaches the player source, attach it again and add gravity to player
                setTimeout(function(){
                    var that = thot;
                    that.game.camera.follow(that.orange_block_walk.sprite, Phaser.Camera.FOLLOW_PLATFORMER);
                    that.orange_block_walk.sprite.body.gravity.y = that.orange_block_walk.gravity;
                    that.connect = false;
                }, 550);
            }, 500);
        }

        _collectIt(player, collectable) {
            this.sounds.collect.play('', 0, 1, false, true);
            if (collectable.frame==5) {
                this.scoreboard.blockcount++;
            } else {
                this.scoreboard.gemcount++;
            }
            this._updateScore();

            collectable.kill();

            if (collectable.frame==19) {

                var val = this.scoreboard.gemcount * this.scoreboard.score4gem + this.scoreboard.blockcount * this.scoreboard.score4block;
                this.endscreen.text.setText(" Thank you for playing this g...\n\n er... tech-demo!\n\nYour score was  " + val.toString());

                this.endscreen.group.setAll('alpha', 1);

                this.sounds.walk.stop();

                this.orange_block_walk.sprite.body.velocity.x = 0;
                this.orange_block_walk.sprite.body.velocity.y = 0;
                this.orange_block_walk.sprite.body.gravity.x = 0;
                this.orange_block_walk.sprite.body.gravity.y = 0;
                this.orange_block_walk.sprite.alpha = 0;

                //this.orange_block_walk.sprite.animations.stop('left', true);
                //this.orange_block_walk.sprite.animations.stop('right', true);

                this.endscreen.active = true;

                this.sounds.walk.stop();
            }
        }

        _updatelevelmap() {

            // var onFloor = this.game.physics.arcade.collide(this.orange_block_walk.sprite, this.levelmap.group, null, this._testFloor, this);
            var touching = this.game.physics.arcade.collide(this.orange_block_walk.sprite, this.levelmap.group, this._testFloor, null, this);
            this.game.physics.arcade.collide(this.orange_block_walk.sprite, this.levelmap.collectables, this._collectIt, null, this);
            if (!touching)
                this.onFloor = false;

            // CONNECT
            // console.log(touching + ' - ' + this.connect);
            if (touching && this.connect) {
                // add block and reset player
                this.orange_block_walk.sprite.alpha = 0;

                this._connectPlayer();

                this.connect = false;

            }

        // LEFT down
            if (this.cursors.left.isDown) {

                // this.orange_block_walk.sprite.body.moveLeft(this.orange_block_walk.movespeed);
                this.orange_block_walk.sprite.body.velocity.x = -this.playerspeed;

                // this.game.camera.x -= 10;

                // start walking animation if it is not running yet
                if (!this.orange_block_walk.anims.walk_left.isPlaying) {
                    this.orange_block_walk.sprite.animations.play('left');
                    this.sounds.walk.play('', 0, 1, true, false);
                }
            } else {
                // left button not pressed, stop walking animation if it is still running
                if (this.orange_block_walk.anims.walk_left.isPlaying) {
                    this.orange_block_walk.sprite.animations.stop('left', true);
                    if (!this.orange_block_walk.anims.walk_right.isPlaying)
                        this.sounds.walk.stop();
                }
                if (this.orange_block_walk.sprite.body.velocity.x < 0)
                    this.orange_block_walk.sprite.body.velocity.x = 0;
            }

        // RIGHT down
            if (this.cursors.right.isDown) {

                // P2 this.orange_block_walk.sprite.body.moveRight(this.orange_block_walk.movespeed);
                this.orange_block_walk.sprite.body.velocity.x = this.playerspeed;
                // this.game.camera.x += 10;

                // start walking animation if it is not running yet
                if (!this.orange_block_walk.anims.walk_right.isPlaying) {
                    this.orange_block_walk.sprite.animations.play('right');
                    this.sounds.walk.play('', 0, 1, true, false);
                }
            } else {
                // right button not pressed, stop walking animation if it is still running
                if (this.orange_block_walk.anims.walk_right.isPlaying) {
                    this.orange_block_walk.sprite.animations.stop('right', true);
                    if (!this.orange_block_walk.anims.walk_left.isPlaying)
                        this.sounds.walk.stop();
                }
                if (this.orange_block_walk.sprite.body.velocity.x > 0)
                    this.orange_block_walk.sprite.body.velocity.x = 0;
            }

            if (this.cursors.up.isDown && this.onFloor // &&  this.game.time.now > this.jumpTimer
                ) {
                this.orange_block_walk.sprite.body.velocity.y = -this.orange_block_walk.jumpspeed;
                if (!this.sounds.jump.isPlaying) {
                    this.sounds.jump.play('', 0, 1, false, true);
                }
            }

            if ( this.spacekey.isDown ) {

                // activate stickyness
                if (this.scoreboard.blockcount>0) {
                    this.connect = true;
                    this.scoreboard.blockcount--;
                    this._updateScore();
                }

                // convert player to cube


            }

        }




        render() {

        }

    }

}
