"figured out" z-ordering

might write a blog about it...
main
Elijah Voigt 1 year ago
parent 7bcfcf0954
commit 1cdba9b75a

@ -1,6 +1,6 @@
use bevy::{prelude::*, utils::RandomState}; use bevy::{prelude::*, utils::RandomState};
use crate::{deck::Card, menu::UiMessage, setup::AnimationStore, GameState}; use crate::{deck::Card, menu::UiMessage, setup::AnimationStore, view::ViewState};
pub struct PlayPlugin; pub struct PlayPlugin;
@ -8,7 +8,7 @@ impl Plugin for PlayPlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.add_event::<ServeCards>() app.add_event::<ServeCards>()
.add_observer(serve_cards) .add_observer(serve_cards)
.add_systems(OnEnter(GameState::Main), deal_cards) .add_systems(OnEnter(ViewState::Play), deal_cards)
.add_systems(Update, deal_cards.run_if(set_added)) .add_systems(Update, deal_cards.run_if(set_added))
.add_systems(Update, delayed_animation); .add_systems(Update, delayed_animation);
} }
@ -54,20 +54,20 @@ pub(crate) fn play_selected_animation(
store: Res<AnimationStore>, store: Res<AnimationStore>,
mut commands: Commands, mut commands: Commands,
) { ) {
let e = trigger.entity();
info!("Play selected animation {:?}", e);
let (g, ai) = store.store.get("rotate".into()).unwrap(); let (g, ai) = store.store.get("rotate".into()).unwrap();
commands.entity(e).insert(g.clone()); commands.entity(trigger.entity()).insert(g.clone());
query.get_mut(e).unwrap().play(ai.clone()).repeat(); query
.get_mut(trigger.entity())
.unwrap()
.play(ai.clone())
.repeat();
} }
pub(crate) fn stop_selected_animation( pub(crate) fn stop_selected_animation(
trigger: Trigger<OnRemove, Selected>, trigger: Trigger<OnRemove, Selected>,
mut query: Query<(&mut Transform, &mut AnimationPlayer)>, mut query: Query<(&mut Transform, &mut AnimationPlayer)>,
) { ) {
let e = trigger.entity(); let (mut t, mut ap) = query.get_mut(trigger.entity()).unwrap();
info!("Stop selected animation {:?}", e);
let (mut t, mut ap) = query.get_mut(e).unwrap();
ap.stop_all(); ap.stop_all();
t.rotation = Quat::default(); t.rotation = Quat::default();
} }
@ -75,9 +75,11 @@ pub(crate) fn stop_selected_animation(
/// Check a set when the "Set" button is clicked /// Check a set when the "Set" button is clicked
pub(crate) fn check_set( pub(crate) fn check_set(
_trigger: Trigger<Pointer<Click>>, _trigger: Trigger<Pointer<Click>>,
mut query: Query<(Entity, &Card, &mut Visibility, &mut Transform), With<Selected>>, query: Query<(Entity, &Card, &PlayLocation, &Parent), With<Selected>>,
mut transforms: Query<&mut Transform>,
sets: Query<&SetNumber>, sets: Query<&SetNumber>,
mut commands: Commands, mut commands: Commands,
animation_store: Res<AnimationStore>,
) { ) {
let mut cards = query.iter(); let mut cards = query.iter();
if cards.len() == 3 { if cards.len() == 3 {
@ -86,20 +88,35 @@ pub(crate) fn check_set(
cards.next().unwrap(), cards.next().unwrap(),
cards.next().unwrap(), cards.next().unwrap(),
); );
match is_set((a, b, c)) { match is_set((a, b, c)) {
Ok(()) => { Ok(()) => {
query query.iter().for_each(|(entity, _, play_location, parent)| {
.iter_mut() let (graph_handle, animation_index) = animation_store
.for_each(|(entity, _, mut visibility, mut transform)| { .store
*visibility = Visibility::Hidden; .get(&format!(
*transform = Transform::default(); "{:?}->discard",
let set_number = sets.iter().len() as u8 / 3 + 1; (play_location.x, play_location.y)
commands ))
.entity(entity) .unwrap();
.remove::<PlayLocation>()
.remove::<Selected>() let set_number = sets.iter().len() as u8 / 3 + 1;
.insert(SetNumber(set_number));
}); // Adjust Z-location for proper discard animation
transforms.get_mut(parent.get()).unwrap().translation.z = set_number as f32;
commands
.entity(entity)
.remove::<PlayLocation>()
.remove::<Selected>()
.insert(DelayedAnimation {
delay: Timer::from_seconds(0.1, TimerMode::Once),
graph: graph_handle.clone(),
animation_index: animation_index.clone(),
})
.insert(SetNumber(set_number))
.insert(PickingBehavior::IGNORE);
});
commands.trigger(UiMessage("Yipee!".into())); commands.trigger(UiMessage("Yipee!".into()));
} }
Err(invalid_set_err) => { Err(invalid_set_err) => {
@ -226,54 +243,50 @@ fn set_added(query: Query<Entity, Added<SetNumber>>) -> bool {
#[derive(Component)] #[derive(Component)]
struct DelayedAnimation { struct DelayedAnimation {
graph: AnimationGraphHandle, graph: AnimationGraphHandle,
animation_index: AnimationNodeIndex,
delay: Timer, delay: Timer,
} }
#[derive(Event, Clone)]
pub(crate) struct AnimationComplete;
/// When a card is added to the board, place it on the screen /// When a card is added to the board, place it on the screen
pub(crate) fn place_card( pub(crate) fn place_card(
trigger: Trigger<OnAdd, PlayLocation>, trigger: Trigger<OnAdd, PlayLocation>,
mut query: Query<( mut query: Query<(Entity, &PlayLocation, &mut Visibility)>,
Entity,
&PlayLocation,
&mut Visibility,
&Name,
&mut AnimationPlayer,
)>,
animation_store: Res<AnimationStore>, animation_store: Res<AnimationStore>,
mut commands: Commands, mut commands: Commands,
random_state: Local<RandomState>, random_state: Local<RandomState>,
) { ) {
let (entity, play_location, mut visibility, name, mut player) = let (entity, play_location, mut visibility) = query.get_mut(trigger.entity()).unwrap();
query.get_mut(trigger.entity()).unwrap();
let (graph_handle, animation_index) = animation_store let (graph_handle, animation_index) = animation_store
.store .store
.get(&format!("deck->{:?}", (play_location.x, play_location.y))) .get(&format!("deck->{:?}", (play_location.x, play_location.y)))
.unwrap(); .unwrap();
player.play(animation_index.clone());
let delay = ((random_state.hash_one(entity) % 9) as f32 / 10.0) + 0.1; let delay = ((random_state.hash_one(entity) % 9) as f32 / 10.0) + 0.1;
info!("Delay: {:?}", delay); commands.entity(entity).insert(DelayedAnimation {
commands.entity(entity).insert((DelayedAnimation { animation_index: animation_index.clone(),
graph: graph_handle.clone(),
delay: Timer::from_seconds(delay, TimerMode::Once), delay: Timer::from_seconds(delay, TimerMode::Once),
},)); graph: graph_handle.clone(),
});
// Set it to visible // Set it to visible
*visibility = Visibility::Inherited; *visibility = Visibility::Inherited;
} }
fn delayed_animation( fn delayed_animation(
mut query: Query<(Entity, &mut DelayedAnimation)>, mut query: Query<(Entity, &mut DelayedAnimation, &mut AnimationPlayer)>,
mut commands: Commands, mut commands: Commands,
time: Res<Time>, time: Res<Time>,
) { ) {
query query
.iter_mut() .iter_mut()
.for_each(|(entity, mut delayed_animation)| { .for_each(|(entity, mut delayed_animation, mut animation_player)| {
delayed_animation.delay.tick(time.delta()); delayed_animation.delay.tick(time.delta());
if delayed_animation.delay.just_finished() { if delayed_animation.delay.just_finished() {
animation_player.play(delayed_animation.animation_index);
commands commands
.entity(entity) .entity(entity)
.insert(delayed_animation.graph.clone()) .insert(delayed_animation.graph.clone())

@ -6,6 +6,7 @@ use bevy::{
prelude::*, prelude::*,
utils::HashMap, utils::HashMap,
}; };
use play::AnimationComplete;
use view::ViewState; use view::ViewState;
pub struct SetupPlugin; pub struct SetupPlugin;
@ -66,29 +67,33 @@ pub(crate) fn setup_cards(mut commands: Commands, deck: Res<Deck>) {
Transform::default().with_translation(Vec3::new(-400.0, -200.0, 0.0)); Transform::default().with_translation(Vec3::new(-400.0, -200.0, 0.0));
let visibility = Visibility::Hidden; let visibility = Visibility::Hidden;
let mut child = parent.spawn_empty(); // Spawn card with a simple Transform parent so we can adjust the Z-axis for
let entity_id = child.id(); // card ordering
let animation_target = AnimationTarget { parent.spawn(Transform::default()).with_children(|parent| {
id: animation_target_id, let mut child = parent.spawn_empty();
player: entity_id, let entity_id = child.id();
}; let animation_target = AnimationTarget {
child id: animation_target_id,
.insert(( player: entity_id,
animation_player, };
animation_target, child
name, .insert((
order, animation_player,
this_card, animation_target,
this_sprite, name,
this_transform, order,
visibility, this_card,
)) this_sprite,
.observe(play::place_card) this_transform,
.observe(debug::set_debug_card) visibility,
.observe(debug::hide_debug_card) ))
.observe(play::play_selected_animation) .observe(play::place_card)
.observe(play::stop_selected_animation) .observe(debug::set_debug_card)
.observe(play::toggle_selected); .observe(debug::hide_debug_card)
.observe(play::play_selected_animation)
.observe(play::stop_selected_animation)
.observe(play::toggle_selected);
});
}); });
}); });
} }
@ -149,6 +154,8 @@ fn setup_animations(
); );
animation.add_curve_to_target(*target, curve); animation.add_curve_to_target(*target, curve);
}); });
animation.set_duration(1.0);
animation.add_event(1.0, AnimationComplete);
let animation_handle = clips.add(animation); let animation_handle = clips.add(animation);
let (graph, animation_index) = AnimationGraph::from_clip(animation_handle); let (graph, animation_index) = AnimationGraph::from_clip(animation_handle);
let graph_handle = AnimationGraphHandle(graphs.add(graph)); let graph_handle = AnimationGraphHandle(graphs.add(graph));
@ -169,6 +176,8 @@ fn setup_animations(
); );
animation.add_curve_to_target(*target, curve); animation.add_curve_to_target(*target, curve);
}); });
animation.set_duration(1.0);
animation.add_event(1.0, AnimationComplete);
let animation_handle = clips.add(animation); let animation_handle = clips.add(animation);
let (graph, animation_index) = AnimationGraph::from_clip(animation_handle); let (graph, animation_index) = AnimationGraph::from_clip(animation_handle);
let graph_handle = AnimationGraphHandle(graphs.add(graph)); let graph_handle = AnimationGraphHandle(graphs.add(graph));
@ -195,6 +204,7 @@ pub(crate) fn setup_camera(mut commands: Commands) {
hdr: true, hdr: true,
..default() ..default()
}, },
Transform::default().with_translation(Vec3::new(0.0, 0.0, 100.0)),
)); ));
} }

@ -1,11 +1,8 @@
TODO: TODO:
* BUG: Help does not animate rotating cards * A deck with face-down card(s)
* Make "set" button visually interesting when 3 cards selected * Make "set" button visually interesting when 3 cards selected
* Animate cards deck -> board
* Animate cards deck -> set-pile
* Better shuffling * Better shuffling
Later: Later:
* View all cards with some indication of in-set * View all cards with some indication of in-set
* Music! (w/ sam)
* Make button(s) look pretty * Make button(s) look pretty

Loading…
Cancel
Save