package ecs.systems;

import ecs.core.Entity;
import ecs.components.Explosion;
import ecs.components.Spatial;
import ecs.core.System;
import res.display.FrameBuffer;
import Math.*;

using Std;
using res.graphics.Painter;

class ExplosionSystem extends System {
	public function new() {
		super(Spatial, Explosion);
	}

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

		if (explosion.time >= explosion.totalTime) {
			engine.remove(entity);
		} else
			explosion.time += dt;
	}

	override function render(frameBuffer:FrameBuffer) {
		for (entity in entities) {
			final explosion = entity.get(Explosion);
			final spatial = entity.get(Spatial);

			final t = 1 - pow(1 - (explosion.time / explosion.totalTime), 3);

			frameBuffer.circle(spatial.x.int(), spatial.y.int(), (explosion.maxRadius * (t * t * t)).int(), 5, 5);
		}
	}
}
