package ecs.systems;

import ecs.components.Action;
import ecs.core.Entity;
import ecs.core.System;

class ScriptSystem extends System {
	var events:Array<ScriptEvent> = [];

	public function new() {
		super(Action);

		renderable = false;
	}

	public function addEvent(...events:ScriptEvent) {
		for (event in events) {
			this.events.push(event);
		}
	}

	override function updateEntity(entity:Entity, dt:Float) {
		final action = entity.get(Action);

		action.action();

		entity.remove(action);
	}

	override function update(dt:Float) {
		if (events.length > 0) {
			events[0].update(dt);

			if (events[0].ready)
				events[0].go();

			if (events[0].remove)
				events.shift();
		}

		super.update(dt);
	}
}
