|
|
|
|
@ -6,7 +6,8 @@ pub struct PlayPlugin;
|
|
|
|
|
|
|
|
|
|
impl Plugin for PlayPlugin {
|
|
|
|
|
fn build(&self, app: &mut App) {
|
|
|
|
|
app.add_systems(OnEnter(GameState::Play), serve_cards)
|
|
|
|
|
app.add_observer(serve_new_cards)
|
|
|
|
|
.add_systems(OnEnter(GameState::Play), serve_cards)
|
|
|
|
|
.add_systems(Update, rotate_cards);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
@ -64,20 +65,27 @@ pub(crate) fn reset_rotation(
|
|
|
|
|
/// Check a set when the "Set" button is clicked
|
|
|
|
|
pub(crate) fn check_set(
|
|
|
|
|
_trigger: Trigger<Pointer<Click>>,
|
|
|
|
|
query: Query<(&Card, Entity), With<Selected>>,
|
|
|
|
|
mut query: Query<(Entity, &Card, &mut Visibility, &mut Transform), With<Selected>>,
|
|
|
|
|
mut commands: Commands,
|
|
|
|
|
) {
|
|
|
|
|
let mut cards = query.iter();
|
|
|
|
|
if cards.len() == 3 {
|
|
|
|
|
let ((a, _), (b, _), (c, _)) = (
|
|
|
|
|
let ((_, a, _, _), (_, b, _, _), (_, c, _, _)) = (
|
|
|
|
|
cards.next().unwrap(),
|
|
|
|
|
cards.next().unwrap(),
|
|
|
|
|
cards.next().unwrap(),
|
|
|
|
|
);
|
|
|
|
|
if is_set((a, b, c)) {
|
|
|
|
|
query.iter().for_each(|(_, e)| {
|
|
|
|
|
// TODO: Figure out something better to do than despawning!
|
|
|
|
|
commands.entity(e).despawn_recursive();
|
|
|
|
|
query
|
|
|
|
|
.iter_mut()
|
|
|
|
|
.for_each(|(entity, _, mut visibility, mut transform)| {
|
|
|
|
|
*visibility = Visibility::Hidden;
|
|
|
|
|
*transform = Transform::default();
|
|
|
|
|
let set_number = 1;
|
|
|
|
|
commands
|
|
|
|
|
.entity(entity)
|
|
|
|
|
.remove::<PlayLocation>()
|
|
|
|
|
.insert(SetNumber(set_number));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
} else if cards.len() > 3 {
|
|
|
|
|
@ -116,7 +124,39 @@ fn serve_cards(
|
|
|
|
|
) {
|
|
|
|
|
let mut target = (cards.iter().len() - 1) as u8;
|
|
|
|
|
// Iterate over every x, y play location
|
|
|
|
|
for this_x in 0..=2 {
|
|
|
|
|
for this_x in 0..=3 {
|
|
|
|
|
for this_y in 0..=3 {
|
|
|
|
|
// If this spot does not have a card
|
|
|
|
|
if spots
|
|
|
|
|
.iter()
|
|
|
|
|
.find(|PlayLocation { x, y }| *x == (this_x as u8) && *y == (this_y as u8))
|
|
|
|
|
.is_none()
|
|
|
|
|
{
|
|
|
|
|
if let Some((e, _)) = cards.iter().find(|(_, deck_order)| deck_order.0 == target) {
|
|
|
|
|
// Set the selected card to this play location and remove from the deck
|
|
|
|
|
commands
|
|
|
|
|
.entity(e)
|
|
|
|
|
.remove::<DeckOrder>()
|
|
|
|
|
.insert(PlayLocation {
|
|
|
|
|
x: this_x,
|
|
|
|
|
y: this_y,
|
|
|
|
|
});
|
|
|
|
|
target = target - 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn serve_new_cards(
|
|
|
|
|
_trigger: Trigger<OnAdd, SetNumber>,
|
|
|
|
|
cards: Query<(Entity, &DeckOrder)>,
|
|
|
|
|
spots: Query<&PlayLocation>,
|
|
|
|
|
mut commands: Commands,
|
|
|
|
|
) {
|
|
|
|
|
let mut target = (cards.iter().len() - 1) as u8;
|
|
|
|
|
// Iterate over every x, y play location
|
|
|
|
|
for this_x in 0..=3 {
|
|
|
|
|
for this_y in 0..=3 {
|
|
|
|
|
// If this spot does not have a card
|
|
|
|
|
if spots
|
|
|
|
|
@ -150,7 +190,7 @@ pub(crate) fn place_card(
|
|
|
|
|
|
|
|
|
|
// Set it's transform based on it's placement
|
|
|
|
|
let CARD_SIZE = [100.0, 160.0];
|
|
|
|
|
let offset = Vec2::new(CARD_SIZE[0] * 3.0, CARD_SIZE[1] * 4.0) / 2.0;
|
|
|
|
|
let offset = Vec2::new(CARD_SIZE[0] * 4.0, CARD_SIZE[1] * 4.0) / 2.5;
|
|
|
|
|
transform.translation = Vec3::new(
|
|
|
|
|
((*x as f32) * CARD_SIZE[0]) - offset.x,
|
|
|
|
|
((*y as f32) * CARD_SIZE[1]) - offset.y,
|
|
|
|
|
|