Stage changes for multiple tilesets per tmx file

stinkhead7ds
Ashley N. 2023-09-19 17:43:07 -04:00
parent 6fe992cb33
commit 32874109d8
2 changed files with 14 additions and 8 deletions

View File

@ -6,7 +6,7 @@ use super::ecs;
#[derive(Debug)] #[derive(Debug)]
pub struct TiledTilemap { pub struct TiledTilemap {
pub tileset: TiledTileset, pub tileset: Vec<TiledTileset>,
pub layers: Vec<Layer>, pub layers: Vec<Layer>,
ecs: Vec<ecs::Entity>, ecs: Vec<ecs::Entity>,
pub width: usize, pub width: usize,
@ -207,6 +207,7 @@ pub fn get_tiled_tilemap( path: &str ) -> Result<TiledTilemap, Box<dyn Error>> {
} }
let tileset = get_tiles( tileset, &working_directory )?; let tileset = get_tiles( tileset, &working_directory )?;
let tileset = vec![ tileset ];
// Get the layers // Get the layers
let layers: Vec<Layer> = map.descendants() let layers: Vec<Layer> = map.descendants()

View File

@ -16,17 +16,18 @@ pub fn get_tiles( tilemap: &TiledTilemap ) -> Result<(Vec<u8>, Vec<u8>), Box<dyn
]; ];
let mut all_tiles: Vec<u8> = vec![0; 32]; // --system md, start with a blank buffer tile let mut all_tiles: Vec<u8> = vec![0; 32]; // --system md, start with a blank buffer tile
let tiles_height = tilemap.tileset.image.height() / 8; // --system md let tileset = tilemap.tileset.first().ok_or( "internal error: no tileset" )?;
let tiles_width = tilemap.tileset.image.width() / 8; // --system md let tiles_height = tileset.image.height() / 8; // --system md
let tiles_width = tileset.image.width() / 8; // --system md
for tile_y in 0..tiles_height { for tile_y in 0..tiles_height {
for tile_x in 0..tiles_width { for tile_x in 0..tiles_width {
let tile_index = ( tile_y * tiles_width ) + tile_x; let tile_index = ( tile_y * tiles_width ) + tile_x;
let tile = tilemap.tileset.image.clone().crop( tile_x * 8, tile_y * 8, 8, 8 ); let tile = tileset.image.clone().crop( tile_x * 8, tile_y * 8, 8, 8 );
// Fake palette (see below) // Fake palette (see below)
let mut fake: [u16; 16] = [ 0; 16 ]; let mut fake: [u16; 16] = [ 0; 16 ];
let selected_pal = tilemap.tileset.palettes[ tile_index as usize ]; let selected_pal = tileset.palettes[ tile_index as usize ];
let tile_bin = image_to_tiles( let tile_bin = image_to_tiles(
&tile, &tile,
@ -72,6 +73,8 @@ pub fn get_tiles( tilemap: &TiledTilemap ) -> Result<(Vec<u8>, Vec<u8>), Box<dyn
* In `--system md`, this outputs tilemap B and then tilemap A * In `--system md`, this outputs tilemap B and then tilemap A
*/ */
pub fn get_tilemap( tilemap: &TiledTilemap ) -> Result<Vec<u8>, Box<dyn Error>> { pub fn get_tilemap( tilemap: &TiledTilemap ) -> Result<Vec<u8>, Box<dyn Error>> {
let tileset = tilemap.tileset.first().ok_or( "internal error: no tileset" )?;
let layer_b: Option<&Layer> = tilemap.layers.iter().find( | layer | matches!( layer, Layer::Tile { system_plane: SystemPlane::MdPlaneB, tiles: _ } ) ); let layer_b: Option<&Layer> = tilemap.layers.iter().find( | layer | matches!( layer, Layer::Tile { system_plane: SystemPlane::MdPlaneB, tiles: _ } ) );
let layer_a: Option<&Layer> = tilemap.layers.iter().find( | layer | matches!( layer, Layer::Tile { system_plane: SystemPlane::MdPlaneA, tiles: _ } ) ); let layer_a: Option<&Layer> = tilemap.layers.iter().find( | layer | matches!( layer, Layer::Tile { system_plane: SystemPlane::MdPlaneA, tiles: _ } ) );
@ -94,7 +97,7 @@ pub fn get_tilemap( tilemap: &TiledTilemap ) -> Result<Vec<u8>, Box<dyn Error>>
// From the starting point of x, y "stamp" the target tile's indices // From the starting point of x, y "stamp" the target tile's indices
let nametable_entry = target_tile; let nametable_entry = target_tile;
let selected_pal = tilemap.tileset.palettes[ source_tile as usize ]; let selected_pal = tileset.palettes[ source_tile as usize ];
if let Some( selected_pal ) = selected_pal { if let Some( selected_pal ) = selected_pal {
nametable[ ( y * tilemap.width ) + x ] = ( ( selected_pal as u16 ) << 13 ) | nametable_entry; nametable[ ( y * tilemap.width ) + x ] = ( ( selected_pal as u16 ) << 13 ) | nametable_entry;
} else { } else {
@ -124,7 +127,7 @@ pub fn get_tilemap( tilemap: &TiledTilemap ) -> Result<Vec<u8>, Box<dyn Error>>
// From the starting point of x, y "stamp" the target tile's indices // From the starting point of x, y "stamp" the target tile's indices
let nametable_entry = target_tile; let nametable_entry = target_tile;
let selected_pal = tilemap.tileset.palettes[ source_tile as usize ]; let selected_pal = tileset.palettes[ source_tile as usize ];
if let Some( selected_pal ) = selected_pal { if let Some( selected_pal ) = selected_pal {
nametable[ ( y * tilemap.width ) + x ] = ( ( selected_pal as u16 ) << 13 ) | nametable_entry; nametable[ ( y * tilemap.width ) + x ] = ( ( selected_pal as u16 ) << 13 ) | nametable_entry;
} else { } else {
@ -173,10 +176,12 @@ pub fn get_collision_map( tilemap: &TiledTilemap ) -> Result<Vec<u8>, Box<dyn Er
* Get a helper .asm or .c file that ties all the level components together * Get a helper .asm or .c file that ties all the level components together
*/ */
pub fn get_code( tilemap: &TiledTilemap, level_name: &str, path_prefix: &str ) -> Result<String, Box<dyn Error>> { pub fn get_code( tilemap: &TiledTilemap, level_name: &str, path_prefix: &str ) -> Result<String, Box<dyn Error>> {
let tileset = tilemap.tileset.first().ok_or( "internal error: no tileset" )?;
let version = env!( "CARGO_PKG_VERSION" ); let version = env!( "CARGO_PKG_VERSION" );
let level_label = symbol_to_pascal( level_name ); let level_label = symbol_to_pascal( level_name );
let ( width, height ) = ( tilemap.width, tilemap.height ); let ( width, height ) = ( tilemap.width, tilemap.height );
let num_tiles = ( tilemap.tileset.image.width() / 8 ) * ( tilemap.tileset.image.height() / 8 ); // --system md let num_tiles = ( tileset.image.width() / 8 ) * ( tileset.image.height() / 8 ); // --system md
let file = format!( r#"; Level definition file let file = format!( r#"; Level definition file
; Generated by reskit v{version} ; Generated by reskit v{version}