use bevy::prelude::*; use crate::{game::wall::WallTiles, tiles::Tile}; #[derive(Component)] pub struct Hand; #[derive(Component)] #[relationship(relationship_target = HandTiles)] pub struct InHand(pub Entity); #[derive(Component)] #[relationship_target(relationship = InHand, linked_spawn)] pub struct HandTiles(Vec); pub(crate) fn deal_hands( mut commands: Commands, walltiles: Single<&WallTiles>, walltiles_entity: Single>, ) -> Result { let hand = walltiles.iter().collect::>(); commands .get_entity(*walltiles_entity)? .remove_children(hand.last_chunk::<13>().unwrap()); commands.spawn((Hand, HandTiles(hand))); trace!("dealt hands"); Ok(()) } #[allow(clippy::type_complexity)] pub fn sort_hands( mut commands: Commands, tiles: Populated<&Tile>, handtiles_entity: Single>, handtiles: Single<&HandTiles, Changed>, ) -> Result { let mut hand: Vec<_> = handtiles .iter() .map(|e| -> Result<(_, _)> { Ok((tiles.get(e)?, e)) }) .collect::>()?; hand.sort_by_key(|(t, _)| t.suit); let hand: Vec<_> = hand.iter().map(|(_, e)| *e).collect(); commands .get_entity(*handtiles_entity)? .replace_children(&hand); trace!("sort_hand"); Ok(()) }