-
Notifications
You must be signed in to change notification settings - Fork 22
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
1 parent
7618bc5
commit c444890
Showing
7 changed files
with
193 additions
and
4 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
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,6 @@ | ||
[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,74 @@ | ||
use bevy::prelude::*; | ||
use bevy::reflect::TypePath; | ||
use bevy_common_assets::jsonl::JsonLinesAssetPlugin; | ||
|
||
fn main() { | ||
App::new() | ||
.add_plugins(( | ||
DefaultPlugins, | ||
JsonLinesAssetPlugin::<Level, TreePosition>::new(&["level.jsonl"]), | ||
)) | ||
.insert_resource(Msaa::Off) | ||
.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.jsonl")); | ||
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>, | ||
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(SpriteBundle { | ||
transform: Transform::from_translation(position.0.into()), | ||
texture: tree.0.clone(), | ||
..default() | ||
}); | ||
} | ||
|
||
state.set(AppState::Level); | ||
} | ||
} | ||
|
||
#[derive(serde::Deserialize)] | ||
#[serde(transparent)] | ||
struct TreePosition([f32; 3]); | ||
|
||
#[derive(serde::Deserialize, Asset, TypePath)] | ||
struct Level { | ||
positions: Vec<TreePosition>, | ||
} | ||
|
||
impl FromIterator<TreePosition> for Level { | ||
fn from_iter<T: IntoIterator<Item = TreePosition>>(iter: T) -> Self { | ||
let positions = Vec::from_iter(iter); | ||
Self { positions } | ||
} | ||
} | ||
|
||
#[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>); |
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,93 @@ | ||
use bevy::app::{App, Plugin}; | ||
use bevy::asset::io::Reader; | ||
use bevy::asset::{Asset, AssetApp, AssetLoader, AsyncReadExt, LoadContext}; | ||
use serde_json::from_str; | ||
use std::io::BufRead; | ||
use std::marker::PhantomData; | ||
use thiserror::Error; | ||
|
||
/// Plugin to load your asset type `A` via a list of ` from jsonl files. | ||
pub struct JsonLinesAssetPlugin<A, D> { | ||
extensions: Vec<&'static str>, | ||
_marker: PhantomData<(A, D)>, | ||
} | ||
|
||
impl<A, D> Plugin for JsonLinesAssetPlugin<A, D> | ||
where | ||
for<'de> D: serde::Deserialize<'de> + Sync + Send + 'de, | ||
for<'de> A: FromIterator<D> + Asset + Sync + Send + 'de, | ||
{ | ||
fn build(&self, app: &mut App) { | ||
app.init_asset::<A>() | ||
.register_asset_loader(JsonLinesAssetLoader::<A, D> { | ||
extensions: self.extensions.clone(), | ||
_marker: PhantomData, | ||
}); | ||
} | ||
} | ||
|
||
impl<A, D> JsonLinesAssetPlugin<A, D> | ||
where | ||
for<'de> D: serde::Deserialize<'de> + Sync + Send + 'de, | ||
for<'de> A: FromIterator<D> + Asset + Sync + Send + 'de, | ||
{ | ||
/// 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, | ||
} | ||
} | ||
} | ||
|
||
/// Loads your asset type `A` from jsonl files | ||
pub struct JsonLinesAssetLoader<A, D> { | ||
extensions: Vec<&'static str>, | ||
_marker: PhantomData<(A, D)>, | ||
} | ||
|
||
/// Possible errors that can be produced by [`JsonLinesAssetLoader`] | ||
#[non_exhaustive] | ||
#[derive(Debug, Error)] | ||
pub enum JsonLinesLoaderError { | ||
/// An [IO Error](std::io::Error) | ||
#[error("Could not read the file: {0}")] | ||
Io(#[from] std::io::Error), | ||
/// A [JSON Error](serde_json::error::Error) | ||
#[error("Could not parse the JSON: {0}")] | ||
JsonError(#[from] serde_json::error::Error), | ||
} | ||
|
||
impl<A, D> AssetLoader for JsonLinesAssetLoader<A, D> | ||
where | ||
for<'de> D: serde::Deserialize<'de> + Sync + Send + 'de, | ||
for<'de> A: FromIterator<D> + Asset + Sync + Send + 'de, | ||
{ | ||
type Asset = A; | ||
type Settings = (); | ||
type Error = JsonLinesLoaderError; | ||
|
||
async fn load<'a>( | ||
&'a self, | ||
reader: &'a mut Reader<'_>, | ||
_settings: &'a (), | ||
_load_context: &'a mut LoadContext<'_>, | ||
) -> Result<Self::Asset, Self::Error> { | ||
let mut bytes = Vec::new(); | ||
reader.read_to_end(&mut bytes).await?; | ||
|
||
if let Some(b'\n') = bytes.last() { | ||
// Json Lines may optionally end with a line break. | ||
let _ = bytes.pop(); | ||
} | ||
|
||
bytes | ||
.lines() | ||
.map(|line| Ok::<D, JsonLinesLoaderError>(from_str(line?.as_str())?)) | ||
.collect() | ||
} | ||
|
||
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