-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP asset server example (the wrong loader is currently used for the …
…processed asset)
- Loading branch information
Showing
7 changed files
with
136 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
target/ | ||
Cargo.lock | ||
.idea/ | ||
|
||
examples/asset_savers/imported_assets |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
use bevy::asset::processor::LoadTransformAndSave; | ||
use bevy::asset::transformer::IdentityAssetTransformer; | ||
use bevy::prelude::*; | ||
use bevy_common_assets::json::{JsonAssetLoader, JsonAssetPlugin}; | ||
use bevy_common_assets::postcard::{PostcardAssetPlugin, PostcardAssetSaver}; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
fn main() { | ||
App::new() | ||
.add_plugins(( | ||
DefaultPlugins.set(AssetPlugin { | ||
mode: AssetMode::Processed, | ||
file_path: "examples/asset_savers/assets".to_string(), | ||
processed_file_path: "examples/asset_savers/imported_assets/Default".to_string(), | ||
..default() | ||
}), | ||
PostcardAssetPlugin::<Level>::new(&["level"]), | ||
JsonAssetPlugin::<Level>::new(&[]), | ||
)) | ||
.register_asset_processor::<LoadTransformAndSave< | ||
JsonAssetLoader<Level>, | ||
IdentityAssetTransformer<Level>, | ||
PostcardAssetSaver<Level>, | ||
>>(LoadTransformAndSave::new( | ||
IdentityAssetTransformer::<Level>::default(), | ||
PostcardAssetSaver::<Level>::default(), | ||
)) | ||
.init_state::<AppState>() | ||
.add_systems(Startup, setup) | ||
.add_systems(Update, spawn_level.run_if(in_state(AppState::Loading))) | ||
.run(); | ||
} | ||
|
||
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) { | ||
let level = LevelHandle(asset_server.load("trees.level")); | ||
commands.insert_resource(level); | ||
let tree = ImageHandle(asset_server.load("tree.png")); | ||
commands.insert_resource(tree); | ||
commands.spawn((Camera2d, Msaa::Off)); | ||
} | ||
|
||
fn spawn_level( | ||
mut commands: Commands, | ||
level: Res<LevelHandle>, | ||
tree: Res<ImageHandle>, | ||
mut levels: ResMut<Assets<Level>>, | ||
mut state: ResMut<NextState<AppState>>, | ||
) { | ||
if let Some(level) = levels.remove(level.0.id()) { | ||
for position in level.positions { | ||
commands.spawn(( | ||
Sprite::from_image(tree.0.clone()), | ||
Transform::from_translation(position.into()), | ||
)); | ||
} | ||
state.set(AppState::Level); | ||
} | ||
} | ||
|
||
#[derive(Debug, Clone, Copy, Default, Eq, PartialEq, Hash, States)] | ||
enum AppState { | ||
#[default] | ||
Loading, | ||
Level, | ||
} | ||
|
||
#[derive(Resource)] | ||
struct ImageHandle(Handle<Image>); | ||
|
||
#[derive(Resource)] | ||
struct LevelHandle(Handle<Level>); | ||
|
||
#[derive(Deserialize, Serialize, Asset, TypePath)] | ||
struct Level { | ||
positions: Vec<[f32; 3]>, | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
( | ||
meta_format_version: "1.0", | ||
asset: Load( | ||
loader: "bevy_image::image_loader::ImageLoader", | ||
settings: ( | ||
format: FromExtension, | ||
is_srgb: true, | ||
sampler: Default, | ||
asset_usage: ("MAIN_WORLD | RENDER_WORLD"), | ||
), | ||
), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
{ | ||
"positions": [ | ||
[ | ||
42.0, | ||
42.0, | ||
0.0 | ||
], | ||
[ | ||
4.0, | ||
32.0, | ||
0.0 | ||
], | ||
[ | ||
54.0, | ||
7.0, | ||
0.0 | ||
], | ||
[ | ||
-61.0, | ||
4.0, | ||
0.0 | ||
], | ||
[ | ||
-6.0, | ||
-72.0, | ||
0.0 | ||
], | ||
[ | ||
6.0, | ||
-89.0, | ||
0.0 | ||
] | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
( | ||
meta_format_version: "1.0", | ||
asset: Load( | ||
loader: "bevy_common_assets::json::JsonAssetLoader<asset_savers::Level>", | ||
settings: (), | ||
), | ||
) |