diff --git a/assets/up_test.png b/assets/up_test.png new file mode 100644 index 0000000..2c32852 Binary files /dev/null and b/assets/up_test.png differ diff --git a/src/constants.rs b/src/constants.rs index 42cdd04..002a045 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -1,4 +1,5 @@ pub const PIXEL_RATIO: f32 = 2.0; -pub const TILE_SIZE: f32 = 16.0; +pub const TILE_PIXELS: f32 = 16.0; +pub const TILE_SIZE: f32 = TILE_PIXELS * PIXEL_RATIO; pub const ITILE_SIZE: i32 = TILE_SIZE as i32; pub const UTILE_SIZE: u32 = TILE_SIZE as u32; diff --git a/src/cursor.rs b/src/cursor.rs index 6160ef3..84f370e 100644 --- a/src/cursor.rs +++ b/src/cursor.rs @@ -24,7 +24,7 @@ pub fn move_cursor( for mut transform in query.iter_mut() { let (camera, camera_transform) = *camera_query; - let Ok(window) = windows.get_single() else { + let Ok(window) = windows.single() else { return; }; diff --git a/src/tilemap.rs b/src/tilemap.rs index 41a318a..640e71c 100644 --- a/src/tilemap.rs +++ b/src/tilemap.rs @@ -275,8 +275,8 @@ fn generate_chunks_from_algo( fn setup_initial_chunks(mut event_writer: EventWriter) { println!("setup_initial_chunks"); - for x in -7..=7 { - for y in -7..=7 { + for x in -2..=2 { + for y in -2..=2 { event_writer.write(LoadChunkEvent { chunk_position: IVec2::new(x, y), }); diff --git a/src/tiles.rs b/src/tiles.rs index 0bb4c75..4939d90 100644 --- a/src/tiles.rs +++ b/src/tiles.rs @@ -168,11 +168,19 @@ pub fn build_quilted_terrain_sprites( let min_y = tiles.iter().map(|(pos, _)| pos.y).reduce(f32::min).unwrap(); let max_y = tiles.iter().map(|(pos, _)| pos.y).reduce(f32::max).unwrap(); + // Snap min/max values to tile grid - ensure we're centered on tile centers + // Our tiles are positioned at their centers (not corners) + let min_x_aligned = (min_x / TILE_SIZE).floor() * TILE_SIZE; + let min_y_aligned = (min_y / TILE_SIZE).floor() * TILE_SIZE; + let max_x_aligned = (max_x / TILE_SIZE).ceil() * TILE_SIZE; + let max_y_aligned = (max_y / TILE_SIZE).ceil() * TILE_SIZE; + // Calculate texture dimensions in pixels - let width_tiles = ((max_x - min_x) / TILE_SIZE).ceil() as u32 + 1; - let height_tiles = ((max_y - min_y) / TILE_SIZE).ceil() as u32 + 1; - let width_px = width_tiles * UTILE_SIZE; - let height_px = height_tiles * UTILE_SIZE; + // We use TILE_PIXELS (16.0) as the base unit for the texture size + let width_tiles = ((max_x_aligned - min_x_aligned) / TILE_SIZE) as u32; + let height_tiles = ((max_y_aligned - min_y_aligned) / TILE_SIZE) as u32; + let width_px = width_tiles * TILE_PIXELS as u32; + let height_px = height_tiles * TILE_PIXELS as u32; // Store the dimensions for later use (especially for adjusting the transform) quilt_cache @@ -191,10 +199,16 @@ pub fn build_quilted_terrain_sprites( // Fetch the source texture data if let Some(source_texture) = images.get(texture) { // Calculate position within the quilted texture - let tile_x = ((pos.x - min_x) / TILE_SIZE).floor() as u32; - let tile_y = ((pos.y - min_y) / TILE_SIZE).floor() as u32; + // We need to be precise with the mapping from world coordinates to texture pixels + // Calculate tile position relative to the quilted texture origin + // This gives us the tile index (whole number of tiles from origin) + let tile_x = ((pos.x - min_x_aligned) / TILE_SIZE).round() as u32; + let tile_y = + (height_tiles as f32 - 1.0 - ((pos.y - min_y_aligned) / TILE_SIZE).round()) + as u32; // Copy the tile texture into the quilted texture + // Convert from tile indices to pixel coordinates in the output texture if let Some(data) = &source_texture.data { blit_texture( data, @@ -203,8 +217,8 @@ pub fn build_quilted_terrain_sprites( source_texture.size().y as u32, width_px, height_px, - tile_x * UTILE_SIZE, - tile_y * UTILE_SIZE, + (tile_x * TILE_PIXELS as u32) + (TILE_PIXELS / 2.0) as u32, + (tile_y * TILE_PIXELS as u32) + (TILE_PIXELS / 2.0) as u32, ); } } @@ -226,8 +240,9 @@ pub fn build_quilted_terrain_sprites( let texture_handle = images.add(quilted_texture); // Calculate the center position of the quilted sprite - let center_x = min_x + (max_x - min_x) / 2.0; - let center_y = min_y + (max_y - min_y) / 2.0; + // Using the aligned coordinates to ensure proper centering + let center_x = min_x_aligned + (max_x_aligned - min_x_aligned) / 2.0; + let center_y = min_y_aligned + (max_y_aligned - min_y_aligned) / 2.0; // Spawn the quilted sprite commands.spawn(( @@ -235,8 +250,7 @@ pub fn build_quilted_terrain_sprites( image: texture_handle, ..Default::default() }, - Transform::from_xyz(center_x, center_y, *z_index as f32) - .with_scale(Vec3::splat(PIXEL_RATIO)), + Transform::from_xyz(center_x, center_y, 0.0).with_scale(Vec3::splat(PIXEL_RATIO)), Visibility::Hidden, TerrainSprite { z_index: *z_index }, ));