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, 3, 4, 5, 6];
        }
        MainGameState.prototype.preload = function () {
            _super.prototype.preload.call(this);
            for (var i = 0; i < 6; 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");
                this.game.load.image(this.getOutfit(i, false) + "_01_g", "Graphics/game/girl/" + this.getOutfit(i, false) + "_01.png");
                this.game.load.image(this.getOutfit(i, false) + "_02_g", "Graphics/game/girl/" + this.getOutfit(i, false) + "_02.png");
                this.game.load.image(this.getOutfit(i, false) + "_03_g", "Graphics/game/girl/" + 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.image("girl", "Graphics/game/girl.png");
            this.game.load.spritesheet("help", "Graphics/gui/help.png", 104, 108, 2, 0, 8);
            this.game.load.spritesheet("replay", "Graphics/gui/replay.png", 108, 111, 2, 0, 0);
            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/goal.mp3");
            this.applause = new Audio("./Sounds/applause.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);
            // old ->> this.outfitNum = Math.floor(Math.random() * 6) + 1;
            this.shuffle(this.tableOutfits);
            this.outfitNum = this.tableOutfits[0];
            this.boy = { "cl": -1, "parts": [] };
            this.girl = { "cl": -1, "parts": [] };
            this.game.add.image(432, 17, "boy");
            this.game.add.image(1087, 17, "girl");
            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 strój " + this.getOutfit(this.outfitNum, true), { font: "normal 30pt 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);
			this.tipsBtn = this.game.add.button(405, 1000, "tips", this.showOutfits, this, 0, 0, 1, 0);
            this.game.add.button(45, 22, "replay", this.replay, this, 0, 0, 1, 0);
            var arX = 339;
            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 + 520, arY + (62 + 198) * i, "next" + (i + 1), function (e) {
                    this.changeOutfit(e, this.boy, "_b", 1);
                }, this, 0, 0, 1, 0);
            }
            arX = 1029;
            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.girl, "_g", -1);
                }, this, 0, 0, 1, 0);
                this.game.add.button(arX + 520, arY + (62 + 198) * i, "next" + (i + 1), function (e) {
                    this.changeOutfit(e, this.girl, "_g", 1);
                }, this, 0, 0, 1, 0);
            }
            var hat = this.game.add.image(432, 17, "kaszubski_01_b");
            hat.alpha = 0;
            var leggins = this.game.add.image(432, 579, "kaszubski_03_b");
            leggins.alpha = 0;
            var torso = this.game.add.image(432, 383, "kaszubski_02_b");
            torso.alpha = 0;
            this.boy.parts.push(hat);
            this.boy.parts.push(torso);
            this.boy.parts.push(leggins);
            var hat = this.game.add.image(1087, 17, "kaszubski_01_g");
            hat.alpha = 0;
            var leggins = this.game.add.image(1087, 579, "kaszubski_03_g");
            leggins.alpha = 0;
            var torso = this.game.add.image(1087, 428, "kaszubski_02_g");
            torso.alpha = 0;
            this.girl.parts.push(hat);
            this.girl.parts.push(torso);
            this.girl.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, "Ubierz postacie w stroje ludowe z poszczególnych regionów Polski. Dobierz górę, dół i nakrycie głowy.", { 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.createOutfitHelp();
            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 > 5) {
                    obj.cl = 0;
                }
                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 < 0) {
                    obj.cl = 5;
                }
                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);
            for (var i = 0; i < this.boy.parts.length; i++) {
                if (i == 2 && (correct === "kaszubski" || correct === "kurpiowski")) {
                    if ((this.boy.parts[i].key.split("_")[0] !== "kurpiowski" && this.boy.parts[i].key.split("_")[0] !== "kaszubski") || this.boy.parts[i].alpha === 0) {
                        win = false;
                    }
                }
                else {
                    if (this.boy.parts[i].key.split("_")[0] !== correct || this.boy.parts[i].alpha === 0) {
                        win = false;
                    }
                }
            }
            for (var i = 0; i < this.girl.parts.length; i++) {
                if (this.girl.parts[i].key.split("_")[0] !== correct || this.girl.parts[i].alpha === 0) {
                    win = false;
                }
            }
            if (win) {
                this.showWin();
            }
            else{
                this.showTryAgain();
            }
        };
        MainGameState.prototype.getOutfit = function (num, pol) {
            var ret = "kaszubski";
            switch (num) {
                case 1:
                    ret = "krakowski";
                    break;
                case 2:
                    ret = "kujawski";
                    break;
                case 3:
                    ret = "kurpiowski";
                    break;
                case 4:
                    ret = "lowicki";
                    if (pol) {
                        ret = "łowicki";
                    }
                    break;
                case 5:
                    ret = "podhalanski";
                    if (pol) {
                        ret = "podhalański";
                    }
                    break;
            }
            return ret;
        };
        MainGameState.prototype.showHelp = function () {
            var tw = null;
            this.cloudText.setText("Ubierz postacie w stroje ludowe z poszczególnych regionów Polski. Dobierz górę, dół i nakrycie głowy.");
            var point = this.game.world.height;
            this.isHelpShown = !this.isHelpShown;
            if (this.isHelpShown) {
                point -= this.guide.height;
                try {
                    this.instruction.currentTime = 0;
                    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();
                    this.instruction.currentTime = 0;
                }
                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, 3, 4, 5, 6];
            this.ggCount++;
            var tw = this.game.add.tween(this.guide);
            this.cloudText.setText("GRATULACJE!");
            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.applause.currentTime = 0;
                        this.reload();
                    }
                    catch (e) {
                    }
                }, this);
            }, this);
            tw.start();
            try {
                this.applause.currentTime = 0;
                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");
            this.halp.visible = false;
            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 g = this.game.add.image(j * x + 342.5, i * y + 20, "girl");
                    var hat = this.game.add.image(0, 0, this.getOutfit(temp, false) + "_01_g");
                    var torso = this.game.add.image(0, 410, this.getOutfit(temp, false) + "_02_g");
                    var leggins = this.game.add.image(0, 562, this.getOutfit(temp, false) + "_03_g");
                    g.addChild(hat);
                    g.addChild(leggins);
                    g.addChild(torso);
                    g.scale.setTo(0.5, 0.5);
                    this.oGroup.addChild(g);
                    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.outfitNum = 0;
            this.firstRun = true;
            this.boy = { "cl": -1, "parts": [] };
            this.girl = { "cl": -1, "parts": [] };
            this.game.state.start("MainGameState", false, false);
        };
        MainGameState.prototype.replay = function () {
            //this.outfitNum = 0;
            this.firstRun = true;
            this.boy = { "cl": -1, "parts": [] };
            this.girl = { "cl": -1, "parts": [] };
            this.ggCount = 0;
            //zmienan a przejscie do glowenj strony
            this.game.state.start("TitleScreenState");
        };
        return MainGameState;
    })(BasicModule.BasicState);
    MainGame.MainGameState = MainGameState;
})(MainGame || (MainGame = {}));
//# sourceMappingURL=MainGameState.js.map