/**
* Created by Largoyle on 12/04/2014.
*/
var __extends = this.__extends || function (d, b) {
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    function __() { this.constructor = d; }
    __.prototype = b.prototype;
    d.prototype = new __();
};
/// <reference path="H:/Webpages/96ideas/Gamster/root/libs/phaser/build/phaser.d.ts" />
/// <reference path="./Player.ts" />
var BTSLD29;
(function (BTSLD29) {
    var Level1 = (function (_super) {
        __extends(Level1, _super);
        function Level1() {
            _super.apply(this, arguments);
            this.playerspeed = 200;
            this.playerjumpspeed = 40;
            this.playerspeed_rotation = 0.05;
            this.playergravity = 200;
            this.tokill = null;
        }
        Level1.prototype.preload = function () {
            this.game.stage.backgroundColor = '#00a8ff';
        };

        Level1.prototype.create = function () {
            this.game.physics.startSystem(Phaser.Physics.ARCADE);

            this.game.physics.startSystem(Phaser.Physics.P2JS);
            this.game.physics.p2.gravity.y = 1200;
            this.game.physics.p2.restitution = 0.25;
            this.game.physics.p2.friction = 10;

            this.topdown = true;

            // SOUNDS
            this.sounds = {
                boatmotor: this.game.add.audio('snd_boatmotor', 1, true),
                ducks: this.game.add.audio('snd_ducks', 1, true),
                sirwalter: null,
                water: null
            };

            // 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, 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, 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, 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, 1, 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, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 0, 1, 0, 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, 1, 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: 11,
                tmapwidth: 80
            };

            // INIT leveldata topdown
            // TODO: how to do that in the declaration
            this.leveldata = {
                tiles: [],
                collectables: [],
                objects: [],
                sprites: [],
                groupTopDown: null,
                groupTopDown_coll: null,
                groupLeftRight: null
            };
            for (var i = 0; i < this.tiledata.tmapwidth; i++) {
                this.leveldata.tiles[i] = [];
                this.leveldata.sprites[i] = [];
                this.leveldata.collectables[i] = [];
                this.leveldata.objects[i] = [];
            }

            this.level = 11;
            this.spacedown = false;

            this._createLevelTopDown();

            this._setWaterLevel(this.level);

            // LEVEL LEFTRIGHT
            this.leftright = {
                bkgr: this.game.add.sprite(0, 0, 'leftright_bkgr'),
                waves: null,
                width: this.tiledata.tmapheight,
                height: this.tiledata.tmapheight,
                group: null,
                tiles: [],
                colls: []
            };
            for (var i = 0; i <= this.leftright.height; i++) {
                this.leftright.tiles[i] = [];
                this.leftright.colls[i] = [];
            }
            this.leftright.bkgr.fixedToCamera = true;
            this.leftright.bkgr.alpha = 0;

            // SIR WALTER
            this.sirwalter_walk = {
                sprite: null,
                movespeed: 140,
                jumpspeed: 240,
                touching: null
            };

            this.sirwalter_boat = this.game.add.sprite(200, 200, 'sirwalter_boat');
            this.sirwalter_boat.anchor.setTo(0.4, 0.5);

            // TODO: make the physical body square and smaller
            this.game.physics.enable(this.sirwalter_boat, Phaser.Physics.ARCADE);
            this.sirwalter_boat.body.collideWorldBounds = true;

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

            // SCOREBOARD
            this.scoreboard = {
                sprite: this.game.add.sprite(660, 5, 'scoreboard'),
                scorearrow: this.game.add.sprite(759, 10, 'scorearrow'),
                scoreduck: this.game.add.sprite(655, 25, 'tiles_collectables', 0),
                scoregem: this.game.add.sprite(705, 25, 'tiles_collectables', 1),
                text: this.game.add.text(665, 10, "", {
                    font: "16px Verdana",
                    fill: "#009933",
                    align: "center"
                }),
                ducktext: this.game.add.text(690, 37, "", {
                    font: "12px Verdana",
                    fill: "#F0B449",
                    align: "center"
                }),
                gemtext: this.game.add.text(732, 37, "", {
                    font: "12px Verdana",
                    fill: "#F0B449",
                    align: "center"
                }),
                string: "Points:",
                duckcount: 0,
                gemcount: 0,
                duckperlevel: 2,
                score: 0,
                score4duck: 10,
                score4gem: 40,
                group: this.game.add.group()
            };

            this.scoreboard.group.enableBody = false;

            this.scoreboard.group.add(this.scoreboard.sprite);
            this.scoreboard.group.add(this.scoreboard.scorearrow);
            this.scoreboard.group.add(this.scoreboard.scoreduck);
            this.scoreboard.group.add(this.scoreboard.scoregem);
            this.scoreboard.group.add(this.scoreboard.text);
            this.scoreboard.group.add(this.scoreboard.ducktext);
            this.scoreboard.group.add(this.scoreboard.gemtext);

            this.scoreboard.group.fixedToCamera = true;

            this._updateScore();

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

            this.spacekey = this.game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);
            //            this.add.tween(this.leveldata.groupTopDown_coll).to({ alpha: 1 }, 200, Phaser.Easing.Linear.None, true);
            //            this.add.tween(this.leveldata.groupTopDown).to({ alpha: 1 }, 200, Phaser.Easing.Linear.None, true);
        };

        Level1.prototype._createLevelLeftRight = function (tmx, tmy) {
            var xoffs = 120;
            var yoffs = 40;

            // TILES
            this.leftright.group = this.game.add.group();
            this.leftright.group.enableBody = true;
            this.leftright.group.physicsBodyType = Phaser.Physics.P2JS;

            // waves
            console.log(this.camera.x);
            this.leftright.waves = this.game.add.sprite(this.camera.x + 400, 440, 'leftright_waves', 0);
            this.leftright.group.add(this.leftright.waves);
            this.leftright.waves.body.static = true;

            for (var y = 0; y < this.leftright.width; y++) {
                // is the current tile above water
                var current_tile = this.leveldata.tiles[y][tmx];

                if ((current_tile > this.level) || (current_tile == 11)) {
                    var height = current_tile - this.level + 1;

                    this.leftright.tiles[current_tile][y] = this.game.add.sprite(this.camera.x + xoffs + (11 - y) * this.tiledata.tilewidth, yoffs + (11 - height) * this.tiledata.tileheight, 'lvl1_tiles', current_tile - 1);
                    this.leftright.group.add(this.leftright.tiles[current_tile][y]);
                    this.leftright.tiles[current_tile][y].body.static = true;

                    for (var n = 0; n < height; n++) {
                        this.leftright.tiles[n][y] = this.game.add.sprite(this.camera.x + xoffs + (11 - y) * this.tiledata.tilewidth, yoffs + (11 - n) * this.tiledata.tileheight, 'lvl1_tiles', 15);
                        this.leftright.group.add(this.leftright.tiles[n][y]);
                        this.leftright.tiles[n][y].body.static = true;
                    }

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

            // SIR WALTER
            this.sirwalter_walk.sprite = this.game.add.sprite(this.camera.x + xoffs + (12 - tmy) * this.tiledata.tilewidth, 300, 'sirwalter');
            this.game.physics.enable(this.sirwalter_walk.sprite, Phaser.Physics.P2JS);
            this.sirwalter_walk.sprite.body.friction = 100;
            this.sirwalter_walk.sprite.body.collideWorldBounds = true;
            this.sirwalter_walk.sprite.body.fixedRotation = true;
            this.sirwalter_walk.sprite.alpha = 1;

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

        Level1.prototype._createLevelTopDown = function () {
            // TILES
            this.leveldata.groupTopDown = this.game.add.group();
            this.leveldata.groupTopDown.enableBody = false;
            this.leveldata.groupTopDown.physicsBodyType = Phaser.Physics.ARCADE;

            for (var y = 0; y < this.tiledata.tmapwidth; y++) {
                for (var x = 0; x < this.tiledata.tmapwidth; x++) {
                    this.leveldata.tiles[y][x] = this.tiledata.tilearray[y * this.tiledata.tmapwidth + x];

                    if (this.leveldata.tiles[y][x] != 1) {
                        this.leveldata.sprites[y][x] = this.game.add.sprite(x * this.tiledata.tilewidth, y * this.tiledata.tileheight, 'lvl1_tiles', this.tiledata.tilearray[y * this.tiledata.tmapwidth + x] - 1);
                        this.leveldata.groupTopDown.add(this.leveldata.sprites[y][x]);
                        // this.leveldata.sprites[y][x].body.immovable = true;
                    } else {
                        this.leveldata.sprites[y][x] = null;
                    }
                }
            }

            // COLLECTABLES
            this.leveldata.groupTopDown_coll = this.game.add.group();
            this.leveldata.groupTopDown_coll.enableBody = false;
            this.leveldata.groupTopDown_coll.physicsBodyType = Phaser.Physics.ARCADE;

            for (var y = 0; y < this.tiledata.tmapwidth; y++) {
                for (var x = 0; x < this.tiledata.tmapwidth; x++) {
                    if (this.tiledata.collarray[y * this.tiledata.tmapwidth + x] > 0) {
                        this.leveldata.collectables[y][x] = this.game.add.sprite(x * this.tiledata.tilewidth, y * this.tiledata.tileheight, 'tiles_collectables', this.tiledata.collarray[y * this.tiledata.tmapwidth + x] - 1);
                        this.leveldata.groupTopDown_coll.add(this.leveldata.collectables[y][x]);
                    } else {
                        this.leveldata.collectables[y][x] = null;
                    }
                }
            }

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

        // hide all tiles with an index lower than level
        Level1.prototype._setWaterLevel = function (level) {
            for (var y = 0; y < this.tiledata.tmapheight; y++) {
                for (var x = 0; x < this.tiledata.tmapwidth; x++) {
                    // TILES
                    // is is below the surface?
                    if (this.leveldata.sprites[y][x] != null) {
                        if (this.leveldata.tiles[y][x] < level) {
                            // fade more the deeper it is
                            var depth = level - this.leveldata.tiles[y][x];
                            this.leveldata.sprites[y][x].alpha = 0.25 - depth * 0.02;
                        } else {
                            // show block and make it inpassable
                            this.leveldata.sprites[y][x].alpha = 1;

                            this.game.physics.enable(this.leveldata.sprites[y][x], Phaser.Physics.ARCADE);
                            this.leveldata.sprites[y][x].body.immovable = true;
                        }
                    }

                    // COLLECTABLES
                    // DUCKS
                    if ((this.leveldata.collectables[y][x] != null) && (this.leveldata.collectables[y][x].frame == 1)) {
                        // console.log("X: " + x + ", Y: " + y + ", A: " + this.leveldata.sprites[y][x].alpha);
                        // test if visible
                        if (this.leveldata.sprites[y][x].alpha == 1) {
                            this.leveldata.collectables[y][x].alpha = 1;
                            // add a body NOT NECESSARY, we can't get them anyhow
                            //                            this.game.physics.enable(this.leveldata.collectables[y][x], Phaser.Physics.ARCADE);
                            //                            this.leveldata.collectables[y][x].body.immovable = true;
                        } else {
                            this.leveldata.collectables[y][x].alpha = 0;
                        }
                        // GEMS
                    } else if ((this.leveldata.collectables[y][x] != null) && (this.leveldata.collectables[y][x].frame == 0)) {
                        this.game.physics.enable(this.leveldata.collectables[y][x], Phaser.Physics.ARCADE);
                        this.leveldata.collectables[y][x].body.immovable = true;
                    }
                }
            }
        };

        Level1.prototype._collector = function (sirw, collectable) {
            // DUCKS
            if (collectable.frame == 0) {
                this.scoreboard.score += this.scoreboard.score4duck;
                this.sounds.ducks.play('', 0, 1, false, true);
                this.scoreboard.duckcount++;

                if ((this.scoreboard.duckcount % this.scoreboard.duckperlevel) == 0) {
                    if (this.level > 0) {
                        this._setWaterLevel(this.level);
                        this.level--;
                        if (this.level != 10)
                            this.scoreboard.scorearrow.y += 5;
                    }
                }

                this._updateScore();
                collectable.kill();
                // GEMS
            } else if (collectable.frame == 1) {
                this.scoreboard.score += this.scoreboard.score4gem;

                // this.sounds.ducks.play('', 0, 1, false, true);
                this.scoreboard.gemcount++;
            }
        };

        Level1.prototype._updateScore = function () {
            this.scoreboard.text.setText(this.scoreboard.string + this.scoreboard.score.toString());
            this.scoreboard.ducktext.setText(" x " + this.scoreboard.duckcount.toString());
            this.scoreboard.gemtext.setText(" x " + this.scoreboard.gemcount.toString());
        };

        Level1.prototype._updateLeftRight = function () {
            if (this.cursors.left.isDown) {
                this.sirwalter_walk.sprite.body.moveLeft(this.sirwalter_walk.movespeed);
            } else if (this.cursors.right.isDown) {
                this.sirwalter_walk.sprite.body.moveRight(this.sirwalter_walk.movespeed);
            }

            if (this.cursors.up.isDown) {
                if (this.sirwalter_walk.touching != null)
                    this.sirwalter_walk.sprite.body.moveUp(this.sirwalter_walk.jumpspeed);
            } else if (this.cursors.down.isDown) {
                this.sirwalter_walk.sprite.body.moveDown(this.sirwalter_walk.movespeed);
            }

            this.sirwalter_walk.sprite.body.onBeginContact.add(this._blockHit, this);
            this.sirwalter_walk.sprite.body.onEndContact.add(this._blockLeave, this);

            if (this.tokill != null) {
                if (this.tokill.name != null) {
                    var temp = this.tokill.name.substr(5, this.tokill.name.length);
                    var x = parseInt(temp.substr(0, temp.indexOf('-')));
                    var y = parseInt(temp.substr(temp.indexOf('-') + 1, temp.length));

                    this.leveldata.collectables[y][x].kill();
                    this.leveldata.collectables[y][x] = null;

                    this.scoreboard.score += this.scoreboard.score4gem;
                    this.sounds.ducks.play('', 2, 1, false, true);
                    this.scoreboard.gemcount++;

                    this._updateScore();
                }
                this.tokill.kill();
                this.tokill = null;
            }
        };

        Level1.prototype._blockHit = function (body, shapeA, shapeB, equation) {
            //	The block hit something
            //	This callback is sent: the Body it collides with
            //	shapeA is the shape in the calling Body involved in the collision
            //	shapeB is the shape in the Body it hit
            //	equation is an array with the contact equation data in it
            console.log('You last hit: ' + body.sprite.key);

            // switch back to normal mode
            if (body.sprite.key == 'leftright_waves') {
                this._activateTopDown();
                this.topdown = true;
                // TODO replace sir walter if necessary
            } else if (body.sprite.key == 'tiles_collectables') {
                this.tokill = body.sprite;
            } else {
                // TODO make sure it is touching the top (compare y?)
                this.sirwalter_walk.touching = body.sprite.key;
            }
        };

        Level1.prototype._blockLeave = function (body, shapeA, shapeB, equation) {
            this.sirwalter_walk.touching = null;
        };

        Level1.prototype._updateTopDown = function () {
            this.game.physics.arcade.collide(this.sirwalter_boat, this.leveldata.groupTopDown, null, null, this);
            this.game.physics.arcade.collide(this.sirwalter_boat, this.leveldata.groupTopDown_coll, this._collector, null, this);

            if (this.cursors.left.isDown) {
                this.sirwalter_boat.rotation -= this.playerspeed_rotation;
            } else if (this.cursors.right.isDown) {
                this.sirwalter_boat.rotation += this.playerspeed_rotation;
            } else {
            }

            if (this.cursors.up.isDown) {
                var angle = (this.sirwalter_boat.angle / 180) * Math.PI;

                this.sirwalter_boat.body.velocity.x = this.playerspeed * Math.cos(angle);
                this.sirwalter_boat.body.velocity.y = this.playerspeed * Math.sin(angle);

                // play boat motor
                this.sounds.boatmotor.play('', 0, 1, true, false);
                //                this.boatmotor.play();
            } else if (this.cursors.down.isDown) {
                // this.sirwalter_boat.body.velocity.y = this.playerspeed;
            } else {
                this.sirwalter_boat.body.velocity.y = 0;
                this.sirwalter_boat.body.velocity.x = 0;
                this.sounds.boatmotor.stop();
            }
        };

        Level1.prototype.update = function () {
            // TOPDOWN VIEW
            // ----------------
            if (this.topdown) {
                this._updateTopDown();
                // LEFTRIGHT VIEW
            } else {
                this._updateLeftRight();
            }

            // ALWAYS
            if (this.spacekey.isDown) {
                this.spacedown = true;
            } else if (this.spacedown) {
                // SWITCH LEVELTYPE
                if (this.topdown) {
                    // getting current x/y position of sir walter
                    var x = Math.floor(this.sirwalter_boat.x / this.tiledata.tilewidth);
                    var y = Math.round(this.sirwalter_boat.y / this.tiledata.tileheight);

                    console.log("X: " + x + ", Y: " + y);

                    // check if there is land nearby
                    if (((this.leveldata.tiles[y][x]) >= this.level) || ((y > 0) && this.leveldata.tiles[y - 1][x] >= this.level) || ((y < (this.tiledata.tmapheight - 1)) && this.leveldata.tiles[y + 1][x] >= this.level)) {
                        this._activateLeftRight(x, y);
                        this.topdown = false;
                    }
                } else {
                    // this._activateTopDown();
                    this.topdown = true;
                }

                this.spacedown = false;
            }
        };

        Level1.prototype.render = function () {
        };

        Level1.prototype._activateLeftRight = function (x, y) {
            console.log('activating LEFTRIGHT');

            // HIDE TOPDOWN
            // SIR WALTER
            this.sirwalter_boat.body.velocity.y = 0;
            this.sirwalter_boat.body.velocity.x = 0;
            this.sounds.boatmotor.stop();
            this.add.tween(this.sirwalter_boat).to({ alpha: 0 }, 200, Phaser.Easing.Linear.None, true);

            this.add.tween(this.leveldata.groupTopDown_coll).to({ alpha: 0 }, 200, Phaser.Easing.Linear.None, true);
            this.add.tween(this.leveldata.groupTopDown).to({ alpha: 0 }, 200, Phaser.Easing.Linear.None, true);

            // LEFTRIGHT
            // bkgr
            this.add.tween(this.leftright.bkgr).to({ alpha: 1 }, 200, Phaser.Easing.Linear.None, true);

            this._createLevelLeftRight(x, y);
        };

        Level1.prototype._activateTopDown = function () {
            console.log('activating TOPDOWN');

            // OK: SIR WALTER
            this.add.tween(this.sirwalter_boat).to({ alpha: 1 }, 200, Phaser.Easing.Linear.None, true);

            // LEVEL
            this.add.tween(this.leveldata.groupTopDown_coll).to({ alpha: 1 }, 200, Phaser.Easing.Linear.None, true);
            this.add.tween(this.leveldata.groupTopDown).to({ alpha: 1 }, 200, Phaser.Easing.Linear.None, true);

            // LEFTRIGHT
            // bkgr
            this.add.tween(this.leftright.bkgr).to({ alpha: 0 }, 200, Phaser.Easing.Linear.None, true);

            this.leftright.group.destroy();
            this.sirwalter_walk.sprite.destroy();
        };
        return Level1;
    })(Phaser.State);
    BTSLD29.Level1 = Level1;
})(BTSLD29 || (BTSLD29 = {}));
//# sourceMappingURL=Level1.js.map
