forked from bevyengine/bevy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
move_sprite.rs
46 lines (40 loc) · 1.28 KB
/
move_sprite.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
//! Renders a 2D scene containing a single, moving sprite.
use bevy::prelude::*;
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_startup_system(setup)
.add_system(sprite_movement)
.run();
}
#[derive(Component)]
enum Direction {
Up,
Down,
}
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
commands.spawn(Camera2dBundle::default());
commands.spawn((
SpriteBundle {
texture: asset_server.load("branding/icon.png"),
transform: Transform::from_xyz(100., 0., 0.),
..default()
},
Direction::Up,
));
}
/// The sprite is animated by changing its translation depending on the time that has passed since
/// the last frame.
fn sprite_movement(time: Res<Time>, mut sprite_position: Query<(&mut Direction, &mut Transform)>) {
for (mut logo, mut transform) in &mut sprite_position {
match *logo {
Direction::Up => transform.translation.y += 150. * time.delta_seconds(),
Direction::Down => transform.translation.y -= 150. * time.delta_seconds(),
}
if transform.translation.y > 200. {
*logo = Direction::Down;
} else if transform.translation.y < -200. {
*logo = Direction::Up;
}
}
}