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

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

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

Loading…
Cancel
Save