use crate::components::PhysicsComponent;
use crate::entity::EntityManager;
use crate::physics::PhysicsManager;
use crate::utility::transform::Transform;
use crate::world::Storage;

pub fn physics_sync_system(
    entities: &EntityManager,
    physics: &Storage<PhysicsComponent>,
    transforms: &mut Storage<Transform>,
)
{
    let all_entities = entities.all_entities();

    for entity in all_entities
    {
        if let Some(physics) = physics.get(entity)
        {
            if let Some(rigidbody_position) =
                PhysicsManager::get_rigidbody_position(physics.rigidbody)
            {
                let transform = Transform::from(rigidbody_position);

                transforms.with_mut(entity, |t| {
                    *t = transform;
                });
            }
        }
    }
}
