feat: Barely working tilemap system

- Added tilemap bundle
- Added HALVED_METERS_PER_PIXEL constant
This commit is contained in:
Alexey 2026-03-23 14:06:36 +03:00
commit b59cec172d
8 changed files with 109 additions and 18 deletions

60
src/layout/tilemap.rs Normal file
View file

@ -0,0 +1,60 @@
use bevy::{
image::{
ImageArrayLayout,
ImageLoaderSettings,
},
prelude::*,
sprite_render::{
TileData,
TilemapChunk,
TilemapChunkTileData,
},
};
use bevy_rapier2d::prelude::*;
use crate::{HALVED_PIXELS_PER_METER, PIXELS_PER_METER};
const TILEMAP_PATH: &'static str = "sprites/level/tilemap.png";
const TILE_DISPLAY_SIZE: UVec2 = UVec2::splat(PIXELS_PER_METER as u32);
#[derive(Component, Debug, PartialEq, Eq, Default, Clone, Copy, Reflect)]
#[reflect(Component, Debug, PartialEq, Default, Clone)]
pub struct Tilemap;
pub fn tilemap_bundle(
asset_server: &Res<AssetServer>,
size: UVec2,
tiles: Vec<(u16, UVec2)>,
colliders: Vec<(Collider, Vec2)>,
) -> impl Bundle {
let tile_data: Vec<Option<TileData>> = (0..size.element_product()).map(|xy| {
if let Some((id, _)) = tiles.iter().find(|(_, pos)| pos.x == xy % size.x && pos.y == xy / size.x) {
Some(TileData::from_tileset_index(*id))
} else { None }
}).collect();
(
Tilemap,
TilemapChunk {
chunk_size: size,
tile_display_size: TILE_DISPLAY_SIZE,
tileset: asset_server.load_with_settings(
TILEMAP_PATH,
|settings: &mut ImageLoaderSettings| {
settings.array_layout = Some(ImageArrayLayout::RowCount { rows: 3 })
},
),
..default()
},
TilemapChunkTileData(tile_data),
Children::spawn(SpawnIter(colliders.into_iter()
.map(move |(collider, pos)| {(
collider.clone(),
Transform::from_xyz(
pos.x - HALVED_PIXELS_PER_METER * size.x as f32,
pos.y,
0.
),
)})
)),
)
}