package ecs.systems;

import ecs.components.CollisionResult;
import ecs.components.ResolveCollision;
import ecs.components.Spatial;
import ecs.components.Velocity;
import ecs.core.Entity;
import ecs.core.System;
import res.geom.Vector2;

class CollisionResolveSystem extends System {
	public function new() {
		super(Spatial, ResolveCollision, CollisionResult, Velocity);
	}

	override function updateEntity(entity:Entity, dt:Float) {
		final collisions = entity.get(CollisionResult);
		final velocity = entity.get(Velocity);
		final spatial = entity.get(Spatial);

		for (collision in collisions.collisions) {
			switch (collision.result) {
				case OFFSET(dx, dy):
					spatial.x -= dx;
					spatial.y -= dy;

					final v = Vector2.xy(dx, dy).normalize();
					final sp = velocity.speed;

					velocity.x = -v.x * sp;
					velocity.y = -v.y * sp;
				case _:
			}
		}
	}
}
