2024-08-26 18:01:32 +00:00
|
|
|
//! Demonstrates picking for sprites and sprite atlases. The picking backend only tests against the
|
2024-09-19 00:46:40 +00:00
|
|
|
//! sprite bounds, so the sprite atlas can be picked by clicking on its transparent areas.
|
2024-08-26 18:01:32 +00:00
|
|
|
|
|
|
|
use bevy::{prelude::*, sprite::Anchor};
|
|
|
|
use std::fmt::Debug;
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
App::new()
|
2024-10-09 23:51:28 +00:00
|
|
|
.add_plugins(DefaultPlugins.set(ImagePlugin::default_nearest()))
|
2024-08-26 18:01:32 +00:00
|
|
|
.add_systems(Startup, (setup, setup_atlas))
|
|
|
|
.add_systems(Update, (move_sprite, animate_sprite))
|
|
|
|
.run();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn move_sprite(
|
|
|
|
time: Res<Time>,
|
|
|
|
mut sprite: Query<&mut Transform, (Without<Sprite>, With<Children>)>,
|
|
|
|
) {
|
2024-10-16 21:09:32 +00:00
|
|
|
let t = time.elapsed_secs() * 0.1;
|
2024-08-26 18:01:32 +00:00
|
|
|
for mut transform in &mut sprite {
|
|
|
|
let new = Vec2 {
|
2024-09-16 23:28:12 +00:00
|
|
|
x: 50.0 * ops::sin(t),
|
|
|
|
y: 50.0 * ops::sin(t * 2.0),
|
2024-08-26 18:01:32 +00:00
|
|
|
};
|
|
|
|
transform.translation.x = new.x;
|
|
|
|
transform.translation.y = new.y;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set up a scene that tests all sprite anchor types.
|
|
|
|
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
|
2024-10-05 01:59:52 +00:00
|
|
|
commands.spawn(Camera2d);
|
2024-08-26 18:01:32 +00:00
|
|
|
|
|
|
|
let len = 128.0;
|
2024-10-09 16:17:26 +00:00
|
|
|
let sprite_size = Vec2::splat(len / 2.0);
|
2024-08-26 18:01:32 +00:00
|
|
|
|
|
|
|
commands
|
Deprecate SpatialBundle (#15830)
# Objective
- Required components replace bundles, but `SpatialBundle` is yet to be
deprecated
## Solution
- Deprecate `SpatialBundle`
- Insert `Transform` and `Visibility` instead in examples using it
- In `spawn` or `insert` inserting a default `Transform` or `Visibility`
with component already requiring either, remove those components from
the tuple
## Testing
- Did you test these changes? If so, how?
Yes, I ran the examples I changed and tests
- Are there any parts that need more testing?
The `gamepad_viewer` and and `custom_shader_instancing` examples don't
work as intended due to entirely unrelated code, didn't check main.
- How can other people (reviewers) test your changes? Is there anything
specific they need to know?
Run examples, or just check that all spawned values are identical
- If relevant, what platforms did you test these changes on, and are
there any important ones you can't test?
Linux, wayland trough x11 (cause that's the default feature)
---
## Migration Guide
`SpatialBundle` is now deprecated, insert `Transform` and `Visibility`
instead which will automatically insert all other components that were
in the bundle. If you do not specify these values and any other
components in your `spawn`/`insert` call already requires either of
these components you can leave that one out.
before:
```rust
commands.spawn(SpatialBundle::default());
```
after:
```rust
commands.spawn((Transform::default(), Visibility::default());
```
2024-10-13 17:28:22 +00:00
|
|
|
.spawn((Transform::default(), Visibility::default()))
|
2024-08-26 18:01:32 +00:00
|
|
|
.with_children(|commands| {
|
|
|
|
for (anchor_index, anchor) in [
|
|
|
|
Anchor::TopLeft,
|
|
|
|
Anchor::TopCenter,
|
|
|
|
Anchor::TopRight,
|
|
|
|
Anchor::CenterLeft,
|
|
|
|
Anchor::Center,
|
|
|
|
Anchor::CenterRight,
|
|
|
|
Anchor::BottomLeft,
|
|
|
|
Anchor::BottomCenter,
|
|
|
|
Anchor::BottomRight,
|
|
|
|
Anchor::Custom(Vec2::new(0.5, 0.5)),
|
|
|
|
]
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
{
|
|
|
|
let i = (anchor_index % 3) as f32;
|
|
|
|
let j = (anchor_index / 3) as f32;
|
|
|
|
|
|
|
|
// spawn black square behind sprite to show anchor point
|
|
|
|
commands
|
2024-10-09 16:17:26 +00:00
|
|
|
.spawn((
|
|
|
|
Sprite::from_color(Color::BLACK, sprite_size),
|
|
|
|
Transform::from_xyz(i * len - len, j * len - len, -1.0),
|
|
|
|
))
|
2024-08-26 18:01:32 +00:00
|
|
|
.observe(recolor_on::<Pointer<Over>>(Color::srgb(0.0, 1.0, 1.0)))
|
|
|
|
.observe(recolor_on::<Pointer<Out>>(Color::BLACK))
|
|
|
|
.observe(recolor_on::<Pointer<Down>>(Color::srgb(1.0, 1.0, 0.0)))
|
|
|
|
.observe(recolor_on::<Pointer<Up>>(Color::srgb(0.0, 1.0, 1.0)));
|
|
|
|
|
|
|
|
commands
|
2024-10-09 16:17:26 +00:00
|
|
|
.spawn((
|
|
|
|
Sprite {
|
|
|
|
image: asset_server.load("branding/bevy_bird_dark.png"),
|
|
|
|
custom_size: Some(sprite_size),
|
2024-08-26 18:01:32 +00:00
|
|
|
color: Color::srgb(1.0, 0.0, 0.0),
|
|
|
|
anchor: anchor.to_owned(),
|
|
|
|
..default()
|
|
|
|
},
|
|
|
|
// 3x3 grid of anchor examples by changing transform
|
2024-10-09 16:17:26 +00:00
|
|
|
Transform::from_xyz(i * len - len, j * len - len, 0.0)
|
2024-08-26 18:01:32 +00:00
|
|
|
.with_scale(Vec3::splat(1.0 + (i - 1.0) * 0.2))
|
|
|
|
.with_rotation(Quat::from_rotation_z((j - 1.0) * 0.2)),
|
2024-10-09 16:17:26 +00:00
|
|
|
))
|
2024-08-26 18:01:32 +00:00
|
|
|
.observe(recolor_on::<Pointer<Over>>(Color::srgb(0.0, 1.0, 0.0)))
|
|
|
|
.observe(recolor_on::<Pointer<Out>>(Color::srgb(1.0, 0.0, 0.0)))
|
|
|
|
.observe(recolor_on::<Pointer<Down>>(Color::srgb(0.0, 0.0, 1.0)))
|
|
|
|
.observe(recolor_on::<Pointer<Up>>(Color::srgb(0.0, 1.0, 0.0)));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Component)]
|
|
|
|
struct AnimationIndices {
|
|
|
|
first: usize,
|
|
|
|
last: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Component, Deref, DerefMut)]
|
|
|
|
struct AnimationTimer(Timer);
|
|
|
|
|
|
|
|
fn animate_sprite(
|
|
|
|
time: Res<Time>,
|
2024-10-09 23:51:28 +00:00
|
|
|
mut query: Query<(&AnimationIndices, &mut AnimationTimer, &mut Sprite)>,
|
2024-08-26 18:01:32 +00:00
|
|
|
) {
|
|
|
|
for (indices, mut timer, mut sprite) in &mut query {
|
2024-10-09 23:51:28 +00:00
|
|
|
let Some(texture_atlas) = &mut sprite.texture_atlas else {
|
|
|
|
continue;
|
|
|
|
};
|
|
|
|
|
2024-08-26 18:01:32 +00:00
|
|
|
timer.tick(time.delta());
|
2024-10-09 23:51:28 +00:00
|
|
|
|
2024-08-26 18:01:32 +00:00
|
|
|
if timer.just_finished() {
|
2024-10-09 23:51:28 +00:00
|
|
|
texture_atlas.index = if texture_atlas.index == indices.last {
|
2024-08-26 18:01:32 +00:00
|
|
|
indices.first
|
|
|
|
} else {
|
2024-10-09 23:51:28 +00:00
|
|
|
texture_atlas.index + 1
|
2024-08-26 18:01:32 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn setup_atlas(
|
|
|
|
mut commands: Commands,
|
|
|
|
asset_server: Res<AssetServer>,
|
|
|
|
mut texture_atlas_layouts: ResMut<Assets<TextureAtlasLayout>>,
|
|
|
|
) {
|
|
|
|
let texture_handle = asset_server.load("textures/rpg/chars/gabe/gabe-idle-run.png");
|
|
|
|
let layout = TextureAtlasLayout::from_grid(UVec2::new(24, 24), 7, 1, None, None);
|
|
|
|
let texture_atlas_layout_handle = texture_atlas_layouts.add(layout);
|
|
|
|
// Use only the subset of sprites in the sheet that make up the run animation
|
|
|
|
let animation_indices = AnimationIndices { first: 1, last: 6 };
|
|
|
|
commands
|
|
|
|
.spawn((
|
2024-10-09 16:17:26 +00:00
|
|
|
Sprite::from_atlas_image(
|
|
|
|
texture_handle,
|
|
|
|
TextureAtlas {
|
|
|
|
layout: texture_atlas_layout_handle,
|
|
|
|
index: animation_indices.first,
|
|
|
|
},
|
|
|
|
),
|
|
|
|
Transform::from_xyz(300.0, 0.0, 0.0).with_scale(Vec3::splat(6.0)),
|
2024-08-26 18:01:32 +00:00
|
|
|
animation_indices,
|
|
|
|
AnimationTimer(Timer::from_seconds(0.1, TimerMode::Repeating)),
|
|
|
|
))
|
|
|
|
.observe(recolor_on::<Pointer<Over>>(Color::srgb(0.0, 1.0, 1.0)))
|
|
|
|
.observe(recolor_on::<Pointer<Out>>(Color::srgb(1.0, 1.0, 1.0)))
|
|
|
|
.observe(recolor_on::<Pointer<Down>>(Color::srgb(1.0, 1.0, 0.0)))
|
|
|
|
.observe(recolor_on::<Pointer<Up>>(Color::srgb(0.0, 1.0, 1.0)));
|
|
|
|
}
|
|
|
|
|
|
|
|
// An observer listener that changes the target entity's color.
|
|
|
|
fn recolor_on<E: Debug + Clone + Reflect>(color: Color) -> impl Fn(Trigger<E>, Query<&mut Sprite>) {
|
|
|
|
move |ev, mut sprites| {
|
2024-12-08 21:55:09 +00:00
|
|
|
let Ok(mut sprite) = sprites.get_mut(ev.target()) else {
|
2024-08-26 18:01:32 +00:00
|
|
|
return;
|
|
|
|
};
|
|
|
|
sprite.color = color;
|
|
|
|
}
|
|
|
|
}
|