mirror of
https://github.com/bevyengine/bevy
synced 2024-11-22 04:33:37 +00:00
7d40e3ec87
# Objective Continue migration of bevy APIs to required components, following guidance of https://hackmd.io/@bevy/required_components/ ## Solution - Make `Sprite` require `Transform` and `Visibility` and `SyncToRenderWorld` - move image and texture atlas handles into `Sprite` - deprecate `SpriteBundle` - remove engine uses of `SpriteBundle` ## Testing ran cargo tests on bevy_sprite and tested several sprite examples. --- ## Migration Guide Replace all uses of `SpriteBundle` with `Sprite`. There are several new convenience constructors: `Sprite::from_image`, `Sprite::from_atlas_image`, `Sprite::from_color`. WARNING: use of `Handle<Image>` and `TextureAtlas` as components on sprite entities will NO LONGER WORK. Use the fields on `Sprite` instead. I would have removed the `Component` impls from `TextureAtlas` and `Handle<Image>` except it is still used within ui. We should fix this moving forward with the migration.
43 lines
1.2 KiB
Rust
43 lines
1.2 KiB
Rust
//! Renders a 2D scene containing a single, moving sprite.
|
|
|
|
use bevy::prelude::*;
|
|
|
|
fn main() {
|
|
App::new()
|
|
.add_plugins(DefaultPlugins)
|
|
.add_systems(Startup, setup)
|
|
.add_systems(Update, sprite_movement)
|
|
.run();
|
|
}
|
|
|
|
#[derive(Component)]
|
|
enum Direction {
|
|
Up,
|
|
Down,
|
|
}
|
|
|
|
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
|
|
commands.spawn(Camera2d);
|
|
commands.spawn((
|
|
Sprite::from_image(asset_server.load("branding/icon.png")),
|
|
Transform::from_xyz(100., 0., 0.),
|
|
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;
|
|
}
|
|
}
|
|
}
|