render hand at index 0

This commit is contained in:
Tao Tien 2026-01-12 21:57:08 -08:00
parent a6079103a4
commit f4c4339204
7 changed files with 56 additions and 59 deletions

View file

@ -1,6 +1,9 @@
use bevy::prelude::*;
use crate::{game::wall::WallTiles, tiles::Tile};
use crate::{
game::{player::Player, wall::WallTiles},
tiles::Tile,
};
#[derive(Component)]
pub struct Hand;
@ -17,39 +20,39 @@ pub(crate) fn deal_hands(
mut commands: Commands,
walltiles: Single<&WallTiles>,
walltiles_entity: Single<Entity, With<WallTiles>>,
players: Populated<Entity, With<Player>>,
) -> Result {
let hand = walltiles.iter().collect::<Vec<_>>();
let mut wall = walltiles.iter().collect::<Vec<_>>();
commands
.get_entity(*walltiles_entity)?
.remove_children(hand.last_chunk::<13>().unwrap());
for player_entity in players {
let hand = wall.split_off(13);
commands.spawn((Hand, HandTiles(hand)));
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 fn sort_hands(
pub(crate) fn sort_hands(
mut commands: Commands,
tiles: Populated<&Tile>,
handtiles_entity: Single<Entity, With<HandTiles>>,
handtiles: Single<&HandTiles, Changed<HandTiles>>,
mut hands: Populated<&mut Children, Changed<HandTiles>>,
) -> Result {
let mut hand: Vec<_> = handtiles
.iter()
.map(|e| -> Result<(_, _)> { Ok((tiles.get(e)?, e)) })
.collect::<Result<_>>()?;
for (mut children) in hands {
children.sort_unstable_by_key(|e| tiles.get(*e).unwrap().suit);
trace!("sorted a hand")
}
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");
trace!("sort_hands");
Ok(())
}

View file

@ -1,6 +1,9 @@
use bevy::prelude::*;
use crate::tiles::{self, *};
use crate::{
game::player::MainPlayer,
tiles::{self, *},
};
pub mod hand;
pub mod player;
@ -24,7 +27,7 @@ impl Plugin for Riichi {
.add_systems(Startup, tiles::init_tiles)
.init_state::<GameState>()
.add_systems(OnEnter(GameState::Setup), (wall::build_wall, hand::deal_hands, setup_done).chain())
.add_systems(Update, (hand::sort_hand).run_if(in_state(GameState::Play)))
.add_systems(Update, (hand::sort_hands).run_if(in_state(GameState::Play)))
// semicolon stopper
;
}
@ -79,7 +82,7 @@ pub(crate) fn init_match(
player_count,
});
let players = (1..=player_count)
let players = (2..=player_count)
.map(|i| {
(
player::Player {
@ -90,6 +93,13 @@ pub(crate) fn init_match(
})
.collect::<Vec<_>>();
commands.spawn_batch(players);
let main_player = (
player::Player {
name: format!("Player {}", 1),
},
player::Points(starting),
);
commands.spawn((main_player, MainPlayer));
// *compass = Compass {
// prevalent_wind: Wind::Ton,

View file

@ -6,12 +6,12 @@ use crate::{
};
#[derive(Component)]
pub(crate) struct Player {
pub(crate) name: String,
pub struct Player {
pub name: String,
}
fn spawn_players(mut commands: Commands) {}
#[derive(Component)]
pub struct Points(pub isize);
#[derive(Component)]
pub(crate) struct Points(pub isize);
pub struct MainPlayer;

View file

@ -43,7 +43,7 @@ fn main() {
tui_logger::init_logger(tui_logger::LevelFilter::Trace).unwrap();
tui_logger::set_env_filter_from_string(FILTERSTRING);
app.add_plugins(tui::RiichiTui::default())
app.add_plugins(tui::RiichiTui)
}
};

View file

@ -5,8 +5,8 @@ use tui_logger::TuiLoggerWidget;
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Default, States)]
pub(crate) enum ConsoleState {
#[default]
Closed,
#[default]
Open,
}

View file

@ -7,44 +7,26 @@ use jong::tiles::Tile;
use crate::tui::render::tiles;
#[derive(Resource, Default)]
pub(crate) struct RenderedHand(pub(crate) Vec<Paragraph<'static>>);
pub(crate) struct RenderedHand(pub(crate) Vec<Vec<Paragraph<'static>>>);
pub(crate) fn render_changed_hand(
hand: Single<&HandTiles, Changed<HandTiles>>,
hands: Populated<&Children, Changed<HandTiles>>,
tiles: Populated<&Tile>,
mut target: ResMut<RenderedHand>,
) -> Result {
let mut rendered = vec![];
for hand in hands {
let hand_tiles = hand
let tiles = hand
.iter()
.map(|inhand| -> Result<_> { Ok(tiles.get(inhand).map(tiles::draw_tile)?) })
.collect::<Result<Vec<_>>>()?;
rendered.push(hand_tiles);
.map(|inhand| tiles.get(inhand).map(tiles::draw_tile).unwrap())
.collect();
rendered.push(tiles);
}
target.0 = rendered;
trace!("render_changed_hand");
render_hand(hand, tiles, target)?;
Ok(())
}
pub(crate) fn render_hand(
hand: Single<&HandTiles, Changed<HandTiles>>,
tiles: Populated<&Tile>,
mut target: ResMut<RenderedHand>,
) -> Result {
trace!("render_hand");
let hand_tiles = hand
.iter()
.map(|inhand| -> Result<_> { Ok(tiles.get(inhand).map(tiles::draw_tile)?) })
.collect::<Result<Vec<_>>>()?;
target.0 = hand_tiles;
Ok(())
}

View file

@ -17,8 +17,10 @@ pub(crate) fn draw_ingame(
let mut area = frame.area();
area.height = 4;
let areas = layout.areas::<13>(area);
for (tile, area) in rendered_hand.0.iter().zip(areas.iter()) {
frame.render_widget(tile, *area);
if let Some(tiles) = rendered_hand.0.first() {
for (tile, area) in tiles.iter().zip(areas.iter()) {
frame.render_widget(tile, *area);
}
}
})?;