simplify game setup

This commit is contained in:
Tao Tien 2026-01-13 00:01:38 -08:00
parent f4c4339204
commit 30d19ed9d9
8 changed files with 145 additions and 171 deletions

View file

@ -1,58 +1,29 @@
use bevy::prelude::*;
use crate::{
game::{player::Player, wall::WallTiles},
game::{player::Player /* wall::WallTiles */},
tiles::Tile,
};
#[derive(Component)]
pub struct Hand;
#[derive(Component)]
#[relationship(relationship_target = HandTiles)]
pub struct InHand(pub Entity);
// #[derive(Component, Default)]
// enum SortHand {
// #[default]
// Unsorted,
// Sort,
// Manual,
// }
#[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>>,
players: Populated<Entity, With<Player>>,
) -> Result {
let mut wall = walltiles.iter().collect::<Vec<_>>();
for player_entity in players {
let hand = wall.split_off(13);
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(crate) fn sort_hands(
mut commands: Commands,
tiles: Populated<&Tile>,
mut hands: Populated<&mut Children, Changed<HandTiles>>,
mut hands: Populated<&mut Children, (With<Player>, Changed<Hand>)>,
) -> Result {
for (mut children) in hands {
children.sort_unstable_by_key(|e| tiles.get(*e).unwrap().suit);
trace!("sorted a hand")
for mut hand in hands {
hand.sort_unstable_by_key(|e| tiles.get(*e).unwrap().suit);
debug!("sorted: {hand:?}")
}
trace!("sort_hands");
Ok(())
}