render pond
This commit is contained in:
parent
875c3fb5bb
commit
1d9577ba42
5 changed files with 76 additions and 7 deletions
|
|
@ -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) {
|
let dealt_tile = if let Some(dealt) = ctx.db.tile().id().find(tile_id) {
|
||||||
if let Some(drawn) = player.drawn_tile {
|
if let Some(drawn) = player.drawn_tile {
|
||||||
if drawn.id == dealt.id {
|
if drawn.id == dealt.id {
|
||||||
|
// dealt from drawn tile
|
||||||
dealt
|
dealt
|
||||||
} else if let Some((i, _)) = player
|
} else if let Some((i, _)) = player
|
||||||
.hand
|
.hand
|
||||||
|
|
@ -56,6 +57,7 @@ pub fn discard_tile(ctx: &ReducerContext, tile_id: u32) -> Result<(), String> {
|
||||||
.enumerate()
|
.enumerate()
|
||||||
.find(|(_, t)| t.id == tile_id)
|
.find(|(_, t)| t.id == tile_id)
|
||||||
{
|
{
|
||||||
|
// dealt from hand
|
||||||
let dealt = player.hand.remove(i);
|
let dealt = player.hand.remove(i);
|
||||||
player.hand.push(drawn);
|
player.hand.push(drawn);
|
||||||
player.hand.sort_by_key(|t| t.tile);
|
player.hand.sort_by_key(|t| t.tile);
|
||||||
|
|
|
||||||
|
|
@ -101,15 +101,24 @@ fn on_player_insert_update(
|
||||||
mut messages: ReadInsertUpdateMessage<jong_db::Player>,
|
mut messages: ReadInsertUpdateMessage<jong_db::Player>,
|
||||||
mut commands: Commands,
|
mut commands: Commands,
|
||||||
|
|
||||||
|
pond: Option<Single<Entity, With<Pond>>>,
|
||||||
hand: Option<Single<Entity, With<Hand>>>,
|
hand: Option<Single<Entity, With<Hand>>>,
|
||||||
tiles: Query<(Entity, &TileId)>,
|
tiles: Query<(Entity, &TileId)>,
|
||||||
) {
|
) {
|
||||||
let hand = if hand.is_none() {
|
let hand = if hand.is_none() {
|
||||||
let hand = commands.spawn(Hand).id();
|
let hand = commands.spawn(Hand).id();
|
||||||
|
commands.spawn(Pond);
|
||||||
hand
|
hand
|
||||||
} else {
|
} else {
|
||||||
*hand.unwrap()
|
*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() {
|
for msg in messages.read() {
|
||||||
let hand_tiles: Vec<_> = msg
|
let hand_tiles: Vec<_> = msg
|
||||||
|
|
@ -125,11 +134,30 @@ fn on_player_insert_update(
|
||||||
})
|
})
|
||||||
.collect();
|
.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:?}");
|
debug!("hand_tiles: {hand_tiles:?}");
|
||||||
|
|
||||||
commands.entity(hand).replace_children(&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 {
|
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));
|
commands.spawn((Tile::from(&dbt.tile), TileId(dbt.id), Drawn));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@ pub struct MainPlayer;
|
||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
pub struct CurrentPlayer;
|
pub struct CurrentPlayer;
|
||||||
|
|
||||||
#[derive(Component, PartialEq, Eq)]
|
#[derive(Component, PartialEq, Eq, Debug)]
|
||||||
pub struct TileId(pub u32);
|
pub struct TileId(pub u32);
|
||||||
|
|
||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
|
|
|
||||||
|
|
@ -85,7 +85,7 @@ impl Plugin for TuiPlugin {
|
||||||
.add_systems(
|
.add_systems(
|
||||||
Update,
|
Update,
|
||||||
(
|
(
|
||||||
render::render_hand.run_if(in_state(GameState::Play)),
|
(render::render_hand, render::render_pond).run_if(in_state(GameState::Play)),
|
||||||
render::render,
|
render::render,
|
||||||
)
|
)
|
||||||
.chain()
|
.chain()
|
||||||
|
|
@ -106,7 +106,7 @@ fn discard_tile(
|
||||||
while let Some(message) = selected.read().next() {
|
while let Some(message) = selected.read().next() {
|
||||||
if let Ok(tile_id) = tiles.get(message.0) {
|
if let Ok(tile_id) = tiles.get(message.0) {
|
||||||
stdb.reducers().discard_tile(tile_id.0).unwrap();
|
stdb.reducers().discard_tile(tile_id.0).unwrap();
|
||||||
commands.get_entity(drawn.0).unwrap().despawn();
|
commands.entity(drawn.0).remove::<Drawn>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,10 +6,8 @@ use ratatui::layout::{Constraint, Flex, Layout, Offset, Rect, Size};
|
||||||
use ratatui::style::{Modifier, Stylize};
|
use ratatui::style::{Modifier, Stylize};
|
||||||
use ratatui::widgets::{Block, Borders, Clear, Paragraph};
|
use ratatui::widgets::{Block, Borders, Clear, Paragraph};
|
||||||
|
|
||||||
use jong::riichi::player::{CurrentPlayer, MainPlayer, Player};
|
use jong::riichi::player::*;
|
||||||
use jong::riichi::player::{Drawn, Hand};
|
use jong_types::*;
|
||||||
// use jong::riichi::round::Wind;
|
|
||||||
// use jong_types::*;
|
|
||||||
|
|
||||||
use crate::tui::input::Hovered;
|
use crate::tui::input::Hovered;
|
||||||
use crate::tui::layout::*;
|
use crate::tui::layout::*;
|
||||||
|
|
@ -197,3 +195,44 @@ pub(crate) fn render_hand(
|
||||||
|
|
||||||
Ok(())
|
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(())
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue