bevy_entitiles 0.2.2

A tilemap library for bevy. With many algorithms built in.
use bevy::{
    app::{FixedUpdate, Plugin},
    ecs::{
        event::{EventReader, EventWriter},
        system::{Commands, Query},
    },
    math::{UVec2, Vec2},
};
use bevy_rapier2d::{
    geometry::{ActiveEvents, Collider, Friction, Sensor},
    pipeline::CollisionEvent,
    plugin::{NoUserData, RapierPhysicsPlugin},
    render::RapierDebugRenderPlugin,
};

use crate::{
    math::FillArea,
    tilemap::{
        map::Tilemap,
        tile::{Tile, TileType},
    },
};

use super::{get_collision, TileCollision};

pub struct PhysicsRapierTilemapPlugin;

impl Plugin for PhysicsRapierTilemapPlugin {
    fn build(&self, app: &mut bevy::prelude::App) {
        app.add_plugins(RapierPhysicsPlugin::<NoUserData>::default())
            .add_systems(FixedUpdate, collision_handler);

        #[cfg(feature = "debug")]
        app.add_plugins(RapierDebugRenderPlugin::default());
    }
}

impl Tilemap {
    /// Give the tiles physics colliders.
    pub fn fill_physics_tile_rapier(
        &mut self,
        commands: &mut Commands,
        area: FillArea,
        friction: Option<f32>,
        is_trigger: bool,
    ) {
        for y in area.origin.y..=area.dest.y {
            for x in area.origin.x..=area.dest.x {
                self.set_physics_tile_rapier(commands, UVec2 { x, y }, friction, is_trigger);
            }
        }
    }

    /// Give the tile a physics collider.
    pub fn set_physics_tile_rapier(
        &mut self,
        commands: &mut Commands,
        index: UVec2,
        friction: Option<f32>,
        is_trigger: bool,
    ) {
        let Some(tile_entity) = self.get(index) else {
            return;
        };

        let x = self.tile_slot_size.x;
        let y = self.tile_slot_size.y;
        let translation = self.index_to_world(index);

        let collider = match self.tile_type {
            TileType::Square => Collider::convex_hull(&[
                (Vec2::new(-x / 2., -y / 2.) + translation).into(),
                (Vec2::new(-x / 2., y / 2.) + translation).into(),
                (Vec2::new(x / 2., y / 2.) + translation).into(),
                (Vec2::new(x / 2., -y / 2.) + translation).into(),
            ]),
            TileType::IsometricDiamond => Collider::convex_hull(&[
                (Vec2::new(-x / 2., 0.) + translation).into(),
                (Vec2::new(0., y / 2.) + translation).into(),
                (Vec2::new(x / 2., 0.) + translation).into(),
                (Vec2::new(0., -y / 2.) + translation).into(),
            ]),
        }
        .unwrap();

        if is_trigger {
            commands
                .entity(tile_entity)
                .insert((collider, Sensor, ActiveEvents::COLLISION_EVENTS));
        } else {
            commands
                .entity(tile_entity)
                .insert((collider, ActiveEvents::COLLISION_EVENTS));
        }

        if let Some(coe) = friction {
            commands
                .entity(tile_entity)
                .insert(Friction::coefficient(coe));
        }
    }
}

pub fn collision_handler(
    mut collision: EventReader<CollisionEvent>,
    mut tile_collision: EventWriter<TileCollision>,
    tiles_query: Query<&Tile>,
) {
    let mut colls = Vec::with_capacity(collision.len());
    for c in collision.read() {
        match c {
            CollisionEvent::Started(e1, e2, _) => {
                if let Some(data) = get_collision(*e1, *e2, &tiles_query) {
                    colls.push(TileCollision::Started(data));
                }
            }
            CollisionEvent::Stopped(e1, e2, _) => {
                if let Some(data) = get_collision(*e1, *e2, &tiles_query) {
                    colls.push(TileCollision::Stopped(data));
                }
            }
        }
    }
    tile_collision.send_batch(colls);
}