-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New cooldown example #19234
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
alice-i-cecile
merged 14 commits into
bevyengine:main
from
NiklasEi:cooldown-usage-example
May 26, 2025
Merged
New cooldown example #19234
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
fc98373
Initial cooldown example
NiklasEi 8e85b3d
Add the new example category to the example readme
NiklasEi ed4604c
Please clippy
NiklasEi 536392d
Use food textures from kenney
NiklasEi 1eb4494
Generate the example readme again
NiklasEi b8133ed
Do not use the animation player
NiklasEi 4b64eb1
Do not import from internal bevy crate
NiklasEi c00ce94
Update examples/usage/cooldown.rs
NiklasEi 85db273
Update examples/usage/cooldown.rs
NiklasEi b052ef6
Make Ability a struct
NiklasEi 0e6b324
Update examples/usage/cooldown.rs
NiklasEi 1b6e919
More comments and rename FoodItem
NiklasEi c1d599b
Handle clicks on food on cooldown
NiklasEi 7a60c0c
Merge branch 'main' of github.com:bevyengine/bevy into cooldown-usage…
NiklasEi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,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: Val::Percent(100.), | ||
| height: Val::Percent(100.), | ||
| align_items: AlignItems::Center, | ||
| justify_content: JustifyContent::Center, | ||
| column_gap: Val::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: Val::Px(5.0), | ||
| left: Val::Px(15.0), | ||
| ..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 { | ||
NiklasEi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| width: Val::Px(80.0), | ||
| height: Val::Px(80.0), | ||
| 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: Val::Percent(100.), | ||
| height: Val::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 = Val::Percent(0.); | ||
| } else { | ||
| nodes.get_mut(*cooldown)?.height = Val::Percent((1. - timer.0.fraction()) * 100.); | ||
| } | ||
| } | ||
|
|
||
| Ok(()) | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.