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 __();
};
var Maze;
(function (_Maze) {
    var Maze = (function (_super) {
        __extends(Maze, _super);
        function Maze(game, name, x, y, json) {
            _super.call(this, game, name, x, y, json);
            this.koniec = false;
            this.fail = false;
            this.ready = false;
        }
        Maze.prototype.init = function (json) {
            _super.prototype.init.call(this, json);
            this.Elements = json.GameData;
        };
        Maze.prototype.preload = function () {
            _super.prototype.preload.call(this);
            this.game.load.onLoadComplete.addOnce(this.create, this);
            this.loadGraphics();
            this.game.load.start();
            this.map = this.Elements.Tilemap[0].layers[0].data;
            this.game.load.spritesheet('player', 'Graphics/Games/OB_G_II_3/OB_g_II_3_gracz.png', 29, 29, 4);
        };
        Maze.prototype.loadGraphics = function () {
            for (var i = 0; i < this.Elements.Graphics.length; i++)
                this.game.load.image(this.Elements.Graphics[i].key, this.Elements.Graphics[i].src);
            this.cursors = this.game.input.keyboard.createCursorKeys();
        };
        Maze.prototype.create = function () {
            if (this.json.isStandalone) {
                _super.prototype.create.call(this);
            }
            this.game.physics.startSystem(Phaser.Physics.ARCADE);
            this.wall = this.game.add.group();
            this.addChild(this.wall);
            this.woda = this.game.add.sprite(1093, 635, 'woda');
            this.woda.anchor.set(0.5, 1);
            this.woda.scale.y = 0.1;
            this.addChild(this.woda);
            this.game.time.events.loop(Phaser.Timer.SECOND, this.updateWater, this);
            this.kran = this.game.add.sprite(1060, 550, 'kran');
            this.addChild(this.kran);
            this.player = this.game.add.sprite(560, 64, 'player');
            this.player.animations.add('walk', [0, 1, 2, 3], 0, false);
            this.addChild(this.player);
            this.przewodnik = this.game.add.sprite(1150, 255, 'przewodnik');
            this.przewodnik.inputEnabled = true;
            this.przewodnik.events.onInputDown.add(function () {
                this.przewodnik.visible = false;
                if (this.koniec == false)
                    this.ready = true;
                if (this.koniec == true && this.fail == true)
                    this.resetStage();
            }, this);
            this.addChild(this.przewodnik);
            this.dymek = this.game.add.sprite(-700, -200, 'dymek');
            this.przewodnik.addChild(this.dymek);
            this.polecenie = this.game.add.text(150, 200, "Nie pozwól na marnowanie \nwody w Twojej firmie,\nzawsze zakręcaj kran.", { font: "32px Arial", fill: "#000000", wordWrap: true, wordWrapWidth: this.dymek.width, align: "center" });
            this.dymek.addChild(this.polecenie);
            this.game.physics.enable([this.player, this.kran], Phaser.Physics.ARCADE);
            var idx = 0;
            for (var i = 0; i < 21; i++)
                for (var j = 0; j < 21; j++) {
                    if (this.map[idx++] == 1)
                        var spr = this.game.add.sprite(500 + (j * 32), (i * 32), 'blok');
                    this.game.physics.enable(spr, Phaser.Physics.ARCADE);
                    this.addChild(spr);
                    this.wall.add(spr);
                }
            this.wall.setAll('body.immovable', true);
        };
        Maze.prototype.resetStage = function () {
            this.player.x = 560;
            this.player.y = 64;
            this.woda.scale.y = 0.1;
            this.koniec = false;
            this.fail = false;
        };
        Maze.prototype.updateWater = function () {
            if (this.woda.scale.y < 1 && this.koniec == false && this.ready)
                this.woda.scale.y = this.woda.scale.y + 0.02;
        };
        Maze.prototype.update = function () {
            if (this.ready) {
                this.player.body.velocity.x = 0;
                this.player.body.velocity.y = 0;
                if (this.woda.scale.y > 0.9) {
                    this.polecenie.setText("Pamiętaj!\nOchrona środowiska\nwymaga wysiłku.\nSpróbuj jeszcze raz.");
                    this.przewodnik.visible = true;
                    this.koniec = true;
                    this.fail = true;
                    EU.Log("Czas się skończył.");
                }
                if (this.game.physics.arcade.collide(this.player, this.kran)) {
                    this.player.body.moves = false;
                    this.koniec = true;
                    this.ready = false;
                    if (this.woda.scale.y < 1) {
                        this.polecenie.setText("Widzę, że ochrona środowiska\nnie jest Ci obca.");
                        this.przewodnik.visible = true;
                        this.fail = false;
                        EU.setGameCompletion(this.parent, 1);
                        EU.Log("Gra ukończona z sukcesem.");
                    }
                }
                this.game.physics.arcade.collide(this.player, this.wall);
                this.player.body.velocity.x = 0;
                this.player.body.velocity.y = 0;
                if (this.cursors.left.isDown) {
                    this.player.body.velocity.x = -200;
                    if (this.player.body.moves != false)
                        this.player.animations.frame = 3;
                }
                if (this.cursors.right.isDown) {
                    this.player.body.velocity.x = 200;
                    if (this.player.body.moves != false)
                        this.player.animations.frame = 2;
                }
                if (this.cursors.up.isDown) {
                    this.player.body.velocity.y = -200;
                    if (this.player.body.moves != false)
                        this.player.animations.frame = 1;
                }
                if (this.cursors.down.isDown) {
                    this.player.body.velocity.y = 200;
                    if (this.player.body.moves != false)
                        this.player.animations.frame = 0;
                }
            }
        };
        return Maze;
    })(Base.BasicGroup);
    _Maze.Maze = Maze;
})(Maze || (Maze = {}));
//# sourceMappingURL=Maze.js.map