sprite_tile/
sprite_tile.rs

1//! Displays a single [`Sprite`] tiled in a grid, with a scaling animation
2
3use bevy::prelude::*;
4
5fn main() {
6    App::new()
7        .add_plugins(DefaultPlugins)
8        .add_systems(Startup, setup)
9        .add_systems(Update, animate)
10        .run();
11}
12
13#[derive(Resource)]
14struct AnimationState {
15    min: f32,
16    max: f32,
17    current: f32,
18    speed: f32,
19}
20
21fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
22    commands.spawn(Camera2d);
23
24    commands.insert_resource(AnimationState {
25        min: 128.0,
26        max: 512.0,
27        current: 128.0,
28        speed: 50.0,
29    });
30    commands.spawn(Sprite {
31        image: asset_server.load("branding/icon.png"),
32        image_mode: SpriteImageMode::Tiled {
33            tile_x: true,
34            tile_y: true,
35            stretch_value: 0.5, // The image will tile every 128px
36        },
37        ..default()
38    });
39}
40
41fn animate(mut sprites: Query<&mut Sprite>, mut state: ResMut<AnimationState>, time: Res<Time>) {
42    if state.current >= state.max || state.current <= state.min {
43        state.speed = -state.speed;
44    };
45    state.current += state.speed * time.delta_secs();
46    for mut sprite in &mut sprites {
47        sprite.custom_size = Some(Vec2::splat(state.current));
48    }
49}