jong/src/game/hand.rs
2026-01-13 13:41:18 -08:00

66 lines
1.7 KiB
Rust

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<Hand>, Without<Player>)>,
) -> 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<Entity, With<Tile>>,
players: Populated<Entity, With<Player>>,
wall_ent: Single<Entity, With<Wall>>,
mut next_gamestate: ResMut<NextState<GameState>>,
) -> 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(())
}