Crate bevy_parallaxation2d

source ·
Expand description

§bevy_parallaxation2d

Crate providing simple 2D parallax layers in Bevy.

§Features

  • ParallaxPlugin - Plugin required for the parallax functionality.
  • ParallaxCamera - Component for marking the parallax camera. Only one camera can use parallax layers.
  • ParallaxLayer - Component for creating a parallax layer.
  • ParallaxFlags - Bit flags for defining attributes of a parallax layer.

§Examples

use bevy::prelude::*;

// Import the `bevy_parallaxation2d` crate
use bevy_parallaxation2d::prelude::*;

fn main() {
    App::new()
        .add_plugins((DefaultPlugins, ParallaxPlugin::default()))
        .add_systems(Startup, setup)
        .run();
}

fn setup(mut commands: Commands) {
    // Spawn parallax camera
    commands
        .spawn(Camera2dBundle::default())
        .insert(ParallaxCamera);

    // Spawn:
    // * Main background that repeats in both directions.
    // * Hills that will default to repeating horizontally.
    // * Foreground at the top of the screen.
    commands.spawn_batch(vec![
        ParallaxLayer {
            image: "images/main_background.png",
            depth: 80.0.into(),
            flags: ParallaxFlags::REPEAT_X_AXIS | ParallaxFlags::REPEAT_Y_AXIS,
            ..default()
        },
        ParallaxLayer {
            image: "images/hills_background.png",
            depth: 40.0.into(),
            ..default()
        },
        ParallaxLayer {
            image: "images/branches_foreground.png",
            depth: (-5.0).into(),
            flags: ParallaxFlags::REPEAT_X_AXIS | ParallaxFlags::OFFSET_CAMERA_TOP,
            ..default()
        },
    ]);
}

Modules§

  • The depth module provides extra functions and options for creating and managing [Depth] values used in parallax effects.
  • The prelude module exports commonly used types to provide a convenient entry point for users of the bevy_parallaxation2d crate. It includes plugins, components, and bitflags necessary for implementing parallax effects.