-
Notifications
You must be signed in to change notification settings - Fork 3
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
9 changed files
with
405 additions
and
203 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,91 @@ | ||
// This example illustrates how to control the state of an animated sprite. | ||
|
||
#[path = "./common/mod.rs"] | ||
pub mod common; | ||
|
||
use bevy::prelude::*; | ||
use bevy_spritesheet_animation::{ | ||
components::spritesheet_animation::AnimationProgress, prelude::*, | ||
}; | ||
|
||
fn main() { | ||
App::new() | ||
.add_plugins(DefaultPlugins) | ||
.add_plugins(SpritesheetAnimationPlugin) | ||
.add_systems(Startup, setup) | ||
.add_systems(Update, update) | ||
.run(); | ||
} | ||
|
||
fn setup( | ||
mut commands: Commands, | ||
mut library: ResMut<SpritesheetLibrary>, | ||
mut atlas_layouts: ResMut<Assets<TextureAtlasLayout>>, | ||
assets: Res<AssetServer>, | ||
) { | ||
commands.spawn(Camera2dBundle::default()); | ||
|
||
let texture = assets.load("character.png"); | ||
|
||
let layout = atlas_layouts.add(TextureAtlasLayout::from_grid( | ||
UVec2::new(96, 96), | ||
8, | ||
8, | ||
None, | ||
None, | ||
)); | ||
|
||
let clip_id = library.new_clip(|clip| { | ||
clip.push_frame_indices(Spritesheet::new(8, 8).positions([(0, 1), (0, 3), (0, 5), (0, 7)])) | ||
.set_default_duration(AnimationDuration::PerFrame(3_000)); | ||
}); | ||
|
||
let animation_id = library.new_animation(|animation| { | ||
animation | ||
.add_stage(clip_id.into()) | ||
.set_repeat(AnimationRepeat::Cycles(1)); | ||
}); | ||
|
||
commands.spawn(( | ||
SpriteBundle { | ||
texture, | ||
..default() | ||
}, | ||
TextureAtlas { | ||
layout, | ||
..default() | ||
}, | ||
SpritesheetAnimation::from_id(animation_id), | ||
)); | ||
} | ||
|
||
fn update(keyboard: Res<ButtonInput<KeyCode>>, mut sprites: Query<&mut SpritesheetAnimation>) { | ||
for mut sprite in &mut sprites { | ||
// Play/Pause | ||
|
||
if keyboard.just_pressed(KeyCode::KeyP) { | ||
sprite.playing = !sprite.playing; | ||
} | ||
|
||
// Reset | ||
|
||
if keyboard.just_pressed(KeyCode::KeyR) { | ||
sprite.reset(); | ||
} | ||
|
||
// Switch to specific frames | ||
|
||
static KEYS: [KeyCode; 4] = [ | ||
KeyCode::Numpad0, | ||
KeyCode::Numpad1, | ||
KeyCode::Numpad2, | ||
KeyCode::Numpad3, | ||
]; | ||
|
||
for (frame, key) in KEYS.iter().enumerate() { | ||
if keyboard.just_pressed(*key) { | ||
sprite.progress.frame = frame; | ||
} | ||
} | ||
} | ||
} |
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
Oops, something went wrong.