From 15143e08cb9e52163933d8d10faee4426eff9067 Mon Sep 17 00:00:00 2001 From: ashley Date: Mon, 30 Oct 2023 21:09:43 -0400 Subject: [PATCH] WIP - New metatile converter --- src/reskit/level/system.rs | 25 ++++++------------------- 1 file changed, 6 insertions(+), 19 deletions(-) diff --git a/src/reskit/level/system.rs b/src/reskit/level/system.rs index 195357e..3811e83 100644 --- a/src/reskit/level/system.rs +++ b/src/reskit/level/system.rs @@ -143,7 +143,7 @@ pub fn get_metatile_maps( tilemap: &TiledTilemap ) -> Result, Box Result, Box ) -> Result<(usize, Vec>), Box> { +fn get_tilemap_prefix_palette( target_source: &str, tilesets: &Vec ) -> Result<(usize, Vec>), Box> { // Everything starts at 1 for the blank buffer tile let mut index: usize = 1; @@ -325,23 +325,12 @@ pub fn get_objs( tilemap: &TiledTilemap, symbol_ids: &HashMap ) -> pub fn get_sprites( tilemap: &TiledTilemap, sprite_ids: &HashMap ) -> Result, Box> { let mut result: Vec = Vec::new(); - let tiles_before = { - let mut total_tiles = 1; - - for tileset in &tilemap.tileset { - // do not include the sprite metadata - if tileset.sprite_metadata.is_none() { - total_tiles += ( tileset.image.width() / 8 ) * ( tileset.image.height() / 8 ); - } - } - - total_tiles - }; - let mut tiles_after: u32 = 0; - let sprites: Vec<&TiledTileset> = tilemap.tileset.iter().filter( | tileset | matches!( tileset.tile_order, TileOrder::Sprite ) ).collect(); result.extend( ( sprites.len() as u16 ).to_be_bytes() ); for sprite in sprites { + // Just find the prefix of this source (add up all the number of tiles before it) + let ( index, _ ) = get_tilemap_prefix_palette( &sprite.source, &tilemap.tileset )?; + let sprite_metadata = sprite.sprite_metadata.as_ref().ok_or( "internal error: tile order is sprite but no sprite data" )?; let sprite_id = sprite_ids.get( &sprite_metadata.id ).ok_or( format!( "invalid file: undefined sprite id \"{}\"", sprite_metadata.id ) )?; @@ -350,9 +339,7 @@ pub fn get_sprites( tilemap: &TiledTilemap, sprite_ids: &HashMap ) let tiles_across = sprite.image.width() / 8; let frames: u16 = ( tiles_across / ( sprite_metadata.width as u32 ) ) as u16; - let tile_index_location = tiles_before + tiles_after; - result.extend( ( tile_index_location as u16 ).to_be_bytes() ); - tiles_after += ( ( sprite_metadata.width as u16 * sprite_metadata.height as u16 ) * frames ) as u32; + result.extend( ( index as u16 ).to_be_bytes() ); result.push( sprite_metadata.width );