Skip to content

Commit

Permalink
Embedded the game assets in the binary
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-thompson committed Jan 7, 2024
1 parent dc11211 commit 4fac60e
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 45 deletions.
1 change: 1 addition & 0 deletions src/assets/kenney_racing-pack
1 change: 1 addition & 0 deletions src/assets/level1.tmx
108 changes: 108 additions & 0 deletions src/assets/mod.rs
Original file line number Diff line number Diff line change
@@ -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"
);
}
}
59 changes: 19 additions & 40 deletions src/helpers/tiled.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,7 @@ impl Plugin for TiledMapPlugin {
#[derive(TypePath, Asset, Debug)]
pub struct TiledMap {
pub map: tiled::Map,

pub tilemap_textures: HashMap<usize, TilemapTexture>,

// 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>,
}

Expand Down Expand Up @@ -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<Handle<Image>> = 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<Image> =
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<Handle<Image>> = 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<Image> = 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<Image> = load_context.load(asset_path.clone());

TilemapTexture::Single(texture.clone())
Expand All @@ -180,7 +162,6 @@ impl AssetLoader for TiledLoader {
let asset_map = TiledMap {
map,
tilemap_textures,
#[cfg(not(feature = "atlas"))]
tile_image_offsets,
};

Expand Down Expand Up @@ -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!()
};

Expand Down
17 changes: 12 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use itertools::Itertools;
use slicetools::*;
use std::f32::consts::PI;

mod assets;
mod helpers;
mod util;
use util::IteratorToArrayExt;
Expand All @@ -30,6 +31,7 @@ fn main() {
}),
helpers::editor::Plugin,
TilemapPlugin,
assets::Plugin,
helpers::tiled::TiledMapPlugin,
))
.register_type::<Angle>()
Expand Down Expand Up @@ -95,7 +97,8 @@ fn spawn_camera(mut commands: Commands) {
}

fn load_maps(mut commands: Commands, asset_server: Res<AssetServer>) {
let map_handle: Handle<helpers::tiled::TiledMap> = asset_server.load("level1.tmx");
let map_handle: Handle<helpers::tiled::TiledMap> =
asset_server.load("embedded://tdr2024/assets/level1.tmx");

commands.spawn(helpers::tiled::TiledMapBundle {
tiled_map: map_handle,
Expand Down Expand Up @@ -194,7 +197,7 @@ fn spawn_player(
asset_server: Res<AssetServer>,
) {
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,
Expand Down Expand Up @@ -224,7 +227,8 @@ fn spawn_ai_players(
mut texture_atlas: ResMut<Assets<TextureAtlas>>,
asset_server: Res<AssetServer>,
) {
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((
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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;
}
}

0 comments on commit 4fac60e

Please sign in to comment.