diff --git a/examples/the-dungeon-puzzlers-lament/build.rs b/examples/the-dungeon-puzzlers-lament/build.rs index 60d40a2a..c6e90dbc 100644 --- a/examples/the-dungeon-puzzlers-lament/build.rs +++ b/examples/the-dungeon-puzzlers-lament/build.rs @@ -10,24 +10,24 @@ use proc_macro2::TokenStream; const LEVEL_NAMES: &[&str] = &[ "level1", - "level2", - "level3", - "level4", - "level5", - "level6", - "level_switch", - "level_spikes", - "level_spikes2", - "squid_force_button", - "level_squid_intro", - "level_squid2", - "level_squid1", - "level_squid_item", - "level_squid_button", - "level_squid_drop", - "level_spikes3", - "level_around", - "level_squidprogramming", + // "level2", + // "level3", + // "level4", + // "level5", + // "level6", + // "level_switch", + // "level_spikes", + // "level_spikes2", + // "squid_force_button", + // "level_squid_intro", + // "level_squid2", + // "level_squid1", + // "level_squid_item", + // "level_squid_button", + // "level_squid_drop", + // "level_spikes3", + // "level_around", + // "level_squidprogramming", ]; fn main() { @@ -36,11 +36,11 @@ fn main() { let mut tile_loader = tiled::Loader::new(); let ui_map = load_tmx(&mut tile_loader, "maps/UI.tmx"); - let ui_tiles = export_tiles(&ui_map, quote!(ui)); + let ui_tiles = export_ui_tiles(&ui_map, quote!(ui)); let levels = LEVEL_NAMES .iter() - .map(|level| load_level(&mut tile_loader, &format!("maps/levels/{level}.tmx"))) + .map(|level| load_level(&mut tile_loader, &format!("maps/levels16/{level}.tmx"))) .collect::>(); let levels_tiles = levels.iter().map(|level| &level.0); let levels_data = levels.iter().map(|level| &level.1); @@ -278,7 +278,7 @@ fn export_level(map: &tiled::Map) -> Level { let tile_y = id / 11; let is_wall = tiles - .get_tile(tile_x * 2, tile_y * 2) + .get_tile(tile_x, tile_y) .map(|tile| { let tileset = tile.get_tileset(); let tile_data = &tileset.get_tile(tile.id()).unwrap(); @@ -305,6 +305,48 @@ fn export_level(map: &tiled::Map) -> Level { fn export_tiles(map: &tiled::Map, background: TokenStream) -> TokenStream { let map_tiles = map.get_layer(0).unwrap().as_tile_layer().unwrap(); + let width = map_tiles.width().unwrap() * 2; + let height = map_tiles.height().unwrap() * 2; + + let map_tiles = (0..(height * width)).map(|pos| { + let x = pos % width; + let y = pos / width; + + let tile = map_tiles.get_tile(x as i32 / 2, y as i32 / 2); + + match tile { + Some(tile) => { + let vflip = tile.flip_h; + let hflip = tile.flip_v; + + let tile_tileset_x = tile.id() % 9; + let tile_tileset_y = tile.id() / 9; + + let x_offset = if x % 2 == 0 && !hflip { 0 } else { 1 }; + let y_offset = if y % 2 == 0 && !vflip { 0 } else { 1 }; + + // calculate the actual tile ID based on the properties here + // since the tiles in tiled are 16x16, but we want to export to 8x8, we have to work this out carefully + let gba_tile_id = + tile_tileset_x * 2 + x_offset + tile_tileset_y * 9 * 4 + y_offset * 9 * 2; + let gba_tile_id = gba_tile_id as u16; + + let palette_id = + quote! { backgrounds::#background.palette_assignments[#gba_tile_id as usize] }; + quote! { TileSetting::new(#gba_tile_id, #vflip, #hflip, #palette_id) } + } + None => { + quote! { TileSetting::new(1023, false, false, 0) } + } + } + }); + + quote! {&[#(#map_tiles),*]} +} + +fn export_ui_tiles(map: &tiled::Map, background: TokenStream) -> TokenStream { + let map_tiles = map.get_layer(0).unwrap().as_tile_layer().unwrap(); + let width = map_tiles.width().unwrap(); let height = map_tiles.height().unwrap(); diff --git a/examples/the-dungeon-puzzlers-lament/maps/level16.tsx b/examples/the-dungeon-puzzlers-lament/maps/level16.tsx new file mode 100644 index 00000000..4c905d7c --- /dev/null +++ b/examples/the-dungeon-puzzlers-lament/maps/level16.tsx @@ -0,0 +1,96 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/examples/the-dungeon-puzzlers-lament/maps/levels16/level1.tmx b/examples/the-dungeon-puzzlers-lament/maps/levels16/level1.tmx new file mode 100644 index 00000000..2db372a0 --- /dev/null +++ b/examples/the-dungeon-puzzlers-lament/maps/levels16/level1.tmx @@ -0,0 +1,34 @@ + + + + + + + + + + +0,0,0,0,0,0,0,0,0,0,0, +0,0,0,0,0,0,0,0,0,0,0, +0,0,0,0,0,0,0,0,0,0,0, +0,1,3,4,3,8,6,4,7,9,0, +0,46,11,12,12,14,16,17,14,18,0, +0,19,20,23,20,26,20,21,26,27,0, +0,0,0,0,0,0,0,0,0,0,0, +0,0,0,0,0,0,0,0,0,0,0, +0,0,0,0,0,0,0,0,0,0,0, +0,0,0,0,0,0,0,0,0,0,0 + + + + + + + + + + + + + +