colider_lib 0.2.0

A bevy collider crate.
Documentation
//!This crate is a bevy collision detection crate.
//! This crate adds a collider and a collision event.
use bevy::prelude::*;
mod collision;
use collision::*;

///This is the collider builder.
/// This is how you use it:
/// Create a collider using [ColliderBuilder::new_cuboid_collider].
/// Add the collider to an object when spawning using [Commands::spawn] it to add collision to that object.
/// Add an [Update] system that checks for collision between two colliders using [RectangleCollider::check_collider_collision] if the event_feature is not enabled.
/// If the event feature is enabled, then add an observer to get triggered by the [CollisionEvent].
/// Add another [Update] system that updates collider position using [Collider::move_collider] if auto_move is disabled.
/// If auto_move is enabled, then move the object with the collider.
/// If you want to use the event or auto_move features, add the [ColliderPlugin] to your app.
/// WARNING!
/// Physics is not present in this library.
/// If you want a physics library, use bevy_rapier2d.
/// WARNING!
pub struct ColliderBuilder;
impl ColliderBuilder {
    pub fn new_rectangle_collider(half_x: f32, half_y: f32, position: Vec2) -> RectangleCollider {
        RectangleCollider { half_x, half_y, position }
    }
}

///This plugin adds a collision event and automatic collider movement if the vent and auto_move features are enabled.
///It is meant to be used with [EntityCommands::observe].
///This feature is enabled with the event or the auto_move features;
#[cfg(feature = "plugin")]
pub struct ColliderPlugin;
#[cfg(feature = "plugin")]
impl Plugin for ColliderPlugin {
    fn build(&self, app: &mut App) {
        #[cfg(feature = "event")]
        app.add_systems(Update, event_trigger).add_event::<CollisionEvent>();

        if cfg!(feature = "auto_move") {
            app.add_plugins(CollisionPlugin);
        }
    }
}


#[cfg(feature = "event")]
#[derive(Event)]
///This is an event that gets triggered if there is a collision between two colliders.
pub struct CollisionEvent {
    collider1: RectangleCollider,
    collider2: RectangleCollider
}

#[cfg(feature = "event")]
pub fn event_trigger(collider_query: Query<(Entity, &RectangleCollider), With<RectangleCollider>>, mut commands: Commands) {
    let collider_query2 = collider_query.clone();
    for (entity,collider) in collider_query.iter() {
        for (_, collider2) in collider_query2.iter() {
            if collider.position != collider2.position {
                if collider.clone().check_collision(&collider2.clone()) {
                    commands.trigger_targets(CollisionEvent{collider1:collider.clone(), collider2:collider2.clone()}, entity);
                }
            }
        }
    }
}

#[cfg(feature = "plugin")]
#[cfg(not(any(feature = "auto_move", feature = "event")))]
compile_error!("Either auto_move or event or both must be enabled to use the plugin");