package ecs.systems;

import Math.*;
import ecs.components.Aim;
import ecs.components.Spatial;
import ecs.components.TriggerRadius;
import ecs.core.Entity;
import ecs.core.System;
import res.geom.Vector2;

class AimSystem extends System {
	public function new() {
		super(Aim, TriggerRadius, Spatial);
	}

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

		aim.targets.resize(0);

		final triggerRadius = entity.get(TriggerRadius);
		final spatial = entity.get(Spatial);

		final targets = engine.queryEntities([aim.aimTo, TriggerRadius, Spatial]);

		for (target in targets) {
			final targetSpatial = target.get(Spatial);
			final targetTriggerRadius = target.get(TriggerRadius);

			final d = Vector2.xy(targetSpatial.x - spatial.x, targetSpatial.y - spatial.y).length2();

			if (d <= pow(triggerRadius.radius + targetTriggerRadius.radius, 2)) {
				aim.targets.push(target);
			}
		}
	}
}
