jong/src/game/hand.rs

67 lines
1.7 KiB
Rust
Raw Normal View History

2026-01-13 01:08:14 -08:00
use bevy::{ecs::relationship::RelationshipSourceCollection, prelude::*};
2026-01-12 01:54:59 -08:00
2026-01-12 21:57:08 -08:00
use crate::{
2026-01-13 12:38:41 -08:00
game::{GameState, player::Player, wall::Wall /* wall::WallTiles */},
tile::Tile,
2026-01-12 21:57:08 -08:00
};
2026-01-12 01:54:59 -08:00
#[derive(Component)]
pub struct Hand;
2026-01-13 12:38:41 -08:00
#[derive(Component)]
pub struct Drawn(pub Entity);
2026-01-13 00:01:38 -08:00
// #[derive(Component, Default)]
// enum SortHand {
// #[default]
// Unsorted,
// Sort,
// Manual,
// }
2026-01-12 21:57:08 -08:00
pub(crate) fn sort_hands(
2026-01-12 01:54:59 -08:00
tiles: Populated<&Tile>,
2026-01-13 01:08:14 -08:00
mut hands: Populated<&mut Children, (Changed<Hand>, Without<Player>)>,
2026-01-12 01:54:59 -08:00
) -> Result {
2026-01-13 00:01:38 -08:00
for mut hand in hands {
hand.sort_unstable_by_key(|e| tiles.get(*e).unwrap().suit);
2026-01-13 01:08:14 -08:00
debug!("sorted: {hand:?}");
2026-01-12 21:57:08 -08:00
}
2026-01-12 01:54:59 -08:00
Ok(())
}
2026-01-13 12:38:41 -08:00
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(())
}