jong/src/game/hand.rs

59 lines
1.4 KiB
Rust
Raw Normal View History

2026-01-12 01:54:59 -08:00
use bevy::prelude::*;
2026-01-12 21:57:08 -08:00
use crate::{
game::{player::Player, wall::WallTiles},
tiles::Tile,
};
2026-01-12 01:54:59 -08:00
#[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>>,
2026-01-12 21:57:08 -08:00
players: Populated<Entity, With<Player>>,
2026-01-12 01:54:59 -08:00
) -> Result {
2026-01-12 21:57:08 -08:00
let mut wall = walltiles.iter().collect::<Vec<_>>();
2026-01-12 01:54:59 -08:00
2026-01-12 21:57:08 -08:00
for player_entity in players {
let hand = wall.split_off(13);
2026-01-12 01:54:59 -08:00
2026-01-12 21:57:08 -08:00
commands
.get_entity(*walltiles_entity)?
.remove_children(&hand);
let handtiles = commands.spawn((Hand, HandTiles(hand))).id();
commands
.get_entity(player_entity)?
.add_children(&[handtiles]);
}
2026-01-12 01:54:59 -08:00
trace!("dealt hands");
Ok(())
}
2026-01-12 21:07:34 -08:00
#[allow(clippy::type_complexity)]
2026-01-12 21:57:08 -08:00
pub(crate) fn sort_hands(
2026-01-12 01:54:59 -08:00
mut commands: Commands,
tiles: Populated<&Tile>,
2026-01-12 21:57:08 -08:00
mut hands: Populated<&mut Children, Changed<HandTiles>>,
2026-01-12 01:54:59 -08:00
) -> Result {
2026-01-12 21:57:08 -08:00
for (mut children) in hands {
children.sort_unstable_by_key(|e| tiles.get(*e).unwrap().suit);
trace!("sorted a hand")
}
2026-01-12 01:54:59 -08:00
2026-01-12 21:57:08 -08:00
trace!("sort_hands");
2026-01-12 01:54:59 -08:00
Ok(())
}