2026-01-12 01:54:59 -08:00
|
|
|
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<Entity>);
|
|
|
|
|
|
|
|
|
|
pub(crate) fn deal_hands(
|
|
|
|
|
mut commands: Commands,
|
|
|
|
|
walltiles: Single<&WallTiles>,
|
|
|
|
|
walltiles_entity: Single<Entity, With<WallTiles>>,
|
|
|
|
|
) -> Result {
|
|
|
|
|
let hand = walltiles.iter().collect::<Vec<_>>();
|
|
|
|
|
|
|
|
|
|
commands
|
|
|
|
|
.get_entity(*walltiles_entity)?
|
|
|
|
|
.remove_children(hand.last_chunk::<13>().unwrap());
|
|
|
|
|
|
|
|
|
|
commands.spawn((Hand, HandTiles(hand)));
|
|
|
|
|
|
|
|
|
|
trace!("dealt hands");
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-12 21:07:34 -08:00
|
|
|
#[allow(clippy::type_complexity)]
|
|
|
|
|
pub fn sort_hands(
|
2026-01-12 01:54:59 -08:00
|
|
|
mut commands: Commands,
|
|
|
|
|
tiles: Populated<&Tile>,
|
|
|
|
|
handtiles_entity: Single<Entity, With<HandTiles>>,
|
|
|
|
|
handtiles: Single<&HandTiles, Changed<HandTiles>>,
|
|
|
|
|
) -> Result {
|
|
|
|
|
let mut hand: Vec<_> = handtiles
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|e| -> Result<(_, _)> { Ok((tiles.get(e)?, e)) })
|
|
|
|
|
.collect::<Result<_>>()?;
|
|
|
|
|
|
|
|
|
|
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(())
|
|
|
|
|
}
|