/**
 * Created by rockyl on 2018-12-03.
 */

import {Entity, ScillaEngine, setupScene} from "scilla";

/**
 * 场景
 */
export default class Scene {
	private _engine: ScillaEngine;
	private _name: string;
	private _resourceGroups: any = {
		preload: [],
		delay: [],
	};

	private _config: any;
	private _root: Entity;

	constructor(engine: ScillaEngine) {
		this._engine = engine;
	}

	get root(): Entity {
		return this._root;
	}

	/**
	 * 初始化
	 * @param config
	 */
	initByConfig(config: any) {
		this._config = config;
		this._name = config.name;

		const resourceGroups = config['resource-groups'];
		for (let key in resourceGroups) {
			this._resourceGroups[key] = resourceGroups[key];
		}
	}

	setup(root: Entity) {
		this._root = root;

		const assetsManager = this._engine.assetsManager;
		setupScene(this._config, root, assetsManager.getRes.bind(assetsManager));
	}

	/**
	 * 加载资源组
	 * @param name
	 * @param replaceConfig
	 * @param progress
	 */
	async loadResGroup(name, replaceConfig?, progress?) {
		await this._engine.assetsManager.loadResItems(this._resourceGroups[name], replaceConfig, progress);
	}
}
