55 lines
1.3 KiB
Rust
55 lines
1.3 KiB
Rust
|
|
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(())
|
||
|
|
}
|
||
|
|
|
||
|
|
pub(crate) fn sort_hand(
|
||
|
|
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(())
|
||
|
|
}
|