I believe this is a more robust solution to the set helper

main
Elijah Voigt 2 years ago
parent 6e1fdb5207
commit f8ce9b41b2

@ -64,7 +64,7 @@ pub(crate) fn reset_rotation(
/// 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>>, mut query: Query<(Entity, &Card, &mut Visibility, &mut Transform), With<Selected>>,
mut commands: Commands, mut commands: Commands,
) { ) {
@ -205,47 +205,49 @@ pub(crate) fn check_for_sets(
_trigger: Trigger<Pointer<Click>>, _trigger: Trigger<Pointer<Click>>,
cards: Query<(Entity, &Card), With<PlayLocation>>, cards: Query<(Entity, &Card), With<PlayLocation>>,
selected: Query<Entity, With<Selected>>, selected: Query<Entity, With<Selected>>,
mut help_set: Local<Option<(Entity, Entity, Entity)>>,
mut commands: Commands, mut commands: Commands,
) { ) {
match *help_set { cards
None => { // Iterate over all combinations of cards on the board
*help_set = cards .iter_combinations()
.iter_combinations() // Only find combinations including currently selected cards
.find_map(|[(ea, ca), (eb, cb), (ec, cc)]| { .filter(|[(ea, _ca), (eb, _cb), (ec, _cc)]| {
(is_set((ca, cb, cc))).then_some((ea, eb, ec)) // If selected is empty, proceed
}); match selected.iter().len() {
} 0 => true,
Some((a, b, c)) => { 1 | 2 | 3 => {
if cards.get_many([a, b, c]).is_err() { let a = selected.contains(*ea);
*help_set = cards let b = selected.contains(*eb);
.iter_combinations() let c = selected.contains(*ec);
.find_map(|[(ea, ca), (eb, cb), (ec, cc)]| { match selected.iter().len() {
(is_set((ca, cb, cc))).then_some((ea, eb, ec)) 1 => a || b || c,
}); 2 => a && b || b && c || a && c,
3 => a && b && c,
_ => panic!("WTF?"),
}
}
_ => panic!("Impossible!"),
} }
} })
} // return the first valid set
.find_map(|[(ea, ca), (eb, cb), (ec, cc)]| {
// TODO: Check if help_set cards are all still on board, if not set to None if is_set((ca, cb, cc)) {
info!("\n\t{}\n\t{}\n\t{}", ca, cb, cc);
help_set.iter().for_each(|(a, b, c)| { Some((ea, eb, ec))
match ( } else {
selected.contains(*a), None
selected.contains(*b),
selected.contains(*c),
) {
(false, false, false) => {
commands.entity(*a).insert(Selected);
} }
(true, false, false) => { })
.iter()
.for_each(|(a, b, c)| {
if !selected.contains(*a) {
commands.entity(*a).insert(Selected);
} else if !selected.contains(*b) {
commands.entity(*b).insert(Selected); commands.entity(*b).insert(Selected);
} } else if !selected.contains(*c) {
(true, true, false) => {
commands.entity(*c).insert(Selected); commands.entity(*c).insert(Selected);
} else {
info!("I cannot hold your hand further...");
} }
(true, true, true) => info!("I can't hold your hand any further."), });
_ => panic!("How???"),
}
});
} }

Loading…
Cancel
Save