-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(unstable): animation asset loader (#61)
- Loading branch information
Showing
7 changed files
with
151 additions
and
32 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
mode: repeat | ||
frames: | ||
- index: 0 | ||
duration: 100 | ||
- index: 1 | ||
duration: 100 | ||
- index: 2 | ||
duration: 100 | ||
- index: 3 | ||
duration: 100 | ||
- index: 4 | ||
duration: 100 |
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,41 @@ | ||
use bevy::prelude::*; | ||
|
||
use benimator::*; | ||
|
||
fn main() { | ||
App::new() | ||
.add_plugins(DefaultPlugins) | ||
.add_plugin(AnimationPlugin::default()) // <-- Add the plugin | ||
.add_startup_system(spawn) | ||
.run(); | ||
} | ||
|
||
fn spawn( | ||
mut commands: Commands, | ||
asset_server: Res<AssetServer>, | ||
mut textures: ResMut<Assets<TextureAtlas>>, | ||
) { | ||
// Don't forget the camera ;-) | ||
commands.spawn_bundle(OrthographicCameraBundle::new_2d()); | ||
|
||
// Create an animation | ||
// Here we use an index-range (from 0 to 4) where each frame has the same duration | ||
let animation_handle: Handle<SpriteSheetAnimation> = asset_server.load("coin.animation.yml"); | ||
|
||
commands | ||
// Spawn a bevy sprite-sheet | ||
.spawn_bundle(SpriteSheetBundle { | ||
texture_atlas: textures.add(TextureAtlas::from_grid( | ||
asset_server.load("coin.png"), | ||
Vec2::new(16.0, 16.0), | ||
5, | ||
1, | ||
)), | ||
transform: Transform::from_scale(Vec3::splat(10.0)), | ||
..Default::default() | ||
}) | ||
// Insert the asset handle of the animation | ||
.insert(animation_handle) | ||
// Start the animation immediately. Remove this component in order to pause the animation. | ||
.insert(Play); | ||
} |
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,24 @@ | ||
use super::SpriteSheetAnimation; | ||
use bevy_asset::{AssetLoader, LoadContext, LoadedAsset}; | ||
use bevy_utils::BoxedFuture; | ||
|
||
#[derive(Debug, Default)] | ||
pub(crate) struct SpriteSheetAnimationLoader; | ||
|
||
impl AssetLoader for SpriteSheetAnimationLoader { | ||
fn load<'a>( | ||
&'a self, | ||
bytes: &'a [u8], | ||
load_context: &'a mut LoadContext<'_>, | ||
) -> BoxedFuture<'a, Result<(), anyhow::Error>> { | ||
Box::pin(async move { | ||
let custom_asset = SpriteSheetAnimation::from_yaml_bytes(bytes)?; | ||
load_context.set_default_asset(LoadedAsset::new(custom_asset)); | ||
Ok(()) | ||
}) | ||
} | ||
|
||
fn extensions(&self) -> &[&str] { | ||
&["animation.yml", "animation.yaml"] | ||
} | ||
} |
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