Skip to content

[Merged by Bors] - Add move sprite example. #2414

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 11 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ path = "examples/2d/contributors.rs"
name = "many_sprites"
path = "examples/2d/many_sprites.rs"

[[example]]
name = "move_sprite"
path = "examples/2d/move_sprite.rs"

[[example]]
name = "2d_rotation"
path = "examples/2d/rotation.rs"
Expand Down
41 changes: 41 additions & 0 deletions examples/2d/move_sprite.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
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_bundle(OrthographicCameraBundle::new_2d());
commands
.spawn_bundle(SpriteBundle {
texture: asset_server.load("branding/icon.png"),
transform: Transform::from_xyz(100., 0., 0.),
..Default::default()
})
.insert(Direction::Up);
}

fn sprite_movement(time: Res<Time>, mut sprite_position: Query<(&mut Direction, &mut Transform)>) {
for (mut logo, mut transform) in sprite_position.iter_mut() {
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;
}
}
}
1 change: 1 addition & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ Example | File | Description
--- | --- | ---
`contributors` | [`2d/contributors.rs`](./2d/contributors.rs) | Displays each contributor as a bouncy bevy-ball!
`many_sprites` | [`2d/many_sprites.rs`](./2d/many_sprites.rs) | Displays many sprites in a grid arragement! Used for performance testing.
`move_sprite` | [`2d/move_sprite.rs`](./2d/move_sprite.rs) | Changes the transform of a sprite.
`mesh2d` | [`2d/mesh2d.rs`](./2d/mesh2d.rs) | Renders a 2d mesh
`mesh2d_manual` | [`2d/mesh2d_manual.rs`](./2d/mesh2d_manual.rs) | Renders a custom mesh "manually" with "mid-level" renderer apis.
`rect` | [`2d/rect.rs`](./2d/rect.rs) | Renders a rectangle
Expand Down