var __reflect = (this && this.__reflect) || function (p, c, t) {
    p.__class__ = c, t ? t.push(c) : t = [c], p.__types__ = p.__types__ ? t.concat(p.__types__) : t;
};
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 roleDefine = ["roshan", "aziraphale", "devil", "e10sprite", "fey", "redskull", "sithjester"];
var sceneDefine = ["scene1_png", "scene2_png", "scene3_png", "scene4_png", "scene5_png", "scene6_png", "scene7_png", "scene8_png"];
var letterDefine = [1, 2, 3, 4, 5, 6, 7];
var codeSegmentDefine = [9, 10, 11, 12, 13, 14, 15];
var goodsDefine = [1, 2, 3, 4, 5, 6, 7];
var CODE_ID = 8;
var SHIELD_ID = 16;
var shieldID_rate = 0.8;
var userGameData;
var GameLevel = (function (_super) {
    __extends(GameLevel, _super);
    function GameLevel() {
        var _this = _super.call(this) || this;
        _this.gap = 8;
        _this._goods = [];
        _this.endX = 25;
        _this.endY = 50;
        _this.roles = [];
        _this.rolePositions = [];
        _this.walkCount = 0;
        return _this;
    }
    GameLevel.prototype.initGoods = function () {
        // for (let i:number = 0; i < 15; i++) {
        // 	let goods:Goods = new Goods(i % 7 + 1, i + 1);
        // 	this.roleContainer.addChild(goods);
        // 	goods.x = Math.random() * (this.stage.width  - 100) + 50;
        // 	goods.y = Math.random() * (this.stage.height - 360) + 50;
        // }
        for (var i = 0, len = this._sceneData.goods.length; i < len; i++) {
            var goodObj = this._sceneData.goods[i];
            var goods = new Goods(goodObj.id, goodObj.itemID);
            goods.x = goodObj.x;
            // goods.x = Math.random() * (this.stage.width  - 100) + 50
            goods.y = goodObj.y;
            this._goods.push(goods);
            // goods.y = Math.random() * (this.stage.height - 360) + 50;
            this.roleContainer.addChild(goods);
        }
    };
    GameLevel.prototype.init = function () {
        // this.roleContainer = new egret.DisplayObjectContainer();
        // this.addChild(this.roleContainer);
        this.initGameData();
        this.initSceneData();
        this.initScene();
        this.initGoods();
        this.initRole();
        // this.btnSuicide = AssetManager.createBitmapByName("suicide_png");
        // this.btnSuicide.scaleX = this.btnSuicide.scaleY = 1;
        // this.btnSuicide.x = 50;
        // this.btnSuicide.y = 50;
        // this.addChild(this.btnSuicide);
        // this.btnSuicide.touchEnabled = true;
        // this.btnSuicide.addEventListener(egret.TouchEvent.TOUCH_END, this.onTouchSuicide, this);
        // this.touchEnabled = true;
        // this.addEventListener(egret.TouchEvent.TOUCH_END, this.onTouch, this);	
        this.initListener();
        packageList.visible = true;
    };
    GameLevel.prototype.onTouchSuicide = function () {
        // suicideConfim.setContent("你确定要自杀么？");
        var self = this;
        talk.setTalk("自杀", function () {
            self.suicide();
        });
    };
    GameLevel.prototype.clearScene = function () {
        for (var i = 0, len = this._goods.length; i < len; i++) {
            var g = this._goods[i];
            if (g && g.parent) {
                g.parent.removeChild(g);
                g.dispose();
            }
        }
    };
    GameLevel.prototype.suicide = function () {
        userGameData.suicideTime++;
        Game.instance.save();
        console.log("suicideTime  " + userGameData.suicideTime);
        if (userGameData.roles && userGameData.roles.length >= 7) {
            if (userGameData.package.indexOf(SHIELD_ID) != -1) {
                this.next("gameEnd2");
            }
            else {
                this.next("gameEnd1");
            }
        }
        else {
            this.initSceneData();
            this.clearScene();
            this.initScene();
            this.roleContainer.removeChildren();
            this.initGoods();
            this.resetRole();
        }
    };
    GameLevel.prototype.initGameData = function () {
        if (userGameData == null) {
            userGameData = {};
            var roleIndex = Game.instance.random.Int(roleDefine.length);
            userGameData.mainRole = roleDefine[roleIndex];
            userGameData.package = [];
        }
    };
    GameLevel.prototype.initSceneData = function () {
        this._sceneData = {};
        var bgIndex = Game.instance.random.Int(sceneDefine.length);
        this._sceneData.bg = sceneDefine[bgIndex];
        var letterIndex;
        var letterid;
        var i = 0;
        do {
            letterIndex = Game.instance.random.Int(letterDefine.length);
            letterid = letterDefine[letterIndex];
            this._sceneData.letterID = letterid;
            i++;
        } while (userGameData.package.indexOf(letterid) != -1 && i < 3);
        var codeSIndex;
        var codeSid;
        i = 0;
        do {
            codeSIndex = Game.instance.random.Int(codeSegmentDefine.length);
            codeSid = codeSegmentDefine[codeSIndex];
            this._sceneData.codeSegmentID = codeSid;
            i++;
        } while (userGameData.package.indexOf(codeSid) != -1 && i < 3);
        if (userGameData.package.indexOf(CODE_ID) == -1) {
            this._sceneData.codeID = CODE_ID;
        }
        if (userGameData.package.indexOf(SHIELD_ID) == -1) {
            this._sceneData.shieldID = Game.instance.random.Next() > shieldID_rate ? SHIELD_ID : -1;
        }
        var goodsNum = Game.instance.random.Int(4, 7);
        this._sceneData.goods = [];
        for (var i_1 = 0; i_1 < goodsNum; i_1++) {
            var goodsObj = {};
            var goodIndex = Game.instance.random.Int(goodsDefine.length);
            goodsObj.id = goodsDefine[goodIndex];
            var a = Game.instance.random.Next();
            goodsObj.x = a * (this.stage.stageWidth - 100) + 50;
            a = Game.instance.random.Next();
            goodsObj.y = a * (this.stage.stageHeight - 360) + 50;
            goodsObj.itemID = -1;
            this._sceneData.goods.push(goodsObj);
        }
        this._sceneData.goods[0].itemID = this._sceneData.letterID;
        this._sceneData.goods[1].itemID = this._sceneData.codeSegmentID;
        if (this._sceneData.codeID != null) {
            this._sceneData.goods[2].itemID = this._sceneData.codeID;
        }
        if (this._sceneData.shieldID == SHIELD_ID) {
            if (this._sceneData.goods[3]) {
                this._sceneData.goods[3].itemID = this._sceneData.shieldID;
            }
        }
    };
    GameLevel.prototype.initScene = function () {
        this.bg = AssetManager.createBitmapByName(this._sceneData.bg);
        this.bg.x = this.stage.stageWidth / 2;
        this.bg.y = this.stage.stageHeight / 2;
        this.addChild(this.bg);
        this.roleContainer = new egret.DisplayObjectContainer();
        this.addChild(this.roleContainer);
    };
    GameLevel.prototype.initRole = function () {
        this.mainRole = Game.instance.roleManager.getRole(userGameData.mainRole);
        if (userGameData.roles && userGameData.roles.length > 0) {
            for (var i = 0, len = userGameData.roles.length; i < len; i++) {
                var roleObj = userGameData.roles[i];
                var role = Game.instance.roleManager.getRole(roleObj.id);
                // this.roleContainer.addChild(role);
                role.roleX = this.mainRole.roleX + (i + 1) * this.gap;
                role.roleY = this.mainRole.roleY;
                this.rolePositions.push({ x: this.mainRole.roleX + i * this.gap, y: this.mainRole.roleY });
                this.roles.push(role);
            }
        }
        this.resetRole();
    };
    GameLevel.prototype.resetRole = function () {
        this.endX = 25;
        this.endY = 50;
        this.mainRole.roleX = this.endX;
        this.mainRole.roleY = this.endY;
        this.roleContainer.addChild(this.mainRole);
        this.mainRole.stand(Role.FRONT);
        if (this.roles && this.roles.length > 0) {
            this.rolePositions.length = 0;
            for (var i = 0, len = this.roles.length; i < len; i++) {
                var role = this.roles[i];
                this.roleContainer.addChild(role);
                role.roleX = this.mainRole.roleX + (i + 1) * this.gap;
                role.roleY = this.mainRole.roleY;
                this.rolePositions.push({ x: this.mainRole.roleX + i * this.gap, y: this.mainRole.roleY });
            }
        }
    };
    GameLevel.prototype.onTouch = function (e) {
        // this.removeEventListener(egret.TouchEvent.TOUCH_END, this.onTouch, this);
        // this.next("gameStart");
    };
    GameLevel.prototype.tick = function (advancedTime) {
        dragonBones.WorldClock.clock.advanceTime(-1);
        var moveType = this.mainRole.addStep(this.endX, this.endY);
        if (moveType > 0) {
            for (var i = 0; i < this.roles.length; i++) {
                this.roles[i].addStep(this.rolePositions[i].x, this.rolePositions[i].y);
            }
            this.walkCount++;
            if (this.walkCount == this.gap) {
                this.rolePositions[0] = { x: this.mainRole.roleX, y: this.mainRole.roleY };
                for (var i = 1; i < this.roles.length; i++) {
                    this.rolePositions[i] = { x: this.roles[i - 1].roleX, y: this.roles[i - 1].roleY };
                }
                this.walkCount = 0;
            }
            this.roleSort();
            if (moveType == 2) {
                this.mainRole.stand(this.mainRole.direct);
                for (var i = 0; i < this.roles.length; i++) {
                    this.roles[i].stand(this.roles[i].direct);
                }
            }
        }
    };
    GameLevel.prototype.roleSort = function () {
        this.roleContainer.$children.sort(function (a, b) {
            return a.y < b.y ? -1 : 1;
        });
    };
    GameLevel.prototype.initListener = function () {
        this.touchEnabled = true;
        this.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onClickHandler, this);
        packageList.addEventListener("suicide", this.onSuicide, this);
    };
    GameLevel.prototype.onClickHandler = function (e) {
        this.endX = Math.round(e.stageX / GameLevel.STEP);
        this.endY = Math.round(e.stageY / GameLevel.STEP);
    };
    GameLevel.prototype.onSuicide = function (e) {
        var self = this;
        talk.setTalk("自杀", function () {
            self.suicide();
        });
    };
    return GameLevel;
}(Scene));
GameLevel.STEP = 8;
__reflect(GameLevel.prototype, "GameLevel");
//# sourceMappingURL=GameLevel.js.map