use bevy::{ecs::relationship::RelationshipSourceCollection, prelude::*}; use crate::{ game::{GameState, player::Player, wall::Wall /* wall::WallTiles */}, tile::Tile, }; #[derive(Component)] pub struct Hand; #[derive(Component)] pub struct Drawn(pub Entity); // #[derive(Component, Default)] // enum SortHand { // #[default] // Unsorted, // Sort, // Manual, // } pub(crate) fn sort_hands( tiles: Populated<&Tile>, mut hands: Populated<&mut Children, (Changed, Without)>, ) -> Result { for mut hand in hands { hand.sort_unstable_by_key(|e| tiles.get(*e).unwrap().suit); debug!("sorted: {hand:?}"); } Ok(()) } pub(crate) fn shuffle_deal( mut commands: Commands, tiles: Populated>, players: Populated>, wall_ent: Single>, mut next_gamestate: ResMut>, ) -> Result { use rand::seq::SliceRandom; let mut rng = rand::rng(); let mut walltiles: Vec<_> = tiles.iter().collect(); walltiles.shuffle(&mut rng); // commands.get_entity(*wall_ent)?.replace_children(&walltiles); for player_ent in players { let handtiles = walltiles.split_off(walltiles.len() - 13); // commands.get_entity(*wall_ent)?.remove_children(&handtiles); let hand_ent = commands.spawn(Hand).add_children(&handtiles).id(); debug!("hand_ent: {hand_ent:?}"); commands .get_entity(player_ent)? .replace_children(&[hand_ent]); debug!("dealt to player_ent {player_ent:?}"); } commands.get_entity(*wall_ent)?.replace_children(&walltiles); next_gamestate.set(GameState::Play); Ok(()) }