Crate bevy_2dviewangle

Source
Expand description

Bevy plugin to easier to manage and switch texture base on view angles.

§Quickstart

// Struct to store sprite sheet
use bevy::prelude::*;
use bevy_2dviewangle::{Angle, View2dActor, ViewChanged};
use bevy_2dviewangle::View2dCollection;

#[derive(View2dCollection, Default)]
struct MyAssets {
    #[textureview(actor = "player", action = "idle", angle = "front")]
    pub idle_front: Handle<Image>,

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

    // If the angle "right" is not defined, it will be flipped base on the angle "left" image
    #[textureview(angle = "left")]
    pub idle_left: Handle<Image>,

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

// Change the sprite sheet by sending event
fn switch_sprite(
    mut actors: Query<(&mut View2dActor, Entity)>,
    mut action_event: EventWriter<ViewChanged>,
) {
    for (mut act, e) in actors.iter_mut() {
        act.action = ActionMyAssets::Idle.into();
        act.angle = Angle::Right;
        action_event.write(ViewChanged { entity: e });
    }
}

Please see in examples for more detail.

This plugin can work with bevy_asset_loader too:

use bevy::prelude::{Handle, Image, Resource, TextureAtlasLayout};
use bevy_asset_loader::prelude::AssetCollection;
use bevy_2dviewangle::View2dCollection;

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

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

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

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

Structs§

ActorSpriteSheets
The resource that stores every spritesheets. Organized by actor id (u64) and action id (u16)
AngleSpriteSheets
Map of Angle and its SpriteSheet
LastFrame
Sent when animation went to the last frame
SpriteSheet
Sprite sheet for one angle, store image and atlas layout
View2DAnglePlugin
The main plugin
View2DAnglePluginAnyState
Use this if you don’t care to state and want this plugin’s systems run all the time.
View2dActor
ViewChanged
Notify the view is changed.

Enums§

Angle
All supported angles.
DummyState
Notification

Traits§

View2dCollection
The trait to use in derive macro. You won’t need to implement this trait.

Derive Macros§

View2dCollection