diff --git a/src/assets/kenney_racing-pack b/src/assets/kenney_racing-pack new file mode 120000 index 0000000..995c458 --- /dev/null +++ b/src/assets/kenney_racing-pack @@ -0,0 +1 @@ +../../assets/kenney_racing-pack/ \ No newline at end of file diff --git a/src/assets/level1.tmx b/src/assets/level1.tmx new file mode 120000 index 0000000..c3e12e7 --- /dev/null +++ b/src/assets/level1.tmx @@ -0,0 +1 @@ +../../assets/level1.tmx \ No newline at end of file diff --git a/src/assets/mod.rs b/src/assets/mod.rs new file mode 100644 index 0000000..907cbed --- /dev/null +++ b/src/assets/mod.rs @@ -0,0 +1,108 @@ +// SPDX-License-Identifier: GPL-3.0-or-later +// Copyright (C) 2024 Daniel Thompson + +use bevy::asset::embedded_asset; + +#[derive(Default)] +pub struct Plugin; + +impl bevy::prelude::Plugin for Plugin { + fn build(&self, app: &mut bevy::prelude::App) { + embedded_asset!(app, "src/", "level1.tmx"); + + embedded_asset!(app, "src/", "kenney_racing-pack/PNG/Cars/car_red_5.png"); + embedded_asset!(app, "src/", "kenney_racing-pack/PNG/Cars/car_blue_1.png"); + embedded_asset!(app, "src/", "kenney_racing-pack/PNG/Cars/car_yellow_3.png"); + embedded_asset!(app, "src/", "kenney_racing-pack/PNG/Cars/car_green_4.png"); + embedded_asset!( + app, + "src/", + "kenney_racing-pack/Spritesheets/spritesheet_tiles.png" + ); + embedded_asset!( + app, + "src/", + "kenney_racing-pack/Spritesheets/spritesheet_tiles.png" + ); + embedded_asset!( + app, + "src/", + "kenney_racing-pack/Spritesheets/spritesheet_tiles.png" + ); + embedded_asset!( + app, + "src/", + "kenney_racing-pack/Spritesheets/spritesheet_tiles.png" + ); + embedded_asset!( + app, + "src/", + "kenney_racing-pack/PNG/Tiles/Grass/land_grass01.png" + ); + embedded_asset!( + app, + "src/", + "kenney_racing-pack/PNG/Tiles/Grass/land_grass02.png" + ); + embedded_asset!( + app, + "src/", + "kenney_racing-pack/PNG/Tiles/Grass/land_grass03.png" + ); + embedded_asset!( + app, + "src/", + "kenney_racing-pack/PNG/Tiles/Grass/land_grass04.png" + ); + embedded_asset!( + app, + "src/", + "kenney_racing-pack/PNG/Tiles/Grass/land_grass05.png" + ); + embedded_asset!( + app, + "src/", + "kenney_racing-pack/PNG/Tiles/Grass/land_grass06.png" + ); + embedded_asset!( + app, + "src/", + "kenney_racing-pack/PNG/Tiles/Grass/land_grass07.png" + ); + embedded_asset!( + app, + "src/", + "kenney_racing-pack/PNG/Tiles/Grass/land_grass08.png" + ); + embedded_asset!( + app, + "src/", + "kenney_racing-pack/PNG/Tiles/Grass/land_grass09.png" + ); + embedded_asset!( + app, + "src/", + "kenney_racing-pack/PNG/Tiles/Grass/land_grass10.png" + ); + embedded_asset!( + app, + "src/", + "kenney_racing-pack/PNG/Tiles/Grass/land_grass11.png" + ); + embedded_asset!( + app, + "src/", + "kenney_racing-pack/PNG/Tiles/Grass/land_grass12.png" + ); + embedded_asset!( + app, + "src/", + "kenney_racing-pack/PNG/Tiles/Grass/land_grass13.png" + ); + embedded_asset!( + app, + "src/", + "kenney_racing-pack/PNG/Tiles/Grass/land_grass14.png" + ); + } +} diff --git a/src/helpers/tiled.rs b/src/helpers/tiled.rs index 213ccaa..bb72f95 100644 --- a/src/helpers/tiled.rs +++ b/src/helpers/tiled.rs @@ -45,11 +45,7 @@ impl Plugin for TiledMapPlugin { #[derive(TypePath, Asset, Debug)] pub struct TiledMap { pub map: tiled::Map, - pub tilemap_textures: HashMap, - - // The offset into the tileset_images for each tile id within each tileset. - #[cfg(not(feature = "atlas"))] pub tile_image_offsets: HashMap<(usize, tiled::TileId), u32>, } @@ -128,46 +124,32 @@ impl AssetLoader for TiledLoader { for (tileset_index, tileset) in map.tilesets().iter().enumerate() { let tilemap_texture = match &tileset.image { None => { - #[cfg(feature = "atlas")] - { - log::info!("Skipping image collection tileset '{}' which is incompatible with atlas feature", tileset.name); - continue; - } - - #[cfg(not(feature = "atlas"))] - { - let mut tile_images: Vec> = Vec::new(); - for (tile_id, tile) in tileset.tiles() { - if let Some(img) = &tile.image { - // The load context path is the TMX file itself. If the file is at the root of the - // assets/ directory structure then the tmx_dir will be empty, which is fine. - let tmx_dir = load_context - .path() - .parent() - .expect("The asset load context was empty."); - let tile_path = tmx_dir.join(&img.source); - let asset_path = AssetPath::from(tile_path); - log::info!("Loading tile image from {asset_path:?} as image ({tileset_index}, {tile_id})"); - let texture: Handle = - load_context.load(asset_path.clone()); - tile_image_offsets - .insert((tileset_index, tile_id), tile_images.len() as u32); - tile_images.push(texture.clone()); - } + let mut tile_images: Vec> = Vec::new(); + for (tile_id, tile) in tileset.tiles() { + if let Some(img) = &tile.image { + // The load context path is the TMX file itself. If the file is at the root of the + // assets/ directory structure then the tmx_dir will be empty, which is fine. + let tmx_dir = std::path::PathBuf::from("embedded://"); + let tile_path = tmx_dir.join(&img.source); + let asset_path = AssetPath::from(tile_path.to_str().expect("tile_path is not UTF-8").to_string()); + //let asset_path = AssetPath::from(tile_path); + log::info!("Loading tile image from {asset_path:?} as image ({tileset_index}, {tile_id})"); + let texture: Handle = load_context.load(asset_path.clone()); + tile_image_offsets + .insert((tileset_index, tile_id), tile_images.len() as u32); + tile_images.push(texture.clone()); } - - TilemapTexture::Vector(tile_images) } + + TilemapTexture::Vector(tile_images) } Some(img) => { // The load context path is the TMX file itself. If the file is at the root of the // assets/ directory structure then the tmx_dir will be empty, which is fine. - let tmx_dir = load_context - .path() - .parent() - .expect("The asset load context was empty."); + let tmx_dir = std::path::PathBuf::from("embedded://"); let tile_path = tmx_dir.join(&img.source); - let asset_path = AssetPath::from(tile_path); + let asset_path = AssetPath::from(tile_path.to_str().expect("tile_path is not UTF-8").to_string()); + log::info!("Loading tile image from {asset_path:?}"); let texture: Handle = load_context.load(asset_path.clone()); TilemapTexture::Single(texture.clone()) @@ -180,7 +162,6 @@ impl AssetLoader for TiledLoader { let asset_map = TiledMap { map, tilemap_textures, - #[cfg(not(feature = "atlas"))] tile_image_offsets, }; @@ -342,11 +323,9 @@ pub fn process_loaded_maps( let texture_index = match tilemap_texture { TilemapTexture::Single(_) => layer_tile.id(), - #[cfg(not(feature = "atlas"))] TilemapTexture::Vector(_) => *tiled_map.tile_image_offsets.get(&(tileset_index, layer_tile.id())) .expect("The offset into to image vector should have been saved during the initial load."), - #[cfg(not(feature = "atlas"))] _ => unreachable!() }; diff --git a/src/main.rs b/src/main.rs index 3133814..6b1eeab 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,6 +12,7 @@ use itertools::Itertools; use slicetools::*; use std::f32::consts::PI; +mod assets; mod helpers; mod util; use util::IteratorToArrayExt; @@ -30,6 +31,7 @@ fn main() { }), helpers::editor::Plugin, TilemapPlugin, + assets::Plugin, helpers::tiled::TiledMapPlugin, )) .register_type::() @@ -95,7 +97,8 @@ fn spawn_camera(mut commands: Commands) { } fn load_maps(mut commands: Commands, asset_server: Res) { - let map_handle: Handle = asset_server.load("level1.tmx"); + let map_handle: Handle = + asset_server.load("embedded://tdr2024/assets/level1.tmx"); commands.spawn(helpers::tiled::TiledMapBundle { tiled_map: map_handle, @@ -194,7 +197,7 @@ fn spawn_player( asset_server: Res, ) { let atlas = TextureAtlas::from_grid( - asset_server.load("kenney_racing-pack/PNG/Cars/car_red_5.png"), + asset_server.load("embedded://tdr2024/assets/kenney_racing-pack/PNG/Cars/car_red_5.png"), Vec2::new(70., 121.), 1, 1, @@ -224,7 +227,8 @@ fn spawn_ai_players( mut texture_atlas: ResMut>, asset_server: Res, ) { - let handle = asset_server.load("kenney_racing-pack/PNG/Cars/car_blue_1.png"); + let handle = + asset_server.load("embedded://tdr2024/assets/kenney_racing-pack/PNG/Cars/car_blue_1.png"); let atlas = TextureAtlas::from_grid(handle, Vec2::new(70., 121.), 1, 1, None, None); commands.spawn(( @@ -242,7 +246,8 @@ fn spawn_ai_players( }, )); - let handle = asset_server.load("kenney_racing-pack/PNG/Cars/car_yellow_3.png"); + let handle = + asset_server.load("embedded://tdr2024/assets/kenney_racing-pack/PNG/Cars/car_yellow_3.png"); let atlas = TextureAtlas::from_grid(handle, Vec2::new(70., 121.), 1, 1, None, None); commands.spawn(( Racer, @@ -259,7 +264,8 @@ fn spawn_ai_players( }, )); - let handle = asset_server.load("kenney_racing-pack/PNG/Cars/car_green_4.png"); + let handle = + asset_server.load("embedded://tdr2024/assets/kenney_racing-pack/PNG/Cars/car_green_4.png"); let atlas = TextureAtlas::from_grid(handle, Vec2::new(70., 121.), 1, 1, None, None); commands.spawn(( Racer, @@ -508,5 +514,6 @@ fn track_player( for (mut txc, _, _) in camera.iter_mut() { txc.translation.x = txp.translation.x; txc.translation.y = txp.translation.y; + //txc.rotation = txp.rotation; } }