var Phaser = Phaser || {};
var Ritual = Ritual || {};

Ritual.LoadingState = function () {
    "use strict";
    Phaser.State.call(this);
};

Ritual.prototype = Object.create(Phaser.State.prototype);
Ritual.prototype.constructor = Ritual.LoadingState;

Ritual.LoadingState.prototype.init = function (level_data) {
    "use strict";
    this.level_data = level_data;
};

Ritual.LoadingState.prototype.preload = function () {
    "use strict";
    var assets, asset_loader, asset_key, asset;
    assets = this.level_data.assets;
    for (asset_key in assets) { // load assets according to asset key
        if (assets.hasOwnProperty(asset_key)) {
            asset = assets[asset_key];
            switch (asset.type) {
            case "image":
                this.load.image(asset_key, asset.source);
                break;
            case "spritesheet":
                this.load.spritesheet(asset_key, asset.source, asset.frame_width, asset.frame_height, asset.frames, asset.margin, asset.spacing);
                break;
            case "tilemap":
                this.load.tilemap(asset_key, asset.source, null, Phaser.Tilemap.TILED_JSON);
                break;
            }
        }
    }
	
	this.load.bitmapFont('desyrel', 'assets/fonts/desyrel.png', 'assets/fonts/desyrel.xml');
};

Ritual.LoadingState.prototype.create = function () {
    "use strict";
	console.log('game start');
    this.game.state.start("GameState", true, false, this.level_data);
};