mirror of
https://github.com/bevyengine/bevy
synced 2024-12-02 01:19:12 +00:00
aefe1f0739
Co-authored-by: Mike <mike.hsu@gmail.com>
44 lines
1.3 KiB
Rust
44 lines
1.3 KiB
Rust
//! Renders a 2D scene containing pixelated bevy logo in a pixel perfect style
|
|
|
|
use bevy::prelude::*;
|
|
|
|
fn main() {
|
|
App::new()
|
|
.add_plugins(DefaultPlugins.set(ImagePlugin::default_nearest()))
|
|
.add_systems(Startup, setup)
|
|
.add_systems(Update, sprite_movement)
|
|
.run();
|
|
}
|
|
|
|
#[derive(Component)]
|
|
enum Direction {
|
|
Left,
|
|
Right,
|
|
}
|
|
|
|
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
|
|
commands.spawn(Camera2dBundle::default());
|
|
commands.spawn((
|
|
SpriteBundle {
|
|
texture: asset_server.load("pixel/bevy_pixel_light.png"),
|
|
transform: Transform::from_xyz(100., 0., 0.),
|
|
..default()
|
|
},
|
|
Direction::Right,
|
|
));
|
|
}
|
|
|
|
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::Right => transform.translation.x += 30. * time.delta_seconds(),
|
|
Direction::Left => transform.translation.x -= 30. * time.delta_seconds(),
|
|
}
|
|
|
|
if transform.translation.x > 200. {
|
|
*logo = Direction::Left;
|
|
} else if transform.translation.x < -200. {
|
|
*logo = Direction::Right;
|
|
}
|
|
}
|
|
}
|