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

/// <reference path="H:/Webpages/96ideas/Gamster/root/libs/phaser/build/phaser.d.ts" />
/// <reference path="./Player.ts" />

module BTSLD29 {

    export class Level1 extends Phaser.State {

        background: Phaser.Sprite;
        music: Phaser.Sound;
        player: BTSLD29.Player;
        tiledata = [
            ['30', '30', '30', '255', '57', '57', '57', '255', '44', '44', '44', '255', '44', '44', '44', '255', '57', '57', '57', '255', ],
            ['57', '57', '57', '255', '100', '100', '100', '255', '84', '84', '84', '255', '70', '70', '70', '255', '57', '57', '57', '255', ],
            ['57', '57', '57', '255', '30', '30', '30', '255', '44', '44', '44', '255', '70', '70', '70', '255', '100', '100', '100', '255', ],
            ['84', '84', '84', '255', '57', '57', '57', '255', '44', '44', '44', '255', '57', '57', '57', '255', '30', '30', '30', '255', ],
            ['100', '100', '100', '255', '84', '84', '84', '255', '57', '57', '57', '255', '57', '57', '57', '255', '57', '57', '57', '255', ],
            ['70', '70', '70', '255', '70', '70', '70', '255', '70', '70', '70', '255', '70', '70', '70', '255', '44', '44', '44', '255', ],
            ['57', '57', '57', '255', '57', '57', '57', '255', '70', '70', '70', '255', '84', '84', '84', '255', '120', '120', '120', '255', ],
            ['120', '120', '120', '255', '84', '84', '84', '255', '84', '84', '84', '255', '44', '44', '44', '255', '84', '84', '84', '255', ],
            ['120', '120', '120', '255', '100', '100', '100', '255', '100', '100', '100', '255', '84', '84', '84', '255', '84', '84', '84', '255', ],
            ['70', '70', '70', '255', '84', '84', '84', '255', '100', '100', '100', '255', '148', '148', '148', '255', '120', '120', '120', '255', ],
            ['84', '84', '84', '255', '57', '57', '57', '255', '44', '44', '44', '255', '57', '57', '57', '255', '44', '44', '44', '255', ],
        ];
        colordata = ['00a8ff', '43ae3f', '83c84e', 'bedd60', 'f1ea69', 'f4d757', 'f0b449', 'e6843b', 'ce553f', 'a7828e', 'd6d1d9'];

        cursors;
        layer;
        map;
        sirwalter: Phaser.Sprite;
        ninjatiles;

        preload() {
            // load tilemap
            this.game.load.tilemap('testlevel', 'assets/level/bts_testlevel2.json', null, Phaser.Tilemap.TILED_JSON);
            // load the tileset
            this.game.load.image('testtiles', 'assets/std/img/bts_lvl_tiles.png');
        }

        create() {

            //this.game.physics.startSystem(Phaser.Physics.P2JS);

            //  Activate the Ninja physics system
            this.game.physics.startSystem(Phaser.Physics.NINJA);

            // game.stage.backgroundColor = '#787878';

            //  The 'mario' key here is the Loader key given in game.load.tilemap
            this.map = this.game.add.tilemap('testlevel');

            //  The first parameter is the tileset name, as specified in the Tiled map editor (and in the tilemap json file)
            //  The second parameter maps this name to the Phaser.Cache key 'tiles'
            this.map.addTilesetImage('Test', 'testtiles');

            //  Creates a layer from the World1 layer in the map data.
            //  A Layer is effectively like a Phaser.Sprite, so is added to the display list.
            this.layer = this.map.createLayer('Tile Layer 1');

            //  This resizes the game world to match the layer dimensions
            this.layer.resizeWorld();


            //  Set the tiles for collision.
            //  Do this BEFORE generating the p2 bodies below.
            // this.map.setCollisionBetween(1, 10);

            // player
            this.sirwalter = this.game.add.sprite(200, 100, 'sirwalter');

            //  Convert the tilemap layer into bodies. Only tiles that collide (see above) are created.
            //  This call returns an array of body objects which you can perform addition actions on if
            //  required. There is also a parameter to control optimising the map build.
            //this.game.physics.p2.convertTilemap(this.map, this.layer);
            // this.game.physics.ninja.convertTilemap(this.map, this.layer);

            this.ninjatiles = this.game.physics.ninja.convertTilemap(this.map, this.layer);

            this.game.physics.ninja.enableCircle(this.sirwalter, this.sirwalter.width / 2);



            //this.game.physics.p2.enable(this.sirwalter);
            // this.game.physics.enable(this.sirwalter, Phaser.Physics.ARCADE);
            // this.game.physics.startSystem(Phaser.Physics.ARCADE);

            this.game.camera.follow(this.sirwalter);

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

            // this.background = this.add.sprite(0, 0, 'level1');

            // this.music = this.add.audio('music', 1, false);
            // this.music.play();

            // this.player = new Player(this.game, 130, 284);

            // this.game.physics.enable(this.player, Phaser.Physics.ARCADE);

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

        }


        update() {

            // this.game.physics.collide(this.sirwalter, this.layer);

//            if (this.cursors.left.isDown) {
//                // this.sirwalter.body.rotateLeft(100);
//                this.sirwalter.body.velocity.x = -150;
//            } else if (this.cursors.right.isDown) {
//                //this.sirwalter.body.rotateRight(100);
//                this.sirwalter.body.velocity.x = 150;
//            } else {
//                // this.sirwalter.body.setZeroRotation();
//                this.sirwalter.body.velocity.x = 0;
//            }
//
//            if (this.cursors.up.isDown) {
//                // this.sirwalter.body.thrust(400);
//                this.sirwalter.body.velocity.y = -150;
//            } else if (this.cursors.down.isDown) {
//                // this.sirwalter.body.reverse(400);
//                this.sirwalter.body.velocity.y = 150;
//            } else {
//                this.sirwalter.body.velocity.y = 0;
//            }

        }

        render() {

        }
    }

}
