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 MainGame;
(function (MainGame) {
    var MainGameState = (function (_super) {
        __extends(MainGameState, _super);
        function MainGameState() {
            _super.call(this);
            this.isHelpShown = false;
            this.firstRun = true;
            this.isSafari = true;
            this.ggCount = 0;
            this.tableOutfits = [1, 2];
        }
        MainGameState.prototype.preload = function () {
            _super.prototype.preload.call(this);
            for (var i = 0; i < 2; i++) {
                this.game.load.image(this.getOutfit(i, false) + "_01_b", "Graphics/game/boy/" + this.getOutfit(i, false) + "_01.png");
                this.game.load.image(this.getOutfit(i, false) + "_02_b", "Graphics/game/boy/" + this.getOutfit(i, false) + "_02.png");
                this.game.load.image(this.getOutfit(i, false) + "_03_b", "Graphics/game/boy/" + this.getOutfit(i, false) + "_03.png");
            }
            for (var i = 0; i < 3; i++) {
                this.game.load.spritesheet("previous" + (i + 1), "Graphics/gui/" + "previous" + (i + 1) + ".png", 53, 62, 2, 0, 0);
                this.game.load.spritesheet("next" + (i + 1), "Graphics/gui/" + "next" + (i + 1) + ".png", 53, 62, 2, 0, 0);
            }
            this.game.load.image("frame", "Graphics/gui/frame.png");
            this.game.load.spritesheet("ready", "Graphics/gui/ready.png", 220, 71, 2, 0, 0);
            this.game.load.image("boy", "Graphics/game/boy.png");
            this.game.load.spritesheet("help", "Graphics/gui/help.png", 104, 108, 2, 0, 8);
            this.game.load.image("gameGuide", "Graphics/gui/guide.png");
            this.game.load.image("cloud", "Graphics/gui/instr_bg.png");
            this.game.load.spritesheet("tips", "Graphics/gui/tips.png", 220, 71, 2);
            for (var i = 0; i < 3; i++) {
                this.game.load.spritesheet("next" + (i + 1), "Graphics/gui/previous" + (i + 1) + ".png", 53, 62, 2, 0, 0);
                this.game.load.spritesheet("previous" + (i + 1), "Graphics/gui/next" + (i + 1) + ".png", 53, 62, 2, 0, 0);
            }
            this.instruction = new Audio("./Sounds/ZIP_g_I_6_2.mp3");
            this.applause = new Audio("./Sounds/ZIP_g_I_6_3.mp3");
            this.sndTryAgain = new Audio("./Sounds/tryagain.mp3");
        };
        MainGameState.prototype.shuffle = function (o) {
            for (var j, x, i = o.length; i; j = Math.floor(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x)
                ;
            return o;
        };
        MainGameState.prototype.create = function () {
            _super.prototype.create.call(this);
            this.outfitNum = this.tableOutfits[0];
            this.boy = { "cl": -1, "parts": [] };
            this.game.add.image(432 + 300, 17, "boy");
            var fr = this.game.add.image((351), 0, "frame");
            fr.y = this.game.height - fr.height;
            var txt = this.game.add.text(0, 0, "Wybierz ubrania właściwe na deszcz i zimno.", { font: "normal 24pt Arial" });
            txt.anchor.set(0.5, 0.5);
            txt.position.setTo(fr.width / 2, fr.height * 0.6);
            fr.addChild(txt);
            this.helpBtn = this.game.add.button(1778, 22, "help", this.showHelp, this, 0, 0, 1, 0);
            var arX = (339 + 300);
            var arY = 212;
            for (var i = 0; i < 3; i++) {
                this.game.add.button(arX, arY + (62 + 198) * i, "previous" + (i + 1), function (e) {
                    this.changeOutfit(e, this.boy, "_b", -1);
                }, this, 0, 0, 1, 0);
                this.game.add.button(arX + 560, arY + (62 + 198) * i, "next" + (i + 1), function (e) {
                    this.changeOutfit(e, this.boy, "_b", 1);
                }, this, 0, 0, 1, 0);
            }
            var hat = this.game.add.image(432 + 300, 17, "1_01_b");
            hat.alpha = 0;
            var leggins = this.game.add.image(432 + 300, 719, "1_03_b");
            leggins.alpha = 0;
            var torso = this.game.add.image(433 + 300, 383, "1_02_b");
            torso.alpha = 0;
            this.boy.parts.push(hat);
            this.boy.parts.push(torso);
            this.boy.parts.push(leggins);
            var ready = this.game.add.button(971, 24, "ready", this.isDone, this, 0, 0, 1, 0);
            fr.addChild(ready);
            this.guide = this.game.add.group();
            var gd = this.game.add.image(this.game.width * 0.7, 0, "gameGuide");
            this.cloud = this.game.add.image(this.game.width * 0.5, 0, "cloud");
            gd.y = this.cloud.height * 0.8;
            this.cloudText = this.game.add.text(0, 0, "Wybierz ubrania właściwe na deszcz i zimno.", { wordWrap: true, wordWrapWidth: this.cloud.width * 0.75 });
            this.cloudText.anchor.set(0.5, 0.5);
            this.cloudText.position.setTo(this.cloud.width * 0.5, this.cloud.height * 0.47);
            this.cloud.addChild(this.cloudText);
            this.guide.addChild(this.cloud);
            this.guide.addChild(gd);
            this.guide.y = this.game.world.height;
            if (this.ggCount == 0)
                this.showHelp();
        };
        MainGameState.prototype.changeOutfit = function (spr, obj, sfx, idx) {
            var temp = spr.key.substring(0, spr.key.length - 1);
            var piece = parseInt(spr.key[spr.key.length - 1]);
            obj.cl += idx;
            if (temp === "next") {
                if (obj.cl > 2) {
                    obj.cl = -1;
                }
                if (obj.cl === -1) {
                    obj.parts[piece - 1].alpha = 0;
                }
                else {
                    obj.parts[piece - 1].loadTexture(this.getOutfit(obj.cl, false) + "_0" + piece + "" + sfx, 0);
                    obj.parts[piece - 1].alpha = 1;
                }
            }
            else {
                if (obj.cl < -1) {
                    obj.cl = 2;
                }
                if (obj.cl === -1) {
                    obj.parts[piece - 1].alpha = 0;
                }
                else {
                    obj.parts[piece - 1].loadTexture(this.getOutfit(obj.cl, false) + "_0" + piece + "" + sfx, 0);
                    obj.parts[piece - 1].alpha = 1;
                }
            }
        };
        MainGameState.prototype.isDone = function () {
            var win = true;
            var correct = this.getOutfit(this.outfitNum, false);
            console.log(correct);
            for (var i = 0; i < this.boy.parts.length; i++) {
                console.log("stroj: " + this.boy.parts[i].key.split("_")[0]);
                if (this.boy.parts[i].key.split("_")[0] != correct || this.boy.parts[i].alpha === 0) {
                    win = false;
                }
            }
            if (win) {
                this.showWin();
            }
            else {
                this.showTryAgain();
            }
        };
        MainGameState.prototype.getOutfit = function (num, pol) {
            var ret = "1";
            switch (num) {
                case 1:
                    ret = "2";
                    break;
            }
            return ret;
        };
        MainGameState.prototype.showHelp = function () {
            var tw = null;
            this.cloudText.setText("Wybierz ubrania właściwe na deszcz i zimno.");
            var point = this.game.world.height;
            this.isHelpShown = !this.isHelpShown;
            if (this.isHelpShown) {
                point -= this.guide.height;
                try {
                    this.instruction.play();
                    if (this.firstRun) {
                        this.helpBtn.inputEnabled = false;
                        var tmr = this.game.time.events.add(Phaser.Timer.SECOND * 8, this.changeHelp, this);
                    }
                }
                catch (e) {
                }
            }
            else {
                try {
                    this.instruction.pause();
                }
                catch (e) {
                }
            }
            tw = this.game.add.tween(this.guide);
            tw.to({ y: point }, 800, Phaser.Easing.Cubic.Out);
            tw.start();
        };
        MainGameState.prototype.showWin = function () {
            this.tableOutfits.splice(0, 1);
            if (this.tableOutfits.length == 0)
                this.tableOutfits = [1, 2];
            this.ggCount++;
            var tw = this.game.add.tween(this.guide);
            this.cloudText.setText("Teraz możesz wejść do budynku telewizji.");
            if (this.halp != undefined)
                this.halp.visible = false;
            tw.to({ y: this.game.world.height - this.guide.height }, 800, Phaser.Easing.Cubic.Out);
            tw.onComplete.add(function () {
                this.game.time.events.add(Phaser.Timer.SECOND * 4, function () {
                    try {
                        this.applause.pause();
                        this.game.state.start("IntermissionState", false, false);
                    }
                    catch (e) {
                    }
                }, this);
            }, this);
            tw.start();
            try {
                this.applause.play();
            }
            catch (e) {
            }
        };
        MainGameState.prototype.showTryAgain = function () {
            var tw = this.game.add.tween(this.guide);
            var tw2 = this.game.add.tween(this.guide);
            this.cloudText.setText("SPRÓBUJ JESZCZE RAZ");
            if (this.halp != undefined)
                this.halp.visible = true;
            try {
                this.sndTryAgain.currentTime = 0;
                this.sndTryAgain.play();
            }
            catch (e) {
            }
            tw2.to({ y: this.game.world.height }, 800, Phaser.Easing.Cubic.Out);
            tw.to({ y: this.game.world.height - this.guide.height }, 800, Phaser.Easing.Cubic.Out);
            tw.onComplete.add(function () {
                this.game.time.events.add(Phaser.Timer.SECOND * 2, function () {
                    tw2.start();
                }, this);
            }, this);
            tw.start();
        };
        MainGameState.prototype.changeHelp = function () {
            this.showHelp();
            this.helpBtn.inputEnabled = true;
            this.cloudText.y = this.cloud.height * 0.4;
        };
        MainGameState.prototype.showOutfits = function () {
            this.oGroup.visible = true;
        };
        MainGameState.prototype.hideOutfits = function () {
            this.oGroup.visible = false;
        };
        MainGameState.prototype.createOutfitHelp = function () {
            this.oGroup = this.game.add.group();
            this.oGroup.z = 20;
            var back;
            var bmd = this.game.add.bitmapData(1920, 1080);
            bmd.ctx.rect(0, 0, 1920, 1080);
            bmd.ctx.fillStyle = '#000000';
            bmd.ctx.fill();
            back = this.game.add.sprite(this.game.world.centerX, this.game.world.centerY, bmd);
            back.anchor.setTo(0.5, 0.5);
            back.alpha = 0.7;
            back.inputEnabled = true;
            this.oGroup.addChild(back);
            var x = 600;
            var y = 500;
            var temp = 0;
            for (var i = 0; i < 2; i++) {
                for (var j = 0; j < 3; j++) {
                    var b = this.game.add.image(j * x + 142.5, i * y + 20, "boy");
                    var hat = this.game.add.image(0, 0, this.getOutfit(temp, false) + "_01_b");
                    var torso = this.game.add.image(0, 366, this.getOutfit(temp, false) + "_02_b");
                    var leggins = this.game.add.image(0, 562, this.getOutfit(temp, false) + "_03_b");
                    b.addChild(hat);
                    b.addChild(leggins);
                    b.addChild(torso);
                    b.scale.setTo(0.5, 0.5);
                    this.oGroup.addChild(b);
                    var txt = this.game.add.text(0, b.y + b.height + 2, this.getOutfit(temp, true), { font: "normal 40pt Arial", fill: "white", stroke: "black", strokeThickness: 5 });
                    txt.x = b.x + ((435 - txt.width) / 2);
                    this.oGroup.addChild(txt);
                    temp++;
                }
            }
            this.halp = this.game.add.button(1778, 22, "help", this.hideOutfits, this, 0, 0, 1, 0, this.oGroup);
            this.oGroup.visible = false;
        };
        MainGameState.prototype.reload = function () {
            this.firstRun = true;
            this.boy = { "cl": -1, "parts": [] };
            this.game.state.start("MainGameState", false, false);
        };
        MainGameState.prototype.replay = function () {
            this.firstRun = true;
            this.boy = { "cl": -1, "parts": [] };
            this.ggCount = 0;
            this.game.state.start("TitleScreenState");
        };
        return MainGameState;
    })(BasicModule.BasicState);
    MainGame.MainGameState = MainGameState;
})(MainGame || (MainGame = {}));
//# sourceMappingURL=MainGameState.js.map