render hand at index 0

This commit is contained in:
Tao Tien 2026-01-12 21:57:08 -08:00
parent a6079103a4
commit f4c4339204
7 changed files with 56 additions and 59 deletions

View file

@ -1,6 +1,9 @@
use bevy::prelude::*;
use crate::{game::wall::WallTiles, tiles::Tile};
use crate::{
game::{player::Player, wall::WallTiles},
tiles::Tile,
};
#[derive(Component)]
pub struct Hand;
@ -17,39 +20,39 @@ pub(crate) fn deal_hands(
mut commands: Commands,
walltiles: Single<&WallTiles>,
walltiles_entity: Single<Entity, With<WallTiles>>,
players: Populated<Entity, With<Player>>,
) -> Result {
let hand = walltiles.iter().collect::<Vec<_>>();
let mut wall = walltiles.iter().collect::<Vec<_>>();
commands
.get_entity(*walltiles_entity)?
.remove_children(hand.last_chunk::<13>().unwrap());
for player_entity in players {
let hand = wall.split_off(13);
commands.spawn((Hand, HandTiles(hand)));
commands
.get_entity(*walltiles_entity)?
.remove_children(&hand);
let handtiles = commands.spawn((Hand, HandTiles(hand))).id();
commands
.get_entity(player_entity)?
.add_children(&[handtiles]);
}
trace!("dealt hands");
Ok(())
}
#[allow(clippy::type_complexity)]
pub fn sort_hands(
pub(crate) fn sort_hands(
mut commands: Commands,
tiles: Populated<&Tile>,
handtiles_entity: Single<Entity, With<HandTiles>>,
handtiles: Single<&HandTiles, Changed<HandTiles>>,
mut hands: Populated<&mut Children, Changed<HandTiles>>,
) -> Result {
let mut hand: Vec<_> = handtiles
.iter()
.map(|e| -> Result<(_, _)> { Ok((tiles.get(e)?, e)) })
.collect::<Result<_>>()?;
for (mut children) in hands {
children.sort_unstable_by_key(|e| tiles.get(*e).unwrap().suit);
trace!("sorted a hand")
}
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");
trace!("sort_hands");
Ok(())
}