From faa653b012c38fce46b78bda0cac5472e02b572d Mon Sep 17 00:00:00 2001 From: Tao Tien <29749622+taotien@users.noreply.github.com> Date: Sun, 18 Jan 2026 10:52:09 -0800 Subject: [PATCH] render drawn, discard drawn --- src/game.rs | 1 + src/game/round.rs | 15 +++++++-------- src/tui.rs | 3 +-- src/tui/render.rs | 25 +++++++++++++++---------- 4 files changed, 24 insertions(+), 20 deletions(-) diff --git a/src/game.rs b/src/game.rs index 09b4e82..35b5e50 100644 --- a/src/game.rs +++ b/src/game.rs @@ -47,6 +47,7 @@ impl Plugin for Riichi { .add_systems(OnEnter(TurnState::Menzen), round::menzen) .add_systems(Update, round::riichi_kan.run_if(in_state(TurnState::RiichiKan))) .add_systems(Update, round::discard.run_if(in_state(TurnState::Discard))) + .add_systems(Update, round::debug_discard.run_if(in_state(TurnState::Discard))) .add_systems(Update, round::ron_chi_pon_kan.run_if(in_state(TurnState::RonChiiPonKan))) .add_systems(OnEnter(TurnState::End), round::end) // .add_systems(Update, systems) diff --git a/src/game/round.rs b/src/game/round.rs index 5d2ee14..698322e 100644 --- a/src/game/round.rs +++ b/src/game/round.rs @@ -155,31 +155,30 @@ pub(crate) fn discard( curr_player: Single>, players: Query<&Children, With>, - mut hands: Query<(&Children, Entity, &mut Hand), (With, Without)>, + mut hands: Query<(&Children, Entity), (With, Without)>, drawn: Single>, curr_turnstate: Res>, mut next_turnstate: ResMut>, ) -> Result { // trace!("discard"); - let curr_hand = hands.get_mut(players.get(*curr_player)?.iter().next().unwrap())?; + let (handtiles, hand) = hands.get_mut(players.get(*curr_player)?.iter().next().unwrap())?; let mut done = false; while let Some(message) = reader.read().next() { if let GameMessage::Discarded(discarded) = message { + debug!("discarded: {discarded:?}"); if *discarded == *drawn { - } else if curr_hand.0.contains(discarded) { + } else if handtiles.contains(discarded) { commands - .entity(curr_hand.1) + .entity(hand) .remove_child(*discarded) .add_child(*drawn); } else { panic!("current hand nor drawn tile contains discarded tile") } - commands - .entity(*discarded) - .remove::() - .insert(Discarded); + commands.entity(*drawn).remove::(); + commands.entity(*discarded).insert(Discarded); done = true; break; diff --git a/src/tui.rs b/src/tui.rs index 1244401..f166fb2 100644 --- a/src/tui.rs +++ b/src/tui.rs @@ -1,13 +1,12 @@ use std::time::Duration; -use bevy::{app::ScheduleRunnerPlugin, prelude::*, state::app::StatesPlugin, time::TimePlugin}; +use bevy::{app::ScheduleRunnerPlugin, prelude::*, state::app::StatesPlugin}; use bevy_ratatui::RatatuiPlugins; use jong::game::{ GameMessage, GameState, hand::{Drawn, Hand}, player::{CurrentPlayer, Player}, }; -use tracing::instrument; use tui_logger::TuiWidgetState; use crate::tui::{input::ConfirmSelect, states::ConsoleWidget}; diff --git a/src/tui/render.rs b/src/tui/render.rs index 5c63bba..ae6aae5 100644 --- a/src/tui/render.rs +++ b/src/tui/render.rs @@ -94,6 +94,7 @@ pub(crate) fn render( Ok(()) } +#[allow(clippy::too_many_arguments, clippy::type_complexity)] pub(crate) fn render_hands( mut commands: Commands, mut tui: ResMut, @@ -102,18 +103,21 @@ pub(crate) fn render_hands( layouts: Res, tiles: Query<&Tile>, - main_player: Single<(&Player, &Wind), With>, + main_player: Single<(&Player, Entity, &Wind), With>, curr_player: Single>, - players: Query<(&Player, &Children)>, + players: Query<(&Player, Entity, &Children)>, hands: Query<(&Children, Entity), (With, Without)>, - drawn_tile: Option>>, + drawn_tile: Single>, ) -> Result { let mut frame = tui.get_frame(); debug_blocks(*layouts, &mut frame); for (hand, hand_ent) in hands { // debug!("{hand:?}"); - let (player, _) = players.iter().find(|(_, c)| c.contains(&hand_ent)).unwrap(); + let (player, player_ent, _) = players + .iter() + .find(|(_, e, c)| c.contains(&hand_ent)) + .unwrap(); let hand: Vec<_> = hand .iter() .map(|entity| -> Result<_> { @@ -128,7 +132,8 @@ pub(crate) fn render_hands( if player == main_player.0 { // split main box into thirds let mut this_hand = layouts.this_hand; - let this_drawer = drawn_tile.as_ref().is_some_and(|dt| dt.0 == player); + // let this_drawer = drawn_tile..is_some_and(|dt| dt.0 == player); + let this_drawer = player_ent == *curr_player; let tile_drawn = if this_drawer { 7 } else { 0 }; let hand_draw_meld = Layout::horizontal([ Constraint::Max(hand.len() as u16 * 5), @@ -170,23 +175,23 @@ pub(crate) fn render_hands( // tsumo tile if this_drawer { - let (_, entity) = **drawn_tile.as_ref().unwrap(); + // trace!("this_drawer"); let mut area = drawn_area.resize(Size { width: 5, height: 4, }); area = area.offset(Offset { x: 2, y: 0 }); - let hovered = hovered.contains(entity); - let widget = render_tile(tiles.get(entity)?, hovered); + let hovered = hovered.contains(*drawn_tile); + let widget = render_tile(tiles.get(*drawn_tile)?, hovered); if hovered { area = area.offset(Offset { x: 0, y: -1 }); let mut hitbox = area.as_size(); hitbox.height += 1; - commands.entity(entity).insert(PickRegion { + commands.entity(*drawn_tile).insert(PickRegion { area: area.resize(hitbox), }); } else { - commands.entity(entity).insert(PickRegion { area }); + commands.entity(*drawn_tile).insert(PickRegion { area }); } frame.render_widget(widget, area); }