forked from bevyengine/bevy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcooldown.rs
More file actions
179 lines (167 loc) · 5.06 KB
/
cooldown.rs
File metadata and controls
179 lines (167 loc) · 5.06 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
169
170
171
172
173
174
175
176
177
178
179
//! Demonstrates implementing a cooldown in UI.
//!
//! You might want a system like this for abilities, buffs or consumables.
//! We create four food buttons to eat with 2, 1, 10, and 4 seconds cooldown.
use bevy::{color::palettes::tailwind, ecs::spawn::SpawnIter, prelude::*};
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_systems(Startup, setup)
.add_systems(
Update,
(
activate_ability,
animate_cooldowns.run_if(any_with_component::<ActiveCooldown>),
),
)
.run();
}
fn setup(
mut commands: Commands,
mut texture_atlas_layouts: ResMut<Assets<TextureAtlasLayout>>,
asset_server: Res<AssetServer>,
) {
commands.spawn(Camera2d);
let texture = asset_server.load("textures/food_kenney.png");
let layout = TextureAtlasLayout::from_grid(UVec2::splat(64), 7, 7, None, None);
let texture_atlas_layout = texture_atlas_layouts.add(layout);
commands.spawn((
Node {
width: percent(100),
height: percent(100),
align_items: AlignItems::Center,
justify_content: JustifyContent::Center,
column_gap: px(15),
..default()
},
Children::spawn(SpawnIter(
[
FoodItem {
name: "an apple",
cooldown: 2.,
index: 2,
},
FoodItem {
name: "a burger",
cooldown: 1.,
index: 23,
},
FoodItem {
name: "chocolate",
cooldown: 10.,
index: 32,
},
FoodItem {
name: "cherries",
cooldown: 4.,
index: 41,
},
]
.into_iter()
.map(move |food| build_ability(food, texture.clone(), texture_atlas_layout.clone())),
)),
));
commands.spawn((
Text::new("*Click some food to eat it*"),
Node {
position_type: PositionType::Absolute,
top: px(12),
left: px(12),
..default()
},
));
}
struct FoodItem {
name: &'static str,
cooldown: f32,
index: usize,
}
fn build_ability(
food: FoodItem,
texture: Handle<Image>,
layout: Handle<TextureAtlasLayout>,
) -> impl Bundle {
let FoodItem {
name,
cooldown,
index,
} = food;
let name = Name::new(name);
// Every food item is a button with a child node.
// The child node's height will be animated to be at 100% at the beginning
// of a cooldown, effectively graying out the whole button, and then getting smaller over time.
(
Node {
width: px(80),
height: px(80),
flex_direction: FlexDirection::ColumnReverse,
..default()
},
BackgroundColor(tailwind::SLATE_400.into()),
Button,
ImageNode::from_atlas_image(texture, TextureAtlas { layout, index }),
Cooldown(Timer::from_seconds(cooldown, TimerMode::Once)),
name,
children![(
Node {
width: percent(100),
height: percent(0),
..default()
},
BackgroundColor(tailwind::SLATE_50.with_alpha(0.5).into()),
)],
)
}
#[derive(Component)]
struct Cooldown(Timer);
#[derive(Component)]
#[component(storage = "SparseSet")]
struct ActiveCooldown;
fn activate_ability(
mut commands: Commands,
mut interaction_query: Query<
(
Entity,
&Interaction,
&mut Cooldown,
&Name,
Option<&ActiveCooldown>,
),
(Changed<Interaction>, With<Button>),
>,
mut text: Query<&mut Text>,
) -> Result {
for (entity, interaction, mut cooldown, name, on_cooldown) in &mut interaction_query {
if *interaction == Interaction::Pressed {
if on_cooldown.is_none() {
cooldown.0.reset();
commands.entity(entity).insert(ActiveCooldown);
**text.single_mut()? = format!("You ate {name}");
} else {
**text.single_mut()? = format!(
"You can eat {name} again in {} seconds.",
cooldown.0.remaining_secs().ceil()
);
}
}
}
Ok(())
}
fn animate_cooldowns(
time: Res<Time>,
mut commands: Commands,
buttons: Query<(Entity, &mut Cooldown, &Children), With<ActiveCooldown>>,
mut nodes: Query<&mut Node>,
) -> Result {
for (entity, mut timer, children) in buttons {
timer.0.tick(time.delta());
let cooldown = children.first().ok_or("No child")?;
if timer.0.just_finished() {
commands.entity(entity).remove::<ActiveCooldown>();
nodes.get_mut(*cooldown)?.height = percent(0);
} else {
nodes.get_mut(*cooldown)?.height = percent((1. - timer.0.fraction()) * 100.);
}
}
Ok(())
}