Make flap and rewind buttons shimmer

main
Elijah Voigt 2 months ago
parent a2367fb4f7
commit 666d8f52b6

@ -39,9 +39,11 @@ fn main() {
), ),
) )
.add_systems(OnEnter(PlayerState::Alive), alive_bird) .add_systems(OnEnter(PlayerState::Alive), alive_bird)
.add_systems(OnEnter(PlayerState::Alive), reset_button::<FlapButton>)
.add_systems(OnEnter(PlayerState::Rewind), (start_rewinding, alive_bird)) .add_systems(OnEnter(PlayerState::Rewind), (start_rewinding, alive_bird))
.add_systems(OnEnter(PlayerState::Pause), pause_bird) .add_systems(OnEnter(PlayerState::Pause), pause_bird)
.add_systems(OnEnter(PlayerState::Stasis), pause_bird) .add_systems(OnEnter(PlayerState::Stasis), pause_bird)
.add_systems(OnExit(PlayerState::Stasis), reset_button::<RewindButton>)
.add_systems( .add_systems(
Update, Update,
( (
@ -93,6 +95,8 @@ fn main() {
update_batch_position.run_if(any_component_changed::<Batch>), update_batch_position.run_if(any_component_changed::<Batch>),
move_batches.run_if(on_event::<CollisionStarted>), move_batches.run_if(on_event::<CollisionStarted>),
manage_score.run_if(on_event::<CollisionStarted>.or(on_event::<CollisionEnded>)), manage_score.run_if(on_event::<CollisionStarted>.or(on_event::<CollisionEnded>)),
shimmer_button::<RewindButton>.run_if(in_state(PlayerState::Stasis)),
shimmer_button::<FlapButton>.run_if(in_state(PlayerState::Pause)),
), ),
) )
.add_observer(flap) .add_observer(flap)
@ -407,12 +411,18 @@ fn init_assets(
#[derive(Component)] #[derive(Component)]
struct FlapSfx; struct FlapSfx;
#[derive(Component)]
struct FlapButton;
#[derive(Component)] #[derive(Component)]
struct BonkSfx; struct BonkSfx;
#[derive(Component)] #[derive(Component)]
struct RewindSfx; struct RewindSfx;
#[derive(Component)]
struct RewindButton;
fn init_ui(mut commands: Commands) { fn init_ui(mut commands: Commands) {
commands commands
.spawn(( .spawn((
@ -532,11 +542,9 @@ fn init_ui(mut commands: Commands) {
flex_direction: FlexDirection::Column, flex_direction: FlexDirection::Column,
..default() ..default()
}, },
Button,
PlayerState::Pause, PlayerState::Pause,
children![Text::new("Go!"),], children![Text::new("Go!")],
)) ));
.observe(start_game);
commands commands
.spawn(Node { .spawn(Node {
@ -550,13 +558,15 @@ fn init_ui(mut commands: Commands) {
.spawn(( .spawn((
Node { ..default() }, Node { ..default() },
Button, Button,
BackgroundColor::default(),
RewindButton,
children![Text::new("Rewind!"),], children![Text::new("Rewind!"),],
)) ))
.observe(start_rewind) .observe(start_rewind)
.observe(end_rewind); .observe(end_rewind);
parent parent
.spawn((Node { ..default() }, Button, children![Text::new("Flap!"),])) .spawn((Node { ..default() }, Button, FlapButton, children![Text::new("Flap!"),]))
.observe(flap_button); .observe(flap_button);
}); });
@ -585,7 +595,12 @@ fn flap_button(
_trigger: Trigger<Pointer<Pressed>>, _trigger: Trigger<Pointer<Pressed>>,
mut commands: Commands, mut commands: Commands,
bird: Single<Entity, With<Bird>>, bird: Single<Entity, With<Bird>>,
curr: Res<State<PlayerState>>,
mut next: ResMut<NextState<PlayerState>>,
) { ) {
if !matches!(curr.get(), PlayerState::Alive) {
next.set(PlayerState::Alive);
}
let e = *bird; let e = *bird;
debug!("Flapping {:?}", e); debug!("Flapping {:?}", e);
commands.trigger_targets(Flap, e); commands.trigger_targets(Flap, e);
@ -994,3 +1009,21 @@ fn start_rewinding(
RewindSfx, RewindSfx,
)); ));
} }
fn shimmer_button<T: Component>(
mut bg: Single<&mut BackgroundColor, With<T>>,
time: Res<Time>,
) {
let t = time.elapsed_secs();
let period = 3.0;
let r = (((t / period) % 1.0) * std::f32::consts::PI).cos();
let g = ((((t / period) + 0.3) % 1.0) * std::f32::consts::PI).cos();
let b = ((((t / period) + 0.6) % 1.0) * std::f32::consts::PI).cos();
bg.0 = Srgba::rgb(r, g, b).into();
}
fn reset_button<T: Component>(
mut bg: Single<&mut BackgroundColor, With<T>>,
) {
bg.0 = WHITE.into();
}

Loading…
Cancel
Save