#bevy-plugin #bevy #texture #view #switch #base #angle

bevy_2dviewangle

Bevy plugin for easier to switch texture base on view angles

10 releases (4 breaking)

new 0.4.1 Apr 22, 2024
0.4.0 Apr 17, 2024
0.3.3 Apr 15, 2024
0.2.0 Feb 26, 2024
0.0.4 Jan 30, 2024

#186 in Game dev

Download history 14/week @ 2024-01-26 108/week @ 2024-02-09 149/week @ 2024-02-16 152/week @ 2024-02-23 23/week @ 2024-03-01 5/week @ 2024-03-08 1/week @ 2024-03-15 67/week @ 2024-04-05 426/week @ 2024-04-12 160/week @ 2024-04-19

653 downloads per month

MIT/Apache

30KB
222 lines

bevy_2dviewangle

crates.io docs.rs

Bevy plugin to easier to switch texture base on view angles. Currently, support 8 view angles:

  • front
  • back
  • left
  • right
  • front_left
  • front_right
  • back_left
  • back_right

Quick Start

use bevy_2dviewangle::{
    ActorsTextures, ActorsTexturesCollection, Angle, DynamicActor, View2DAnglePlugin,
    ViewChanged,
};

// Struct to load spritesheet
#[derive(ActorsTexturesCollection, Default)]
struct MyAssets {
    #[textureview(actor = 0, action = 0, angle = "front", handle = "image")]
    pub idle_front: Handle<Image>,

    // If not specify actor/action, the previous value will be used
    #[textureview(angle = "back", handle = "image")]
    pub idle_back: Handle<Image>,

    // If angle is any, other angle which has not been defined will use this value
    #[textureview(angle = "front", handle = "atlas_layout", angle = "any")]
    pub layout: Handle<TextureAtlasLayout>,
}

fn main() {
    App::new()
        ...
        // Add the plugin
        .add_plugins(View2DAnglePlugin)
        .add_systems(Startup, setup)
        .add_systems(Update, input)
        .run();
}

fn setup(...) {
    let mut my_assets = MyAssets::default();
    my_assets.idle_front = asset_server.load("frog_idle_front.png");
    my_assets.idle_back = asset_server.load("frog_idle_back.png");

    let layout = TextureAtlasLayout::from_grid(Vec2::new(16., 16.), 1, 3, None, None);
    my_assets.layout = texture_atlases.add(layout);

    // Load into collection
    animation2d.load_asset_loader(&my_assets);

    commands.spawn((
        SpriteSheetBundle {
            ...
        },
        // Specify actor for entity
        DynamicActor {
            actor: 0, // actor id
            animation_timer: Some(Timer::from_seconds(0.25, TimerMode::Repeating)),
            ..default()
        },
    ));
}

fn input(...) {
    for (mut act, e) in actors.iter_mut() {
        let mut action = act.action;
        let mut direction = act.angle;

        // Update action id and direction of actor
        if if kb_input.any_pressed([KeyCode::ArrowUp, KeyCode::KeyW]) {
            action = 0;
            direction = Angle::Back;
        }
        ...

        if action != act.action || direction != act.angle {
            act.action = action;
            act.angle = direction;
            // Send event to change to spritesheet of another view
            action_event.send(ViewChanged { entity: e });
        }
    }
}

Please see in examples for more detail.

This plugin can work with bevy_asset_loader too:

#[derive(AssetCollection, ActorsTexturesCollection, Resource)]
pub struct MyAssets {
    #[asset(path = "frog_idle_front.png")]
    #[textureview(actor = 0, action = 0, angle = "front", handle = "image")]
    pub idle_front: Handle<Image>,

    #[asset(path = "frog_idle_back.png")]
    #[textureview(angle = "back", handle = "image")]
    pub idle_back: Handle<Image>,

    #[asset(path = "frog_idle_left.png")]
    #[textureview(angle = "left", handle = "image")]
    pub idle_left: Handle<Image>,

    #[asset(texture_atlas_layout(tile_size_x = 16., tile_size_y = 16., columns = 1, rows = 3))]
    #[textureview(angle = "front", handle = "atlas_layout", angle = "any")]
    pub front_layout: Handle<TextureAtlasLayout>,
}

Examples

2d

2d demo

2d example

3d

3d demo

3d example

Use with bevy_asset_loader

asset loader example

License

Please see LICENSE.

Compatible Bevy Versions

bevy bevy_2dviewangle
0.13 0.2-0.4, branch master
0.12 0.1

Dependencies

~41–80MB
~1M SLoC