draw tile

This commit is contained in:
Tao Tien 2026-01-13 13:07:21 -08:00
parent 6e6b792a58
commit b5e670b491
5 changed files with 82 additions and 41 deletions

View file

@ -9,7 +9,7 @@ use crate::{
pub struct Hand; pub struct Hand;
#[derive(Component)] #[derive(Component)]
pub struct Drawn(pub Entity); pub struct DrawnTile(pub Entity);
// #[derive(Component, Default)] // #[derive(Component, Default)]
// enum SortHand { // enum SortHand {

View file

@ -4,7 +4,7 @@ use strum::{EnumCount, FromRepr};
use crate::{ use crate::{
EnumNextCycle, EnumNextCycle,
game::{ game::{
hand::{Drawn, Hand}, hand::{DrawnTile, Hand},
player::{MainPlayer, Player}, player::{MainPlayer, Player},
round::{CurrentPlayer, TurnState, Wind}, round::{CurrentPlayer, TurnState, Wind},
wall::Wall, wall::Wall,
@ -40,7 +40,7 @@ impl Plugin for Riichi {
.add_systems(OnEnter(GameState::Deal), hand::shuffle_deal) .add_systems(OnEnter(GameState::Deal), hand::shuffle_deal)
.add_systems(Update, hand::sort_hands.run_if(in_state(GameState::Play))) .add_systems(Update, hand::sort_hands.run_if(in_state(GameState::Play)))
// .add_systems(Update, turn_manager.run_if(in_state(GameState::Play))) // .add_systems(Update, turn_manager.run_if(in_state(GameState::Play)))
.add_systems(OnEnter(TurnState::Tsumo), tsumo) .add_systems(OnEnter(TurnState::Tsumo), round::tsumo)
// semicolon stopper // semicolon stopper
; ;
} }
@ -53,31 +53,11 @@ impl Plugin for Riichi {
// fn turn_manager() {} // fn turn_manager() {}
fn tsumo(
mut commands: Commands,
curr_player: Res<CurrentPlayer>,
// players: Populated<Entity, With<Player>>,
wall_ent: Single<Entity, With<Wall>>,
walltiles: Single<&Children, With<Wall>>,
curr_turnstate: Res<State<TurnState>>,
mut next_turnstate: ResMut<NextState<TurnState>>,
) {
trace!("tsumo for: {:?}", curr_player.0);
let drawn = walltiles.last().unwrap();
commands.entity(*wall_ent).remove_child(*drawn);
commands.entity(curr_player.0).insert(Drawn(*drawn));
debug!("wall: {:?}", *walltiles);
next_turnstate.set(curr_turnstate.next());
}
pub(crate) fn setup( pub(crate) fn setup(
mut commands: Commands, mut commands: Commands,
matchsettings: Res<round::MatchSettings>, matchsettings: Res<round::MatchSettings>,
// mut compass: ResMut<Compass> // mut compass: ResMut<Compass>
tiles: Query<Entity, With<Tile>>, // tiles: Query<Entity, With<Tile>>,
mut next_gamestate: ResMut<NextState<GameState>>, mut next_gamestate: ResMut<NextState<GameState>>,
) { ) {
for i in 1..=matchsettings.player_count { for i in 1..=matchsettings.player_count {

View file

@ -1,7 +1,10 @@
use bevy::prelude::*; use bevy::prelude::*;
use strum::{EnumCount, FromRepr}; use strum::{EnumCount, FromRepr};
use crate::{EnumNextCycle, game::GameState}; use crate::{
EnumNextCycle,
game::{GameState, hand::DrawnTile, wall::Wall},
};
#[derive(Component)] #[derive(Component)]
pub(crate) struct Dice(u8, u8); pub(crate) struct Dice(u8, u8);
@ -84,3 +87,23 @@ impl EnumNextCycle for TurnState {
} }
} }
} }
pub(crate) fn tsumo(
mut commands: Commands,
curr_player: Res<CurrentPlayer>,
// players: Populated<Entity, With<Player>>,
wall_ent: Single<Entity, With<Wall>>,
walltiles: Single<&Children, With<Wall>>,
curr_turnstate: Res<State<TurnState>>,
mut next_turnstate: ResMut<NextState<TurnState>>,
) {
trace!("tsumo for: {:?}", curr_player.0);
let drawn = walltiles.last().unwrap();
commands.entity(*wall_ent).remove_child(*drawn);
commands.entity(curr_player.0).insert(DrawnTile(*drawn));
debug!("drew: {:?}", drawn);
next_turnstate.set(curr_turnstate.next());
}

View file

@ -5,8 +5,8 @@ use tui_logger::TuiLoggerWidget;
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Default, States)] #[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Default, States)]
pub(crate) enum ConsoleState { pub(crate) enum ConsoleState {
Closed,
#[default] #[default]
Closed,
Open, Open,
} }

View file

@ -1,10 +1,13 @@
use bevy::prelude::*; use bevy::prelude::*;
use bevy_ratatui::RatatuiContext; use bevy_ratatui::RatatuiContext;
use jong::game::hand::DrawnTile;
use jong::game::player::{MainPlayer, Player}; use jong::game::player::{MainPlayer, Player};
use jong::tile::Tile;
use rand::rand_core::block::BlockRngCore; use rand::rand_core::block::BlockRngCore;
use ratatui::style::Styled; use ratatui::style::Styled;
use ratatui::widgets::{Block, Borders, Widget}; use ratatui::widgets::{Block, Borders, Widget};
use crate::tui::render::tile::draw_tile;
use crate::tui::{ use crate::tui::{
input::mouse::PickRegion, input::mouse::PickRegion,
render::{Hovered, hand, tile::RenderedTile}, render::{Hovered, hand, tile::RenderedTile},
@ -13,10 +16,12 @@ use crate::tui::{
pub(crate) fn draw_ingame( pub(crate) fn draw_ingame(
mut commands: Commands, mut commands: Commands,
mut tui_ctx: ResMut<RatatuiContext>, mut tui_ctx: ResMut<RatatuiContext>,
main_player: Single<Entity, (With<Player>, With<MainPlayer>)>, tiles: Query<&Tile>,
hovered_entity: Query<Entity, With<Hovered>>, hovered_entity: Query<Entity, With<Hovered>>,
main_player: Single<Entity, (With<Player>, With<MainPlayer>)>,
rendered_tiles: Populated<&RenderedTile>, rendered_tiles: Populated<&RenderedTile>,
rendered_hand: Res<hand::RenderedHand>, rendered_hand: Res<hand::RenderedHand>,
drawn_tile: Option<Single<&DrawnTile, With<MainPlayer>>>,
) -> Result { ) -> Result {
use ratatui::layout::Flex; use ratatui::layout::Flex;
use ratatui::prelude::*; use ratatui::prelude::*;
@ -45,7 +50,8 @@ pub(crate) fn draw_ingame(
); );
let pond_area = term_area.inner(margin); let pond_area = term_area.inner(margin);
let all_pond = Layout::horizontal([Constraint::Fill(1); 3]); let all_pond = Layout::horizontal([Constraint::Fill(1); 3]);
let cross_pond = Layout::vertical([Constraint::Fill(1), Constraint::Max(1),Constraint::Fill(1)]); let cross_pond =
Layout::vertical([Constraint::Fill(1), Constraint::Max(1), Constraint::Fill(1)]);
let [mut left_pond, center, mut right_pond] = all_pond.areas::<3>(pond_area); let [mut left_pond, center, mut right_pond] = all_pond.areas::<3>(pond_area);
let [cross_pond, compass, this_pond] = cross_pond.areas::<3>(center); let [cross_pond, compass, this_pond] = cross_pond.areas::<3>(center);
// let shift = left_pond.height - cross_pond.height; // let shift = left_pond.height - cross_pond.height;
@ -65,19 +71,30 @@ pub(crate) fn draw_ingame(
frame.render_widget(debug_block.clone(), right_pond); frame.render_widget(debug_block.clone(), right_pond);
// TODO attempt to merge blocks on smol term? // TODO attempt to merge blocks on smol term?
let layout = Layout::horizontal(vec![Constraint::Max(5); 13]).flex(Flex::Start); if let Some(hand) = rendered_hand.0.get(&*main_player) {
let hand_area_layout = Layout::horizontal([
Constraint::Max(hand.len() as u16 * 5),
Constraint::Max(if drawn_tile.is_some() { 7 } else { 0 }),
Constraint::Fill(1),
])
.flex(Flex::SpaceBetween);
let this_clamped = this_hand.height.abs_diff(5); let this_clamped = this_hand.height.abs_diff(5);
if let Some(val) = this_hand.height.checked_sub(this_clamped) { if let Some(val) = this_hand.height.checked_sub(this_clamped) {
this_hand.height = val this_hand.height = 4
} else { } else {
// FIXME show error // FIXME show error
panic!("terminal too small!"); panic!("terminal too small!");
} }
this_hand.y += this_clamped + 1; this_hand.y += this_clamped + 1;
let areas = layout.areas::<13>(this_hand); let [this_hand, mut this_drawn, this_meld] = hand_area_layout.areas::<3>(this_hand);
if let Some(hand) = rendered_hand.0.get(&*main_player) {
for (tile, mut tile_area) in hand.iter().zip(areas.into_iter()) { // this_hand
tile_area.height = 4; let mut constraints = vec![Constraint::Max(5); hand.len()];
constraints.push(Constraint::Fill(1));
let layout = Layout::horizontal(constraints).flex(Flex::Start);
let hand_areas = layout.split(this_hand);
for (tile, mut tile_area) in hand.iter().zip(hand_areas.iter().cloned()) {
// tile_area.height = 4;
let mut widget = rendered_tiles.get(*tile).unwrap().0.clone(); let mut widget = rendered_tiles.get(*tile).unwrap().0.clone();
if hovered_entity.contains(*tile) { if hovered_entity.contains(*tile) {
widget = widget.add_modifier(Modifier::BOLD); widget = widget.add_modifier(Modifier::BOLD);
@ -93,6 +110,27 @@ pub(crate) fn draw_ingame(
.insert(PickRegion { area: tile_area }); .insert(PickRegion { area: tile_area });
frame.render_widget(widget, tile_area); frame.render_widget(widget, tile_area);
} }
// this_drawn
if let Some(tile) = drawn_tile {
// this_drawn.height = 4;
this_drawn.width = 5;
this_drawn.x += 2;
let mut widget = draw_tile(tiles.get(tile.0).unwrap());
if hovered_entity.contains(tile.0) {
widget = widget.add_modifier(Modifier::BOLD);
if let Some(val) = this_drawn.y.checked_sub(1) {
this_drawn.y = val
} else {
// FIXME show error
panic!("terminal too small!");
}
}
commands
.entity(tile.0)
.insert(PickRegion { area: this_drawn });
frame.render_widget(widget, this_drawn);
}
} }
})?; })?;