forked from jcornaz/benimator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bevy.rs
66 lines (57 loc) · 1.81 KB
/
bevy.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
use bevy::{prelude::*, render::texture::ImageSettings};
use benimator::FrameRate;
// Create the animation component
// Note: you may make the animation an asset instead of a component
#[derive(Component, Deref)]
struct Animation(benimator::Animation);
// Create the player component
#[derive(Default, Component, Deref, DerefMut)]
struct AnimationState(benimator::State);
fn main() {
App::new()
.insert_resource(ImageSettings::default_nearest())
.add_plugins(DefaultPlugins)
.add_startup_system(spawn)
.add_system(animate)
.run();
}
fn spawn(
mut commands: Commands,
asset_server: Res<AssetServer>,
mut textures: ResMut<Assets<TextureAtlas>>,
) {
// Don't forget the camera ;-)
commands.spawn_bundle(Camera2dBundle::default());
// Create an animation
let animation = Animation(benimator::Animation::from_indices(
0..=4,
FrameRate::from_fps(12.0),
));
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 animation
.insert(animation)
// Insert the state
.insert(AnimationState::default());
}
fn animate(
time: Res<Time>,
mut query: Query<(&mut AnimationState, &mut TextureAtlasSprite, &Animation)>,
) {
for (mut player, mut texture, animation) in query.iter_mut() {
// Update the state
player.update(animation, time.delta());
// Update the texture atlas
texture.index = player.sprite_frame_index();
}
}