import Script from '../components/Script.js';
import { System } from '../System.js';


class ScriptSystem extends System {
    constructor() {
        super();
        //
        this.entityManager = null;

        this.dependencies = [Script];
    }

    update(timeDelta) {

        const entityManager = this.entityManager;

        const dataset = entityManager.dataset;

        if (dataset === null) {
            //no dataset to update
            return;
        }

        dataset.traverseComponents(Script, function (component, entity) {
            const scripts = component.scripts;
            let i = 0;
            const l = scripts.length;
            for (; i < l; i++) {
                const script = scripts[i];

                try {
                    script(timeDelta);
                } catch (e) {
                    console.error(`ScriptComponent ${entity} threw error`, e);
                }
            }
        });
    }
}


export default ScriptSystem;
