-
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.
- Loading branch information
Showing
6 changed files
with
199 additions
and
9 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
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,7 @@ | ||
x,y,z | ||
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,71 @@ | ||
use bevy::asset::RecursiveDependencyLoadState; | ||
use bevy::prelude::*; | ||
use bevy::reflect::TypePath; | ||
use bevy_common_assets::csv::{CsvAssetPlugin, LoadedCsv}; | ||
|
||
fn main() { | ||
App::new() | ||
.add_plugins(( | ||
DefaultPlugins, | ||
CsvAssetPlugin::<TreePosition>::new(&["level.csv"]), | ||
)) | ||
.insert_resource(Msaa::Off) | ||
.add_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.csv")); | ||
commands.insert_resource(level); | ||
let tree = ImageHandle(asset_server.load("tree.png")); | ||
commands.insert_resource(tree); | ||
|
||
commands.spawn(Camera2dBundle::default()); | ||
} | ||
|
||
fn spawn_level( | ||
mut commands: Commands, | ||
level: Res<LevelHandle>, | ||
asset_server: Res<AssetServer>, | ||
tree: Res<ImageHandle>, | ||
mut positios: ResMut<Assets<TreePosition>>, | ||
mut state: ResMut<NextState<AppState>>, | ||
) { | ||
if asset_server.get_recursive_dependency_load_state(&level.0) | ||
== Some(RecursiveDependencyLoadState::Loaded) | ||
{ | ||
for (_, position) in positios.iter() { | ||
commands.spawn(SpriteBundle { | ||
transform: Transform::from_translation(Vec3::new( | ||
position.x, position.y, position.z, | ||
)), | ||
texture: tree.0.clone(), | ||
..default() | ||
}); | ||
} | ||
|
||
state.set(AppState::Level); | ||
} | ||
} | ||
|
||
#[derive(serde::Deserialize, Asset, TypePath, Debug)] | ||
struct TreePosition { | ||
x: f32, | ||
y: f32, | ||
z: f32, | ||
} | ||
|
||
#[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<LoadedCsv<TreePosition>>); |
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,99 @@ | ||
use bevy::app::{App, Plugin}; | ||
use bevy::asset::io::Reader; | ||
use bevy::asset::{Asset, AssetApp, AssetLoader, AsyncReadExt, BoxedFuture, Handle, LoadContext}; | ||
use bevy::prelude::TypePath; | ||
use std::marker::PhantomData; | ||
use thiserror::Error; | ||
|
||
/// Plugin to load your asset type `A` from csv files. | ||
pub struct CsvAssetPlugin<A> { | ||
extensions: Vec<&'static str>, | ||
_marker: PhantomData<A>, | ||
} | ||
|
||
impl<A> Plugin for CsvAssetPlugin<A> | ||
where | ||
for<'de> A: serde::Deserialize<'de> + Asset, | ||
{ | ||
fn build(&self, app: &mut App) { | ||
app.init_asset::<A>() | ||
.init_asset::<LoadedCsv<A>>() | ||
.register_asset_loader(CsvAssetLoader::<A> { | ||
extensions: self.extensions.clone(), | ||
_marker: PhantomData, | ||
}); | ||
} | ||
} | ||
|
||
impl<A> CsvAssetPlugin<A> | ||
where | ||
for<'de> A: serde::Deserialize<'de> + Asset, | ||
{ | ||
/// Create a new plugin that will load assets from files with the given extensions. | ||
pub fn new(extensions: &[&'static str]) -> Self { | ||
Self { | ||
extensions: extensions.to_owned(), | ||
_marker: PhantomData, | ||
} | ||
} | ||
} | ||
|
||
struct CsvAssetLoader<A> { | ||
extensions: Vec<&'static str>, | ||
_marker: PhantomData<A>, | ||
} | ||
|
||
/// Possible errors that can be produced by [`CsvAssetLoader`] | ||
#[non_exhaustive] | ||
#[derive(Debug, Error)] | ||
pub enum CsvLoaderError { | ||
/// An [IO Error](std::io::Error) | ||
#[error("Could not read the file: {0}")] | ||
Io(#[from] std::io::Error), | ||
/// A [CSV Error](serde_csv::Error) | ||
#[error("Could not parse CSV: {0}")] | ||
CsvError(#[from] csv::Error), | ||
} | ||
|
||
/// Asset representing a loaded CSV file with rows deserialized to Assets of type `A` | ||
#[derive(TypePath, Asset)] | ||
pub struct LoadedCsv<A> | ||
where | ||
for<'de> A: serde::Deserialize<'de> + Asset, | ||
{ | ||
/// Handles to the Assets the were loaded from the rows of this CSV file | ||
pub rows: Vec<Handle<A>>, | ||
} | ||
|
||
impl<A> AssetLoader for CsvAssetLoader<A> | ||
where | ||
for<'de> A: serde::Deserialize<'de> + Asset, | ||
{ | ||
type Asset = LoadedCsv<A>; | ||
type Settings = (); | ||
type Error = CsvLoaderError; | ||
|
||
fn load<'a>( | ||
&'a self, | ||
reader: &'a mut Reader, | ||
_settings: &'a (), | ||
load_context: &'a mut LoadContext, | ||
) -> BoxedFuture<'a, Result<Self::Asset, Self::Error>> { | ||
Box::pin(async move { | ||
let mut bytes = Vec::new(); | ||
reader.read_to_end(&mut bytes).await?; | ||
let mut reader = csv::Reader::from_reader(bytes.as_slice()); | ||
let mut handles = vec![]; | ||
for (index, result) in reader.deserialize().enumerate() { | ||
let asset: A = result?; | ||
handles | ||
.push(load_context.add_loaded_labeled_asset(index.to_string(), asset.into())); | ||
} | ||
Ok(LoadedCsv { rows: handles }) | ||
}) | ||
} | ||
|
||
fn extensions(&self) -> &[&str] { | ||
&self.extensions | ||
} | ||
} |
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