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 Game = (function (_super) {
    __extends(Game, _super);
    // public gameURL:string = "http://10.0.12.50:5334/index.html";
    function Game() {
        var _this = _super.call(this) || this;
        _this.gameURL = "http://sucresk.github.io/ggj2017/index.html";
        Game._instance = _this;
        _this.addEventListener(egret.Event.ADDED_TO_STAGE, _this.onAdded, _this);
        _this.roleManager = new RoleManager();
        // this.random = new Random(0, RES.getRes("random_png"));
        _this.random = new Random(Math.floor(Math.random() * 100000), RES.getRes("random_png"));
        return _this;
    }
    Object.defineProperty(Game, "instance", {
        get: function () {
            return Game._instance;
        },
        enumerable: true,
        configurable: true
    });
    Game.prototype.resetGame = function () {
        this.clearData();
        this.initGameData();
        var rIdStr = this.GetQueryString("rid");
        var rid = parseFloat(rIdStr);
        if (rid >= 0 && rid <= 7) {
            this.addRole(rid);
        }
    };
    Game.prototype.loadGame = function () {
        this.load();
        this.initGameData();
        var rIdStr = this.GetQueryString("rid");
        var rid = parseFloat(rIdStr);
        if (rid >= 0 && rid <= 7) {
            this.addRole(rid);
        }
        this.loadPackage();
    };
    Game.prototype.GetQueryString = function (name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) {
            return decodeURI(r[2]);
        }
        return null;
    };
    Game.prototype.addRole = function (rid) {
        var roleId = roleDefine[rid];
        if (roleId) {
            if (userGameData.roles.length < 7) {
                userGameData.roles.push({ id: roleId });
                this.save();
            }
        }
    };
    Game.prototype.clearData = function () {
        egret.localStorage.clear();
    };
    Game.prototype.save = function () {
        var data = JSON.stringify(userGameData);
        egret.localStorage.setItem("uuuData", data);
    };
    Game.prototype.load = function () {
        var data = egret.localStorage.getItem("uuuData");
        var dataJson = JSON.parse(data);
        var changed;
        if (dataJson.startTime == null) {
            dataJson.startTime = new Date().getTime();
            changed = true;
        }
        if (dataJson.suicideTime == null) {
            dataJson.suicideTime = 0;
            changed = true;
        }
        console.log("load  ", dataJson);
        userGameData = dataJson;
        if (changed) {
            this.save();
        }
    };
    Game.prototype.initGameData = function () {
        if (userGameData == null) {
            userGameData = {};
            var roleIndex = Game.instance.random.Int(roleDefine.length);
            userGameData.mainRole = roleDefine[roleIndex];
            userGameData.package = [];
            userGameData.roles = [];
            userGameData.startTime = new Date().getTime();
            userGameData.suicideTime = 0;
            this.save();
        }
    };
    Game.prototype.addPackageItem = function (itemID) {
        userGameData.package.push(itemID);
        this.save();
    };
    Game.prototype.loadPackage = function () {
        if (userGameData.package) {
            for (var i = 0, len = userGameData.package.length; i < len; i++) {
                packageList.addID(userGameData.package[i]);
            }
        }
    };
    Game.prototype.getItemObj = function (id) {
        var items = RES.getRes("items_json")["items"];
        for (var i = 0, len = items.length; i < len; i++) {
            if (items[i].id == id) {
                return items[i];
            }
        }
        return null;
    };
    Game.prototype.onAdded = function (e) {
        this.removeEventListener(egret.Event.ADDED_TO_STAGE, this.onAdded, this);
        this.init();
    };
    Game.prototype.init = function () {
        console.log("this is a new game!");
        this.initDB();
        this.pageLayer = new egret.DisplayObjectContainer();
        this.addChild(this.pageLayer);
        packageList = new PackageList();
        this.addChild(packageList);
        packageList.bottom = 0;
        talk = new Talk();
        this.addChild(talk);
        talk.visible = false;
        letter = new Letter();
        this.addChild(letter);
        letter.visible = false;
        var sceneManager = new SceneManager(this.pageLayer);
        sceneManager.registerScene("gameInfo", new GameInfo());
        sceneManager.registerScene("gameLevel", new GameLevel());
        sceneManager.registerScene("gameStart", new GameStart());
        sceneManager.registerScene("gameEnd1", new GameEnd1());
        sceneManager.registerScene("gameEnd2", new GameEnd2());
        sceneManager.registerScene("gameScore", new GameScore());
        sceneManager.setCurSceneByName("gameStart");
        sceneManager.startTick();
        //  suicideConfim = new Confirm();
        //  this.addChild(suicideConfim);
        //  suicideConfim.visible = false;
    };
    Game.prototype.initDB = function () {
        DBManager.addData("items_ske_json", "items_tex_json", "items_tex_png");
        //DBManager.addData("bubbles_json","texture2_json","texture2_png")
        //DBManager.addData("questAnim_json","texture3_json","texture3_png")
    };
    return Game;
}(eui.UILayer));
__reflect(Game.prototype, "Game");
//# sourceMappingURL=Game.js.map