-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathcharacter.rs
More file actions
168 lines (131 loc) · 4.74 KB
/
character.rs
File metadata and controls
168 lines (131 loc) · 4.74 KB
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
// This example shows how to create a controllable character with multiple animations.
//
// - We create a few animations for our character (idle, run, shoot) in a Startup system
// - We move the character with the keyboard and switch between animations in an Update system
use bevy::prelude::*;
use bevy_spritesheet_animation::prelude::*;
fn main() {
App::new()
.add_plugins((
DefaultPlugins.set(ImagePlugin::default_nearest()),
SpritesheetAnimationPlugin,
))
.add_systems(Startup, spawn_character)
.add_systems(Update, control_character)
.run();
}
// Let's use a custom resource to store our animations and access them across systems
#[derive(Resource)]
struct MyAnimations {
idle: Handle<Animation>,
run: Handle<Animation>,
shoot: Handle<Animation>,
}
fn spawn_character(
mut commands: Commands,
assets: Res<AssetServer>,
mut animations: ResMut<Assets<Animation>>,
mut atlas_layouts: ResMut<Assets<TextureAtlasLayout>>,
) {
commands.spawn(Camera2d);
// Create the animations
let image = assets.load("character.png");
let spritesheet = Spritesheet::new(&image, 8, 8);
// Idle
let idle_animation = spritesheet
.create_animation()
.add_horizontal_strip(0, 0, 5)
.build();
let idle_animation_handle = animations.add(idle_animation);
// Run
let run_animation = spritesheet.create_animation().add_row(3).build();
let run_animation_handle = animations.add(run_animation);
// Shoot
let shoot_animation = spritesheet
.create_animation()
.add_horizontal_strip(0, 5, 5)
.build();
let shoot_animation_handle = animations.add(shoot_animation);
// Store the animations as a resource
commands.insert_resource(MyAnimations {
idle: idle_animation_handle.clone(),
run: run_animation_handle,
shoot: shoot_animation_handle,
});
// Spawn the character
let sprite = spritesheet
.with_size_hint(768, 768)
.sprite(&mut atlas_layouts);
commands.spawn((sprite, SpritesheetAnimation::new(idle_animation_handle)));
}
// Component to mark that a character is currently shooting
#[derive(Component)]
struct Shooting;
fn control_character(
time: Res<Time>,
keyboard: Res<ButtonInput<KeyCode>>,
mut commands: Commands,
character: Single<(
Entity,
&mut Sprite,
&mut SpritesheetAnimation,
&mut Transform,
Option<&Shooting>,
)>,
my_animations: Res<MyAnimations>,
mut messages: MessageReader<AnimationEvent>,
) {
// Control the character with the keyboard
const CHARACTER_SPEED: f32 = 150.0;
let (entity, mut sprite, mut animation, mut transform, shooting) = character.into_inner();
// If they're shooting, do nothing and wait for the animation to end
if shooting.is_none() {
// Shoot with the spacebar
if keyboard.pressed(KeyCode::Space) {
// Set the animation
animation.switch(my_animations.shoot.clone());
// Add a Shooting component
commands.entity(entity).insert(Shooting);
}
// Run with the arrows
else if keyboard.pressed(KeyCode::ArrowLeft) || keyboard.pressed(KeyCode::ArrowRight) {
// Set the animation
//
// Only if not already running as we don't want to reset the animation in that case
if animation.animation != my_animations.run {
animation.switch(my_animations.run.clone());
}
// Move the entity and flip it horizontally depending on the direction
let translation = Vec3::X * time.delta_secs() * CHARACTER_SPEED;
if keyboard.pressed(KeyCode::ArrowLeft) {
transform.translation -= translation;
sprite.flip_x = true;
} else {
transform.translation += translation;
sprite.flip_x = false;
}
}
// Idle
else {
// Set the animation
//
// Only if not already idle as we don't want to reset the animation in that case
if animation.animation != my_animations.idle {
animation.switch(my_animations.idle.clone());
}
}
}
// 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 messages.read() {
if let AnimationEvent::AnimationRepetitionEnd {
entity, animation, ..
} = event
&& animation == &my_animations.shoot
{
commands.entity(*entity).remove::<Shooting>();
}
}
}