-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcharacter.rs
188 lines (142 loc) · 5.28 KB
/
character.rs
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// This example shows how to create controllable character with multiple animations.
//
// - We'll create a few animations for our character (idle, run, shoot) in a setup system
// - We'll move the character with the keyboard and switch between animations in another system
#[path = "./common/mod.rs"]
pub mod common;
use bevy::prelude::*;
use bevy_spritesheet_animation::prelude::*;
fn main() {
App::new()
.add_plugins((
DefaultPlugins.set(ImagePlugin::default_nearest()),
SpritesheetAnimationPlugin::default(),
))
.add_systems(Startup, spawn_character)
.add_systems(Update, control_character)
.run();
}
fn spawn_character(
mut commands: Commands,
mut atlas_layouts: ResMut<Assets<TextureAtlasLayout>>,
mut library: ResMut<AnimationLibrary>,
assets: Res<AssetServer>,
) {
commands.spawn(Camera2d);
// Create the animations
let spritesheet = Spritesheet::new(8, 8);
// Idle
let idle_clip = Clip::from_frames(spritesheet.horizontal_strip(0, 0, 5));
let idle_clip_id = library.register_clip(idle_clip);
let idle_animation = Animation::from_clip(idle_clip_id);
let idle_animation_id = library.register_animation(idle_animation);
library.name_animation(idle_animation_id, "idle").unwrap();
// Run
let run_clip = Clip::from_frames(spritesheet.row(3));
let run_clip_id = library.register_clip(run_clip);
let run_animation = Animation::from_clip(run_clip_id);
let run_animation_id = library.register_animation(run_animation);
library.name_animation(run_animation_id, "run").unwrap();
// Shoot
let shoot_clip = Clip::from_frames(spritesheet.horizontal_strip(0, 5, 5));
let shoot_clip_id = library.register_clip(shoot_clip);
let shoot_animation = Animation::from_clip(shoot_clip_id);
let shoot_animation_id = library.register_animation(shoot_animation);
library.name_animation(shoot_animation_id, "shoot").unwrap();
// Spawn the character
let image = assets.load("character.png");
let atlas = TextureAtlas {
layout: atlas_layouts.add(Spritesheet::new(8, 8).atlas_layout(96, 96)),
..default()
};
commands.spawn((
Sprite::from_atlas_image(image, atlas),
SpritesheetAnimation::from_id(idle_animation_id),
));
}
// Component to check if a character is currently shooting
#[derive(Component)]
struct Shooting;
fn control_character(
mut commands: Commands,
time: Res<Time>,
keyboard: Res<ButtonInput<KeyCode>>,
library: Res<AnimationLibrary>,
mut events: EventReader<AnimationEvent>,
mut characters: Query<(
Entity,
&mut Sprite,
&mut SpritesheetAnimation,
&mut Transform,
Option<&Shooting>,
)>,
) {
// Control the character with the keyboard
const CHARACTER_SPEED: f32 = 150.0;
for (entity, mut sprite, mut animation, mut transform, shooting) in &mut characters {
// Except if they're shooting, in which case we wait for the animation to end
if shooting.is_some() {
continue;
}
// Shoot
if keyboard.pressed(KeyCode::Space) {
// Set the animation
if let Some(shoot_animation_id) = library.animation_with_name("shoot") {
animation.switch(shoot_animation_id);
}
// Add a Shooting component
commands.entity(entity).insert(Shooting);
}
// Move left
else if keyboard.pressed(KeyCode::ArrowLeft) {
// Set the animation
if let Some(run_animation_id) = library.animation_with_name("run") {
if animation.animation_id != run_animation_id {
animation.switch(run_animation_id);
}
}
// Move
transform.translation -= Vec3::X * time.delta_secs() * CHARACTER_SPEED;
sprite.flip_x = true;
}
// Move right
else if keyboard.pressed(KeyCode::ArrowRight) {
// Set the animation
if let Some(run_animation_id) = library.animation_with_name("run") {
if animation.animation_id != run_animation_id {
animation.switch(run_animation_id);
}
}
// Move
transform.translation += Vec3::X * time.delta_secs() * CHARACTER_SPEED;
sprite.flip_x = false;
}
// Idle
else {
// Set the animation
if let Some(idle_animation_id) = library.animation_with_name("idle") {
if animation.animation_id != idle_animation_id {
animation.switch(idle_animation_id);
}
}
}
}
// Remove the Shooting component when the shooting animation ends
//
// We use animation events to detect when this happens.
// Check out the `events` examples for more details.
for event in events.read() {
match event {
AnimationEvent::AnimationRepetitionEnd {
entity,
animation_id,
..
} => {
if library.is_animation_name(*animation_id, "shoot") {
commands.entity(*entity).remove::<Shooting>();
}
}
_ => (),
}
}
}