render pond

This commit is contained in:
Tao Tien 2026-02-19 00:37:12 -08:00
parent 875c3fb5bb
commit 1d9577ba42
5 changed files with 76 additions and 7 deletions

View file

@ -49,6 +49,7 @@ pub fn discard_tile(ctx: &ReducerContext, tile_id: u32) -> Result<(), String> {
let dealt_tile = if let Some(dealt) = ctx.db.tile().id().find(tile_id) {
if let Some(drawn) = player.drawn_tile {
if drawn.id == dealt.id {
// dealt from drawn tile
dealt
} else if let Some((i, _)) = player
.hand
@ -56,6 +57,7 @@ pub fn discard_tile(ctx: &ReducerContext, tile_id: u32) -> Result<(), String> {
.enumerate()
.find(|(_, t)| t.id == tile_id)
{
// dealt from hand
let dealt = player.hand.remove(i);
player.hand.push(drawn);
player.hand.sort_by_key(|t| t.tile);

View file

@ -101,15 +101,24 @@ fn on_player_insert_update(
mut messages: ReadInsertUpdateMessage<jong_db::Player>,
mut commands: Commands,
pond: Option<Single<Entity, With<Pond>>>,
hand: Option<Single<Entity, With<Hand>>>,
tiles: Query<(Entity, &TileId)>,
) {
let hand = if hand.is_none() {
let hand = commands.spawn(Hand).id();
commands.spawn(Pond);
hand
} else {
*hand.unwrap()
};
let pond = if pond.is_none() {
let pond = commands.spawn(Pond).id();
commands.spawn(Pond);
pond
} else {
*pond.unwrap()
};
for msg in messages.read() {
let hand_tiles: Vec<_> = msg
@ -125,11 +134,30 @@ fn on_player_insert_update(
})
.collect();
let pond_tiles: Vec<_> = msg
.new
.pond
.iter()
.map(|dbt| {
tiles
.iter()
.find_map(|(e, t)| if *t == TileId(dbt.id) { Some(e) } else { None })
.expect(&format!(
"dealt tiles should still be around, couldn't find {:?}. Tiles: {:?}",
dbt,
tiles.iter().map(|(_, t)| t).collect::<Vec<_>>()
))
})
.collect();
debug!("hand_tiles: {hand_tiles:?}");
commands.entity(hand).replace_children(&hand_tiles);
commands.entity(pond).replace_children(&pond_tiles);
// drawn tile is always a new tile to us until wall isn't fake
if let Some(dbt) = &msg.new.drawn_tile {
debug!("drew tile with id: {}", dbt.id);
commands.spawn((Tile::from(&dbt.tile), TileId(dbt.id), Drawn));
}
}

View file

@ -9,7 +9,7 @@ pub struct MainPlayer;
#[derive(Component)]
pub struct CurrentPlayer;
#[derive(Component, PartialEq, Eq)]
#[derive(Component, PartialEq, Eq, Debug)]
pub struct TileId(pub u32);
#[derive(Component)]

View file

@ -85,7 +85,7 @@ impl Plugin for TuiPlugin {
.add_systems(
Update,
(
render::render_hand.run_if(in_state(GameState::Play)),
(render::render_hand, render::render_pond).run_if(in_state(GameState::Play)),
render::render,
)
.chain()
@ -106,7 +106,7 @@ fn discard_tile(
while let Some(message) = selected.read().next() {
if let Ok(tile_id) = tiles.get(message.0) {
stdb.reducers().discard_tile(tile_id.0).unwrap();
commands.get_entity(drawn.0).unwrap().despawn();
commands.entity(drawn.0).remove::<Drawn>();
}
}
}

View file

@ -6,10 +6,8 @@ use ratatui::layout::{Constraint, Flex, Layout, Offset, Rect, Size};
use ratatui::style::{Modifier, Stylize};
use ratatui::widgets::{Block, Borders, Clear, Paragraph};
use jong::riichi::player::{CurrentPlayer, MainPlayer, Player};
use jong::riichi::player::{Drawn, Hand};
// use jong::riichi::round::Wind;
// use jong_types::*;
use jong::riichi::player::*;
use jong_types::*;
use crate::tui::input::Hovered;
use crate::tui::layout::*;
@ -197,3 +195,44 @@ pub(crate) fn render_hand(
Ok(())
}
pub(crate) fn render_pond(
mut commands: Commands,
mut tui: ResMut<RatatuiContext>,
hovered: Query<Entity, With<Hovered>>,
layouts: Res<HandLayouts>,
pond: Single<(&Children, Entity), With<Pond>>,
tiles: Query<&Tile>,
) -> Result {
let mut frame = tui.get_frame();
let pond: Vec<_> = pond
.0
.iter()
.map(|entity| -> Result<_> {
let tile = tiles.get(entity).unwrap();
let widget = render_tile(tile, false);
Ok((entity, widget))
})
.collect::<Result<_>>()?;
let mut this_pond = layouts.this_pond;
let row_constraints = [Constraint::Max(4); 3];
let col_constraints = [Constraint::Max(5); 6];
let row_layouts = Layout::vertical(row_constraints).flex(Flex::Start);
let col_layouts = Layout::horizontal(col_constraints).flex(Flex::Start);
let mut rows = row_layouts.areas::<3>(this_pond);
for (rect, (_, tile)) in rows
.iter()
.flat_map(|row| col_layouts.areas::<6>(*row))
.zip(pond)
{
frame.render_widget(tile, rect);
}
Ok(())
}