level 1 implemented

This commit is contained in:
Gwilym Inzani 2023-07-25 22:23:32 +01:00
parent b80c85f1d9
commit 3bd5ae2313
3 changed files with 193 additions and 21 deletions

View file

@ -10,24 +10,24 @@ use proc_macro2::TokenStream;
const LEVEL_NAMES: &[&str] = &[ const LEVEL_NAMES: &[&str] = &[
"level1", "level1",
"level2", // "level2",
"level3", // "level3",
"level4", // "level4",
"level5", // "level5",
"level6", // "level6",
"level_switch", // "level_switch",
"level_spikes", // "level_spikes",
"level_spikes2", // "level_spikes2",
"squid_force_button", // "squid_force_button",
"level_squid_intro", // "level_squid_intro",
"level_squid2", // "level_squid2",
"level_squid1", // "level_squid1",
"level_squid_item", // "level_squid_item",
"level_squid_button", // "level_squid_button",
"level_squid_drop", // "level_squid_drop",
"level_spikes3", // "level_spikes3",
"level_around", // "level_around",
"level_squidprogramming", // "level_squidprogramming",
]; ];
fn main() { fn main() {
@ -36,11 +36,11 @@ fn main() {
let mut tile_loader = tiled::Loader::new(); let mut tile_loader = tiled::Loader::new();
let ui_map = load_tmx(&mut tile_loader, "maps/UI.tmx"); 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 let levels = LEVEL_NAMES
.iter() .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::<Vec<_>>(); .collect::<Vec<_>>();
let levels_tiles = levels.iter().map(|level| &level.0); let levels_tiles = levels.iter().map(|level| &level.0);
let levels_data = levels.iter().map(|level| &level.1); 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 tile_y = id / 11;
let is_wall = tiles let is_wall = tiles
.get_tile(tile_x * 2, tile_y * 2) .get_tile(tile_x, tile_y)
.map(|tile| { .map(|tile| {
let tileset = tile.get_tileset(); let tileset = tile.get_tileset();
let tile_data = &tileset.get_tile(tile.id()).unwrap(); 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 { fn export_tiles(map: &tiled::Map, background: TokenStream) -> TokenStream {
let map_tiles = map.get_layer(0).unwrap().as_tile_layer().unwrap(); 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 width = map_tiles.width().unwrap();
let height = map_tiles.height().unwrap(); let height = map_tiles.height().unwrap();

View file

@ -0,0 +1,96 @@
<?xml version="1.0" encoding="UTF-8"?>
<tileset version="1.10" tiledversion="1.10.1" name="level16" tilewidth="16" tileheight="16" tilecount="81" columns="9">
<transformations hflip="1" vflip="1" rotate="0" preferuntransformed="1"/>
<image source="level.png" width="144" height="144"/>
<tile id="0" type="WALL"/>
<tile id="1" type="WALL"/>
<tile id="2" type="WALL"/>
<tile id="3" type="WALL"/>
<tile id="4" type="WALL"/>
<tile id="5" type="WALL"/>
<tile id="6" type="WALL"/>
<tile id="7" type="WALL"/>
<tile id="8" type="WALL"/>
<tile id="9" type="WALL"/>
<tile id="10" type="FLOOR"/>
<tile id="11" type="FLOOR"/>
<tile id="12" type="FLOOR"/>
<tile id="13" type="FLOOR"/>
<tile id="14" type="FLOOR"/>
<tile id="15" type="FLOOR"/>
<tile id="16" type="FLOOR"/>
<tile id="17" type="WALL"/>
<tile id="18" type="WALL"/>
<tile id="19" type="WALL"/>
<tile id="20" type="WALL"/>
<tile id="21" type="WALL"/>
<tile id="22" type="WALL"/>
<tile id="23" type="WALL"/>
<tile id="24" type="WALL"/>
<tile id="25" type="WALL"/>
<tile id="26" type="WALL"/>
<tile id="27" type="WALL"/>
<tile id="28" type="WALL"/>
<tile id="29" type="WALL"/>
<tile id="30" type="WALL"/>
<tile id="31" type="WALL"/>
<tile id="32" type="WALL"/>
<tile id="36" type="WALL"/>
<tile id="37" type="WALL"/>
<tile id="38" type="WALL"/>
<tile id="39" type="WALL"/>
<tile id="40" type="WALL"/>
<tile id="45" type="WALL"/>
<tile id="46" type="WALL"/>
<tile id="47" type="WALL"/>
<tile id="48" type="WALL"/>
<tile id="49" type="WALL"/>
<wangsets>
<wangset name="Ground" type="mixed" tile="-1">
<wangcolor name="Wall" color="#ff0000" tile="-1" probability="1"/>
<wangcolor name="Floor" color="#00ff00" tile="-1" probability="1"/>
<wangtile tileid="0" wangid="0,0,1,2,1,0,0,0"/>
<wangtile tileid="1" wangid="0,0,1,2,2,2,1,0"/>
<wangtile tileid="2" wangid="0,0,1,2,2,2,1,0"/>
<wangtile tileid="3" wangid="0,0,1,2,2,2,1,0"/>
<wangtile tileid="4" wangid="0,0,1,2,2,2,1,0"/>
<wangtile tileid="5" wangid="0,0,1,2,2,2,1,0"/>
<wangtile tileid="6" wangid="0,0,1,2,2,2,1,0"/>
<wangtile tileid="7" wangid="0,0,1,2,2,2,1,0"/>
<wangtile tileid="8" wangid="0,0,0,0,1,2,1,0"/>
<wangtile tileid="9" wangid="1,2,2,2,1,0,0,0"/>
<wangtile tileid="10" wangid="2,2,2,2,2,2,2,2"/>
<wangtile tileid="11" wangid="2,2,2,2,2,2,2,2"/>
<wangtile tileid="12" wangid="2,2,2,2,2,2,2,2"/>
<wangtile tileid="13" wangid="2,2,2,2,2,2,2,2"/>
<wangtile tileid="14" wangid="2,2,2,2,2,2,2,2"/>
<wangtile tileid="15" wangid="2,2,2,2,2,2,2,2"/>
<wangtile tileid="16" wangid="2,2,2,2,2,2,2,2"/>
<wangtile tileid="17" wangid="1,0,0,0,1,2,2,2"/>
<wangtile tileid="18" wangid="1,2,1,0,0,0,0,0"/>
<wangtile tileid="19" wangid="2,2,1,0,0,0,1,2"/>
<wangtile tileid="20" wangid="2,2,1,0,0,0,1,2"/>
<wangtile tileid="21" wangid="2,2,1,0,0,0,1,2"/>
<wangtile tileid="22" wangid="2,2,1,0,0,0,1,2"/>
<wangtile tileid="23" wangid="2,2,1,0,0,0,1,2"/>
<wangtile tileid="24" wangid="2,2,1,0,0,0,1,2"/>
<wangtile tileid="25" wangid="2,2,1,0,0,0,1,2"/>
<wangtile tileid="26" wangid="1,0,0,0,0,0,1,2"/>
<wangtile tileid="27" wangid="2,2,2,2,1,0,1,2"/>
<wangtile tileid="28" wangid="2,2,1,2,2,2,2,2"/>
<wangtile tileid="29" wangid="1,0,0,0,1,2,1,2"/>
<wangtile tileid="31" wangid="1,2,2,2,1,2,1,0"/>
<wangtile tileid="32" wangid="1,2,2,2,2,2,1,2"/>
<wangtile tileid="36" wangid="1,2,2,2,1,0,0,0"/>
<wangtile tileid="37" wangid="1,0,0,0,1,2,2,2"/>
<wangtile tileid="38" wangid="2,2,2,2,1,2,2,2"/>
<wangtile tileid="39" wangid="2,2,1,1,1,2,2,2"/>
<wangtile tileid="40" wangid="1,2,2,2,1,1,1,2"/>
<wangtile tileid="45" wangid="1,2,2,2,1,0,0,0"/>
<wangtile tileid="46" wangid="1,0,0,0,1,2,2,2"/>
<wangtile tileid="47" wangid="1,2,1,0,0,0,1,2"/>
<wangtile tileid="48" wangid="1,1,1,0,0,0,1,2"/>
<wangtile tileid="49" wangid="1,2,2,2,1,0,1,1"/>
</wangset>
</wangsets>
</tileset>

View file

@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.10" tiledversion="1.10.1" orientation="orthogonal" renderorder="right-down" width="11" height="10" tilewidth="16" tileheight="16" infinite="0" nextlayerid="3" nextobjectid="4">
<properties>
<property name="DIRECTIONS" value="RRRRRR"/>
<property name="ITEMS" value="KEY"/>
<property name="NAME" value="Keys open doors"/>
</properties>
<tileset firstgid="1" source="../level16.tsx"/>
<layer id="1" name="Tile Layer 1" width="11" height="10">
<data encoding="csv">
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
</data>
</layer>
<objectgroup id="2" name="Object Layer 1">
<object id="1" name="STAIRS" x="134.443" y="72.9947">
<point/>
</object>
<object id="2" name="HERO" x="56.2387" y="72.0629">
<point/>
</object>
<object id="3" name="DOOR" x="103.261" y="72.1311">
<point/>
</object>
</objectgroup>
</map>