package ecs.systems;

import ecs.core.System;
import res.Scene;

class FlashSystem extends System {
	final scene:Scene;

	var flashes:Int = 0;
	var time:Float = 0;

	var flashTime:Float;

	var flashColor:Int;

	public function new(scene:Scene) {
		super();

		this.scene = scene;
	}

	public function flash(num:Int, time:Float, color:Int = 13) {
		flashes = num;
		flashTime = time;
		flashColor = color;
	}

	override function update(dt:Float) {
		if (flashes > 0) {
			final totalTime = flashTime * 2;

			final t = time / totalTime;

			scene.clearColorIndex = t <= 0.5 ? flashColor : 1;

			if (time == totalTime) {
				flashes--;
				time = 0;
			} else
				time = Math.min(totalTime, time + dt);
		}
	}
}
