12 releases (breaking)

0.9.0 May 7, 2024
0.8.0 Feb 26, 2024
0.7.0 Nov 7, 2023
0.5.0 Apr 11, 2023
0.1.0 Mar 20, 2022

#104 in Game dev

Download history 12/week @ 2024-01-29 2/week @ 2024-02-05 9/week @ 2024-02-12 66/week @ 2024-02-19 192/week @ 2024-02-26 23/week @ 2024-03-04 30/week @ 2024-03-11 7/week @ 2024-03-18 108/week @ 2024-04-01 23/week @ 2024-04-08 7/week @ 2024-04-15 9/week @ 2024-04-22 134/week @ 2024-05-06 9/week @ 2024-05-13

152 downloads per month

MIT license

46KB
1K SLoC

bevy-parallax

A parallax plugin for the Bevy Engine. This plugin allows you to easily create scrolling parallax backgrounds for your games.

cyberpunk

fishy

Usage

use bevy::prelude::*;
use bevy_parallax::{
    LayerSpeed, LayerData, ParallaxCameraComponent, ParallaxMoveEvent, ParallaxPlugin, ParallaxSystems, CreateParallaxEvent
};

fn main() {
    let primary_window = Window {
        title: "Window Name".to_string(),
        resolution: (1280.0, 720.0).into(),
        resizable: false,
        ..default()
    };
    App::new()
        .add_plugins(
            DefaultPlugins
                .set(WindowPlugin {
                    primary_window: Some(primary_window),
                    ..default()
                })
                // Use nearest filtering so our pixel art renders clear
                .set(ImagePlugin::default_nearest()),
        )
        .add_plugins(ParallaxPlugin)
        .add_systems(Startup, initialize_camera_system)
        .add_systems(Update, move_camera_system.before(ParallaxSystems))
        .run();
}

pub fn initialize_camera_system(
    mut commands: Commands,
    mut create_parallax: EventWriter<CreateParallaxEvent>
) {
    let camera = commands
        .spawn(Camera2dBundle::default())
        .insert(ParallaxCameraComponent::default())
        .id();
    let event = CreateParallaxEvent {
        layers_data: vec![
            LayerData {
                speed: LayerSpeed::Horizontal(0.9),
                path: "cyberpunk_back.png".to_string(),
                tile_size: Vec2::new(96.0, 160.0),
                cols: 1,
                rows: 1,
                scale: Vec2::splat(4.5),
                z: 0.0,
                ..default()
            },
            LayerData {
                speed: LayerSpeed::Horizontal(0.6),
                path: "cyberpunk_middle.png".to_string(),
                tile_size: Vec2::new(144.0, 160.0),
                cols: 1,
                rows: 1,
                scale: Vec2::splat(4.5),
                z: 1.0,
                ..default()
            },
            LayerData {
                speed: LayerSpeed::Horizontal(0.1),
                path: "cyberpunk_front.png".to_string(),
                tile_size: Vec2::new(272.0, 160.0),
                cols: 1,
                rows: 1,
                scale: Vec2::splat(4.5),
                z: 2.0,
                ..default()
            },
        ],
        camera: camera,
    };
    create_parallax.send(event);
}

pub fn move_camera_system(
    keyboard_input: Res<ButtonInput<KeyCode>>,
    mut move_event_writer: EventWriter<ParallaxMoveEvent>,
    camera_query: Query<Entity, With<Camera>>,
) {
    let camera = camera_query.get_single().unwrap();
    if keyboard_input.pressed(KeyCode::KeyD) || keyboard_input.pressed(KeyCode::ArrowRight) {
        move_event_writer.send(ParallaxMoveEvent {
            translation: Vec2::new(3.0, 0.0),
            rotation: 0.,
            camera: camera,
        });
    } else if keyboard_input.pressed(KeyCode::KeyA) || keyboard_input.pressed(KeyCode::ArrowLeft) {
        move_event_writer.send(ParallaxMoveEvent {
            translation: Vec2::new(-3.0, 0.0),
            rotation: 0.,
            camera: camera,
        });
    }
}

Compatible Bevy versions

Compatibility of bevy-parallax versions:

Bevy version bevy-parallax version
0.13 0.8
0.12 0.7
0.11 0.5 - 0.6
0.10 0.4

Credits

Dependencies

~40–80MB
~1M SLoC