bevy_2dviewangle

Crates.iobevy_2dviewangle
lib.rsbevy_2dviewangle
version0.4.2
sourcesrc
created_at2024-01-26 09:23:16.208264
updated_at2024-05-04 07:48:23.173225
descriptionBevy plugin for easier to switch texture base on view angles
homepage
repositoryhttps://gitlab.com/kimtinh/bevy-2dviewangle
max_upload_size
id1115343
size114,011
Trung Do (dothanhtrung)

documentation

README

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

Add plugin.

    App::new()
        ...
        .add_plugins(View2DAnglePlugin)
        ...

Declare texture map with each actor and action with view angle.

// 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>,

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

Change the sprite sheet by sending event.

fn switch_sprite(
    mut actors: Query<(&mut DynamicActor, Entity)>,
    mut action_event: EventWriter<ViewChanged>,
) {
    for (mut act, e) in actors.iter_mut() {
        act.action = new_action;
        act.angle = new_direction;
        // Send event to change to sprite sheet to 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 = "any", handle = "atlas_layout")]
    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
Commit count: 55

cargo fmt