From 4112edbf2abf9726395de79991e314e87066b3f4 Mon Sep 17 00:00:00 2001 From: Tao Tien <29749622+taotien@users.noreply.github.com> Date: Tue, 13 Jan 2026 03:32:08 -0800 Subject: [PATCH] store rendered tile as entity rather than raw Paragraph --- src/game/hand.rs | 2 +- src/game/mod.rs | 4 ++-- src/game/round.rs | 2 +- src/game/wall.rs | 2 +- src/lib.rs | 2 +- src/{tiles.rs => tile.rs} | 0 src/tui/mod.rs | 2 +- src/tui/render/hand.rs | 25 ++++++++++++++++++------- src/tui/render/ingame.rs | 5 +++-- src/tui/render/mod.rs | 2 +- src/tui/render/tile.rs | 34 ++++++++++++++++++++++++++++++++++ src/tui/render/tiles.rs | 30 ------------------------------ 12 files changed, 63 insertions(+), 47 deletions(-) rename src/{tiles.rs => tile.rs} (100%) create mode 100644 src/tui/render/tile.rs delete mode 100644 src/tui/render/tiles.rs diff --git a/src/game/hand.rs b/src/game/hand.rs index f17b6ba..434fe16 100644 --- a/src/game/hand.rs +++ b/src/game/hand.rs @@ -2,7 +2,7 @@ use bevy::{ecs::relationship::RelationshipSourceCollection, prelude::*}; use crate::{ game::{player::Player /* wall::WallTiles */}, - tiles::Tile, + tile::Tile, }; #[derive(Component)] diff --git a/src/game/mod.rs b/src/game/mod.rs index 8d41908..e42c311 100644 --- a/src/game/mod.rs +++ b/src/game/mod.rs @@ -6,7 +6,7 @@ use crate::{ player::{MainPlayer, Player}, wall::Wall, }, - tiles::{self, *}, + tile::{self, *}, }; pub mod hand; @@ -31,7 +31,7 @@ impl Plugin for Riichi { .init_state::() .init_resource::() .init_resource::() - .add_systems(Startup, tiles::init_tiles) + .add_systems(Startup, tile::init_tiles) .add_systems(OnEnter(GameState::Setup), setup) .add_systems(OnEnter(GameState::Deal), shuffle_deal) .add_systems(Update, hand::sort_hands.run_if(in_state(GameState::Play))) diff --git a/src/game/round.rs b/src/game/round.rs index fa4873b..5f8a3be 100644 --- a/src/game/round.rs +++ b/src/game/round.rs @@ -1,6 +1,6 @@ use bevy::prelude::*; -use crate::tiles::*; +use crate::tile::*; #[derive(Component)] pub(crate) struct Dice(u8, u8); diff --git a/src/game/wall.rs b/src/game/wall.rs index 4463c26..de6ac75 100644 --- a/src/game/wall.rs +++ b/src/game/wall.rs @@ -1,7 +1,7 @@ use bevy::prelude::*; use rand::seq::SliceRandom; -use crate::tiles::Tile; +use crate::tile::Tile; #[derive(Component)] pub struct Wall; diff --git a/src/lib.rs b/src/lib.rs index 45412c3..a64f794 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,5 @@ #![allow(unused)] pub mod game; -pub mod tiles; +pub mod tile; pub mod yakus; diff --git a/src/tiles.rs b/src/tile.rs similarity index 100% rename from src/tiles.rs rename to src/tile.rs diff --git a/src/tui/mod.rs b/src/tui/mod.rs index c6ea950..33ac500 100644 --- a/src/tui/mod.rs +++ b/src/tui/mod.rs @@ -66,7 +66,7 @@ impl Plugin for RiichiTui { // gaming .init_resource::() - .add_systems(Update, render::hand::render_hand.run_if(in_state(InGame).and(in_state(GameState::Play)))) + .add_systems(PostUpdate, render::hand::render_hands.run_if(in_state(InGame).and(in_state(GameState::Play)))) .add_systems(PostUpdate, render::ingame::draw_ingame.run_if(in_state(InGame))) // semicolon stopper diff --git a/src/tui/render/hand.rs b/src/tui/render/hand.rs index a1b5ebc..939fdf9 100644 --- a/src/tui/render/hand.rs +++ b/src/tui/render/hand.rs @@ -3,19 +3,21 @@ use ratatui::widgets::Paragraph; use jong::{ game::{hand::Hand, player::Player}, - tiles::Tile, + tile::Tile, }; -use crate::tui::render::tiles; +use crate::tui::render::tile::{self, RenderedTile}; #[derive(Resource, Default)] -pub(crate) struct RenderedHand(pub(crate) HashMap>>); +pub(crate) struct RenderedHand(pub(crate) HashMap>); -pub(crate) fn render_hand( +#[allow(clippy::type_complexity)] +pub(crate) fn render_hands( + mut commands: Commands, + mut rendered_hand: ResMut, tiles: Populated<&Tile>, player_hands: Populated<(Entity, &Children), (With, Changed)>, hands: Populated<&Children, (Changed, Without)>, - mut target: ResMut, ) -> Result { let mut rendered = HashMap::new(); @@ -24,12 +26,21 @@ pub(crate) fn render_hand( let tiles = hands .get(hand)? .iter() - .map(|it| tiles.get(it).map(tiles::draw_tile).unwrap()) + .map(|it| { + tiles + .get(it) + // .inspect(|t| debug!("{t:?}")) + .map(tile::draw_tile) + .map(|p| commands.spawn(RenderedTile(p)).id()) + // .inspect(|e| debug!("{e:?}")) + .unwrap() + }) .collect(); rendered.insert(player_ent, tiles); } - target.0 = rendered; + rendered_hand.0 = rendered; + trace!("render_hands"); Ok(()) } diff --git a/src/tui/render/ingame.rs b/src/tui/render/ingame.rs index e573cb9..e0a4b0e 100644 --- a/src/tui/render/ingame.rs +++ b/src/tui/render/ingame.rs @@ -2,10 +2,11 @@ use bevy::prelude::*; use bevy_ratatui::RatatuiContext; use jong::game::player::{MainPlayer, Player}; -use crate::tui::render::hand; +use crate::tui::render::{hand, tile::RenderedTile}; pub(crate) fn draw_ingame( rendered_hand: Res, + rendered_tiles: Populated<&RenderedTile>, main_player: Single, With)>, mut tui_ctx: ResMut, ) -> Result { @@ -22,7 +23,7 @@ pub(crate) fn draw_ingame( // if let Some(hand) = rendered_hand.0.get(&*main_player) { if let Some(hand) = rendered_hand.0.get(&*main_player) { for (tile, area) in hand.iter().zip(areas.iter()) { - frame.render_widget(tile, *area); + frame.render_widget(&rendered_tiles.get(*tile).unwrap().0, *area); } } })?; diff --git a/src/tui/render/mod.rs b/src/tui/render/mod.rs index fc6612a..0e3cc55 100644 --- a/src/tui/render/mod.rs +++ b/src/tui/render/mod.rs @@ -1,3 +1,3 @@ pub(crate) mod hand; pub(crate) mod ingame; -mod tiles; +pub(crate) mod tile; diff --git a/src/tui/render/tile.rs b/src/tui/render/tile.rs new file mode 100644 index 0000000..59d50bd --- /dev/null +++ b/src/tui/render/tile.rs @@ -0,0 +1,34 @@ +use bevy::prelude::*; +use ratatui::widgets::Paragraph; + +use jong::tile::Tile; + +#[derive(Component)] +pub(crate) struct RenderedTile(pub(crate) Paragraph<'static>); + +pub(crate) fn draw_tile(tile: &Tile) -> Paragraph<'static> { + use ratatui::prelude::*; + + let block = ratatui::widgets::Block::bordered(); + + Paragraph::new(match &tile.suit { + jong::tile::Suit::Pin(rank) => format!("{}\np", rank.0), + jong::tile::Suit::Sou(rank) => format!("{}\ns", rank.0), + jong::tile::Suit::Man(rank) => format!("{}\nm", rank.0), + jong::tile::Suit::Wind(wind) => (match wind { + jong::tile::Wind::Ton => "e\nw", + jong::tile::Wind::Nan => "s\nw", + jong::tile::Wind::Shaa => "w\nw", + jong::tile::Wind::Pei => "n\nw", + }) + .into(), + jong::tile::Suit::Dragon(dragon) => (match dragon { + jong::tile::Dragon::Haku => "w\nd", + jong::tile::Dragon::Hatsu => "g\nd", + jong::tile::Dragon::Chun => "r\nd", + }) + .into(), + }) + .block(block) + .centered() +} diff --git a/src/tui/render/tiles.rs b/src/tui/render/tiles.rs deleted file mode 100644 index 620a2d1..0000000 --- a/src/tui/render/tiles.rs +++ /dev/null @@ -1,30 +0,0 @@ -use ratatui::widgets::Paragraph; - -use jong::tiles::Tile; - -pub(crate) fn draw_tile(tile: &Tile) -> Paragraph<'static> { - use ratatui::prelude::*; - - let block = ratatui::widgets::Block::bordered(); - - Paragraph::new(match &tile.suit { - jong::tiles::Suit::Pin(rank) => format!("{}\np", rank.0), - jong::tiles::Suit::Sou(rank) => format!("{}\ns", rank.0), - jong::tiles::Suit::Man(rank) => format!("{}\nm", rank.0), - jong::tiles::Suit::Wind(wind) => (match wind { - jong::tiles::Wind::Ton => "e\nw", - jong::tiles::Wind::Nan => "s\nw", - jong::tiles::Wind::Shaa => "w\nw", - jong::tiles::Wind::Pei => "n\nw", - }) - .into(), - jong::tiles::Suit::Dragon(dragon) => (match dragon { - jong::tiles::Dragon::Haku => "w\nd", - jong::tiles::Dragon::Hatsu => "g\nd", - jong::tiles::Dragon::Chun => "r\nd", - }) - .into(), - }) - .block(block) - .centered() -}