System.register(["../input/Pad", "../entities/Player", "../entities/Robe", "../manager/RitualManager", "../manager/Sequence"], function(exports_1) {
    var __extends = (this && this.__extends) || function (d, b) {
        for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
    var Pad_1, Player_1, Robe_1, RitualManager_1, Sequence_1;
    var Graphics, Ritual;
    return {
        setters:[
            function (Pad_1_1) {
                Pad_1 = Pad_1_1;
            },
            function (Player_1_1) {
                Player_1 = Player_1_1;
            },
            function (Robe_1_1) {
                Robe_1 = Robe_1_1;
            },
            function (RitualManager_1_1) {
                RitualManager_1 = RitualManager_1_1;
            },
            function (Sequence_1_1) {
                Sequence_1 = Sequence_1_1;
            }],
        execute: function() {
            Ritual = (function (_super) {
                __extends(Ritual, _super);
                function Ritual() {
                    _super.apply(this, arguments);
                    this.npcs = [];
                    this.canCheck = false;
                }
                Ritual.prototype.preload = function () {
                    this.game.load.atlasJSONHash('player', 'assets/animations/player_robe/player_robe.png', 'assets/animations/player_robe/player_robe.json');
                    this.game.load.atlasJSONHash('robe', 'assets/animations/robe/robe.png', 'assets/animations/robe/robe.json');
                    this.game.load.image('ritual_bg', 'assets/images/ritual_bg.png');
                    this.game.load.audio('ritual_music', 'assets/music/ritual.ogg');
                    this.game.load.image('flame1', 'assets/particles/flame1.png');
                    this.game.load.image('dialog_1', 'assets/dialogs/scene_3_0.png');
                    this.game.load.image('dialog_2', 'assets/dialogs/scene_3_1.png');
                    this.game.load.image('princess_small', 'assets/images/princess_small.png');
                };
                Ritual.prototype.create = function () {
                    var _this = this;
                    this.game.stage.backgroundColor = '#fff';
                    this.game.add.sprite(0, 0, 'ritual_bg');
                    this.music = this.game.add.audio('ritual_music');
                    this.music.play();
                    var flame = this.game.add.image(this.game.world.centerX, this.game.world.centerY, 'flame1');
                    flame.visible = false;
                    flame.anchor.setTo(0.5, 1);
                    this.game.input.gamepad.start();
                    this.pad = new Pad_1.Pad(this.game.input.gamepad.pad1);
                    this.ritualManager = new RitualManager_1.RitualManager(this.game);
                    var playerRobeLook = this.game.add.sprite(0, 180, 'player', '05.png');
                    this.player = new Player_1.Player(playerRobeLook, this.pad);
                    this.player.setupPlayerRobeAnimations(playerRobeLook);
                    this.player.speed = 3;
                    var npcs = [];
                    for (var i = 0; i < 5; i++) {
                        npcs.push(new Robe_1.Robe(this.game.add.sprite(0, i * 80, 'robe', '01.png')));
                        npcs[i].sprite.anchor.setTo(0.5, 1);
                        this.game.add.tween(npcs[i].sprite.position)
                            .to({
                            x: this.ritualManager.circle.fields[i].x + this.game.world.centerX,
                            y: this.ritualManager.circle.fields[i].y + this.game.world.centerY
                        }, 4000, Phaser.Easing.Linear.None, true);
                    }
                    var blinkFire = function () {
                        flame.visible = true;
                        _this.game.time.events.add(50, function () {
                            flame.visible = false;
                            _this.game.time.events.add(100, function () {
                                flame.visible = true;
                                _this.game.time.events.add(200, function () {
                                    flame.visible = false;
                                });
                            });
                        });
                    };
                    var walkToMidAndBack = function (delay, toMid, back) {
                        var def = $.Deferred();
                        seq.chain(delay, function () {
                            var previousRadius = _this.ritualManager.circle.radius;
                            var walkCenter = _this.game.add.tween(_this.ritualManager.circle).to({ radius: 30 }, toMid, Phaser.Easing.Quadratic.Out, false);
                            var walkBackOut = _this.game.add.tween(_this.ritualManager.circle).to({ radius: previousRadius }, back, Phaser.Easing.Quadratic.Out, false);
                            walkCenter.chain(walkBackOut).onComplete.add(function () {
                                def.resolve(walkCenter);
                            });
                            walkCenter.start();
                        });
                        return def;
                    };
                    var princess_small = this.game.add.image(0, 0, 'princess_small');
                    princess_small.position.setTo(this.game.world.centerX, this.game.world.centerY);
                    princess_small.anchor.setTo(0.5, 0.5);
                    princess_small.scale.setTo(1.2, 1.2);
                    princess_small.position.y -= 80;
                    princess_small.visible = false;
                    this.dialog1 = this.game.add.sprite(0, this.game.world.height, 'dialog_1');
                    this.dialog1.anchor.setTo(0, 1);
                    this.dialog2 = this.game.add.sprite(0, this.game.world.height, 'dialog_2');
                    this.dialog2.anchor.setTo(0, 1);
                    this.dialog2.visible = false;
                    var seq = new Sequence_1.Sequence(this.game);
                    seq.chain(4000, function () {
                        _this.dialog1.visible = false;
                    }).resolve();
                    seq.chain(7000, function () {
                        _this.ritualManager.rotate();
                        _this.canCheck = true;
                        _this.npcs = npcs;
                    }).then(function () {
                        return seq.chain(8000, function () {
                            _this.ritualManager.stopRotation();
                        });
                    }).then(function () {
                        return seq.chain(500, function () {
                            _this.ritualManager.rotate(false);
                        });
                    }).then(function () {
                        return seq.chain(3500, function () {
                            _this.ritualManager.setRotation(0.7);
                        });
                    }).then(function () {
                        return seq.chain(4500, function () {
                            _this.ritualManager.setRotation(-0.45);
                        });
                    }).then(function () {
                        return walkToMidAndBack(1000, 2000, 2000);
                    }).then(function () {
                        return seq.chain(8000, function () {
                            blinkFire();
                        }).resolve();
                    }).then(function () {
                        return seq.chain(4000, function () {
                            flame.visible = true;
                        }).resolve();
                    }).then(function () {
                        return seq.chain(21000, function () {
                            var previousRadius = _this.ritualManager.circle.radius;
                            var walkCenter = _this.game.add.tween(_this.ritualManager.circle).to({ radius: 30 }, 1000, Phaser.Easing.Quadratic.In, false);
                            var walkBackOut = _this.game.add.tween(_this.ritualManager.circle).to({ radius: previousRadius }, 1000, Phaser.Easing.Quadratic.Out, false);
                            walkCenter.chain(walkBackOut);
                            walkCenter.start();
                        });
                    }).then(function () {
                        return seq.chain(2000, function () {
                            _this.ritualManager.setRotation(-0.6);
                        });
                    }).then(function () {
                        return seq.chain(1000, function () {
                            _this.ritualManager.setRotation(0.6);
                        });
                    }).then(function () {
                        return seq.chain(1000, function () {
                            _this.ritualManager.stopRotation();
                        });
                    }).then(function () {
                        return seq.chain(1000, function () {
                            blinkFire();
                        });
                    }).then(function () {
                        return seq.chain(300, function () {
                            _this.ritualManager.setRotation(-0.6);
                        });
                    }).then(function () {
                        var def = $.Deferred();
                        seq.chain(3000, function () {
                            var previousRadius = _this.ritualManager.circle.radius;
                            var walkOut = _this.game.add.tween(_this.ritualManager.circle).to({ radius: previousRadius + 100 }, 1000, Phaser.Easing.Quadratic.In, false);
                            var walkBackRing = _this.game.add.tween(_this.ritualManager.circle).to({ radius: previousRadius }, 1000, Phaser.Easing.Quadratic.Out, false);
                            walkOut.chain(walkBackRing).onComplete.add(function () {
                                def.resolve(walkOut);
                            });
                            walkOut.start();
                        });
                        return def;
                    }).then(function () {
                        return walkToMidAndBack(4000, 2000, 2000);
                    }).then(function () {
                        return seq.chain(500, function () {
                            _this.ritualManager.setRotation(0.8);
                        });
                    }).then(function () {
                        return seq.chain(500, function () {
                            _this.ritualManager.setRotation(-0.8);
                        });
                    }).then(function () {
                        return seq.chain(1000, function () {
                            _this.ritualManager.stopRotation();
                        });
                    }).then(function () {
                        return seq.chain(1000, function () {
                            _this.ritualManager.setRotation(0.6);
                        });
                    }).then(function () {
                        return seq.chain(5000, function () {
                            _this.canCheck = false;
                            _this.ritualManager.checkIfWon();
                            _this.ritualManager.stopRotation();
                            flame.visible = true;
                            _this.npcs.forEach(function (npc) {
                                npc.animDown();
                                npc.sprite.animations.stop();
                                _this.player.enabled = false;
                            });
                        });
                    }).then(function () {
                        return seq.chain(2000, function () {
                            princess_small.visible = true;
                        });
                    }).then(function () {
                        return seq.chain(6000, function () {
                            _this.dialog2.visible = true;
                        });
                    }).then(function () {
                        return seq.chain(5000, function () {
                            _this.game.state.start('end');
                        });
                    });
                };
                Ritual.prototype.shutdown = function () {
                    this.music.stop();
                };
                Ritual.prototype.update = function () {
                    this.pad.update();
                    this.ritualManager.update();
                    if (this.canCheck) {
                        this.ritualManager.check(this.player);
                    }
                    for (var i = 0; i < this.npcs.length; i++) {
                        var bla = this.npcs[i];
                        bla.animRight();
                        bla.sprite.x = this.ritualManager.circle.fields[i].x + this.game.world.centerX;
                        bla.sprite.y = this.ritualManager.circle.fields[i].y + this.game.world.centerY;
                    }
                };
                return Ritual;
            })(Phaser.State);
            exports_1("Ritual", Ritual);
        }
    }
});
//# sourceMappingURL=Ritual.js.map