Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
use bevy::prelude::*;
use bevy::render::camera::ScalingMode;
use micro_banimate::definitions::{AnimationSet, AnimationStatus, DirectionalAnimationBundle};
use micro_banimate::directionality::{Directionality, Horizontal};
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugins(micro_banimate::BanimatePluginGroup)
.add_systems(Startup, load_assets)
.add_systems(PostStartup, spawn_assets)
.add_systems(Update, process_input)
.run();
}
#[derive(Default, Resource)]
struct ExampleAssets {
spritesheet: Handle<TextureAtlas>,
animations: Handle<AnimationSet>,
}
fn load_assets(
mut commands: Commands,
assets: Res<AssetServer>,
mut atlas: ResMut<Assets<TextureAtlas>>,
) {
let atlas_handle = atlas.add(TextureAtlas::from_grid(
assets.load("character.png"),
Vec2::new(48., 48.),
10,
6,
None,
None,
));
let anim_handle = assets.load("character.anim.json");
commands.insert_resource(ExampleAssets {
spritesheet: atlas_handle,
animations: anim_handle,
});
}
fn spawn_assets(mut commands: Commands, assets: Res<ExampleAssets>) {
const WIDTH: f32 = 480.0;
const HEIGHT: f32 = 320.0;
commands.spawn((
SpriteSheetBundle {
texture_atlas: assets.spritesheet.clone_weak(),
..Default::default()
},
DirectionalAnimationBundle::with_direction(
"idle",
assets.animations.clone_weak(),
Directionality::Right,
),
));
commands.spawn(Camera2dBundle {
projection: OrthographicProjection {
area: Rect::new(-(WIDTH / 2.0), -(HEIGHT / 2.0), WIDTH / 2.0, HEIGHT / 2.0),
scaling_mode: ScalingMode::AutoMin {
min_height: HEIGHT,
min_width: WIDTH,
},
far: 1000.,
near: -1000.,
..Default::default()
},
..Default::default()
});
}
fn process_input(
input: Res<Input<KeyCode>>,
mut status_query: Query<(&mut AnimationStatus, &mut Directionality)>,
) {
for (mut status, mut direction) in &mut status_query {
if input.just_released(KeyCode::Left) {
direction.with_horizontal(Horizontal::Left);
}
if input.just_released(KeyCode::Right) {
direction.with_horizontal(Horizontal::Right);
}
if input.just_released(KeyCode::Key1) {
status.start_or_continue("idle");
}
if input.just_released(KeyCode::Key2) {
status.start_or_continue("run");
}
}
}