detect hand change and render it

This commit is contained in:
Tao Tien 2026-01-11 20:10:30 -08:00
parent d506a25716
commit 3417384b86
9 changed files with 138 additions and 68 deletions

View file

@ -1,54 +1,81 @@
use std::marker::PhantomData;
use bevy::ecs::system::SystemParam;
use bevy::prelude::*;
use bevy_ratatui::RatatuiContext;
use ratatui::widgets::Paragraph;
use jong::game::wall::Wall;
use jong::game::wall::WallTiles;
use jong::tiles::Tile;
use jong::{
game::{
player::{Hand, HandTiles},
wall::{Wall, WallTiles},
},
tiles::Tile,
};
mod tiles;
pub(crate) fn draw_ingame(
#[derive(Component)]
pub(crate) struct RenderedHand<'a>(Vec<Paragraph<'a>>);
pub(crate) fn render_changed_hand(
hand: Single<&HandTiles, Changed<HandTiles>>,
// hand_tiles: Query<&HandTiles, With<Hand>>,
tiles: Query<&Tile>,
wall: Option<Single<&WallTiles, With<Wall>>>,
mut target: Single<&'static mut RenderedHand>,
) -> Result {
let hand_tiles = hand
.iter()
.map(|inhand| -> Result<_> { Ok(tiles.get(inhand).map(tiles::draw_tile)?) })
.collect::<Result<Vec<_>>>()?;
target.0 = hand_tiles;
Ok(())
}
pub(crate) fn draw_ingame(
// tiles: Query<&Tile>,
// wall_tiles: Option<Single<&WallTiles, With<Wall>>>,
// hand_tiles: Query<&HandTiles, With<Hand>>,
rendered_hand: Single<&'static RenderedHand>,
mut tui_ctx: ResMut<RatatuiContext>,
) -> Result {
use ratatui::layout::Flex;
use ratatui::prelude::*;
let title = ratatui::text::Text::raw("tiny riichi");
// let title = ratatui::text::Text::raw("tiny riichi");
let wall = if let Some(wall) = wall {
let wall = wall
.iter()
.map(|inwall| -> Result<_> { Ok(tiles.get(inwall).map(tiles::draw_tile)?) })
.collect::<Result<Vec<_>>>()?;
// let wall = if let Some(wall_tiles) = wall_tiles {
// let wall_tiles = wall_tiles
// .iter()
// .map(|inwall| -> Result<_> { Ok(tiles.get(inwall).map(tiles::draw_tile)?) })
// .collect::<Result<Vec<_>>>()?;
// let paragraph = Paragraph::new(wall.join(", ")).wrap(ratatui::widgets::Wrap { trim: true });
Some(wall)
} else {
None
};
// // let paragraph = Paragraph::new(wall.join(", ")).wrap(ratatui::widgets::Wrap { trim: true });
// Some(wall_tiles)
// } else {
// None
// };
tui_ctx.draw(|frame| {
// frame.render_widget(title, frame.area());
debug!("{}", frame.area());
if let Some(wall) = wall {
// let tile_area = Rect::new(0, 0, 5, 4);
let layout = Layout::horizontal(vec![Constraint::Max(5); 13]).flex(Flex::Start);
let mut area = frame.area();
area.height = 4;
let areas = layout.areas::<13>(area);
// debug!("wall.len(): {}, areas.len(): {}", wall.len(), areas.len());
for (tile, rect) in wall.iter().zip(areas.iter()) {
// debug!("{rect:?}");
frame.render_widget(tile, *rect);
}
// if let Some(wall) = wall {
// // let tile_area = Rect::new(0, 0, 5, 4);
let layout = Layout::horizontal(vec![Constraint::Max(5); 13]).flex(Flex::Start);
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);
}
// // debug!("wall.len(): {}, areas.len(): {}", wall.len(), areas.len());
// for (tile, rect) in wall.iter().zip(areas.iter()) {
// // debug!("{rect:?}");
// frame.render_widget(tile, *rect);
// }
// }
})?;
Ok(())