render main player's hand

This commit is contained in:
Tao Tien 2026-01-13 01:08:14 -08:00
parent 30d19ed9d9
commit 314c3299ef
8 changed files with 134 additions and 97 deletions

79
src/tui/input.rs Normal file
View file

@ -0,0 +1,79 @@
use bevy::prelude::*;
use bevy_ratatui::event::{KeyMessage, MouseMessage};
use jong::game::GameState;
use crate::tui::{TuiState, console::ConsoleState};
#[allow(clippy::too_many_arguments)]
pub(crate) fn kb_input_system(
mut kb_messages: MessageReader<KeyMessage>,
curr_tuistate: Res<State<TuiState>>,
curr_consolestate: Res<State<ConsoleState>>,
curr_gamestate: Res<State<GameState>>,
mut next_tuistate: ResMut<NextState<TuiState>>,
mut next_consolestate: ResMut<NextState<ConsoleState>>,
mut next_gamestate: ResMut<NextState<GameState>>,
mut exit: MessageWriter<AppExit>,
) {
use bevy_ratatui::crossterm::event::KeyCode;
let (ts, cs, gs) = (
curr_tuistate.get(),
curr_consolestate.get(),
curr_gamestate.get(),
);
for message in kb_messages.read() {
if let KeyCode::Char('`') = message.code {
next_consolestate.set(!*curr_consolestate.get());
continue;
}
if *cs == ConsoleState::Open {
let mut passthrough = false;
match message.code {
KeyCode::Up => todo!(),
KeyCode::Down => todo!(),
KeyCode::Home => todo!(),
KeyCode::End => todo!(),
KeyCode::PageUp => todo!(),
KeyCode::PageDown => todo!(),
KeyCode::Esc => next_consolestate.set(ConsoleState::Closed),
_ => passthrough = true,
}
if !passthrough {
continue;
}
}
match ts {
TuiState::MainMenu => match message.code {
KeyCode::Char('p') => {
next_tuistate.set(TuiState::InGame);
next_gamestate.set(GameState::Setup);
}
KeyCode::Char('q') => {
exit.write_default();
}
_ => {}
},
TuiState::InGame => match gs {
GameState::Setup => match message.code {
_ => {}
},
GameState::Play => match message.code {
KeyCode::Char('q') => {
exit.write_default();
}
_ => {}
},
_ => todo!(),
_ => unreachable!("TuiState::InGame but GameState invalid"),
},
}
}
}

View file

@ -10,13 +10,8 @@ use jong::game::GameState;
use crate::tui::TuiState;
const MAINMENU_OPTIONS: [&str; 2] = ["(p)lay", "(q)uit"];
// const MAINMENU_INPUTS: [char;2] = ['p', 'q'];
pub(crate) fn draw_mainmenu(
mut tui_ctx: ResMut<RatatuiContext>,
// mut tui_state: ResMut<NextState<TuiState>>,
// mut game_state: ResMut<NextState<GameState>>,
) {
pub(crate) fn draw_mainmenu(mut tui_ctx: ResMut<RatatuiContext>) {
let options = MAINMENU_OPTIONS;
let layout = Layout::vertical(vec![Constraint::Min(1); options.len()]);

View file

@ -2,7 +2,6 @@ use std::time::Duration;
use bevy::{app::ScheduleRunnerPlugin, prelude::*, state::app::StatesPlugin};
use bevy_ratatui::RatatuiPlugins;
use bevy_ratatui::event::KeyMessage;
use ratatui::{text::ToSpan, widgets::Paragraph};
use jong::game::GameState;
@ -13,6 +12,7 @@ use crate::tui::{console::ConsoleState, menu::draw_mainmenu, render::ingame::dra
mod console;
mod menu;
mod render;
mod input;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, States, Default)]
pub(crate) enum TuiState {
@ -59,7 +59,7 @@ impl Plugin for RiichiTui {
// general setup
.init_state::<TuiState>()
.add_computed_state::<InGame>()
.add_systems(Update, input_system)
.add_systems(Update, input::kb_input_system)
// main menu
.add_systems(Update, menu::draw_mainmenu.run_if(in_state(TuiState::MainMenu)))
@ -67,62 +67,11 @@ impl Plugin for RiichiTui {
// gaming
.init_resource::<render::hand::RenderedHand>()
.add_systems(Update, render::ingame::draw_ingame.run_if(in_state(InGame)))
// .add_systems(Update, render::hand::render_changed_hand.run_if(in_state(InGame).and(in_state(GameState::Play))))
.add_systems(Update, render::hand::render_hand.run_if(in_state(InGame).and(in_state(GameState::Play))))
// semicolon stopper
;
}
}
#[allow(clippy::too_many_arguments)]
pub(crate) fn input_system(
mut messages: MessageReader<KeyMessage>,
curr_tuistate: Res<State<TuiState>>,
curr_consolestate: Res<State<ConsoleState>>,
curr_gamestate: Res<State<GameState>>,
mut next_tuistate: ResMut<NextState<TuiState>>,
mut next_consolestate: ResMut<NextState<ConsoleState>>,
mut next_gamestate: ResMut<NextState<GameState>>,
mut exit: MessageWriter<AppExit>,
) {
use bevy_ratatui::crossterm::event::KeyCode;
let (ts, cs, gs) = (curr_tuistate.get(), curr_consolestate.get(), curr_gamestate.get());
for message in messages.read() {
if let KeyCode::Char('`') = message.code {
next_consolestate.set(!*curr_consolestate.get());
continue
}
match ts {
TuiState::MainMenu => match message.code {
KeyCode::Char('p') => {
next_tuistate.set(TuiState::InGame);
next_gamestate.set(GameState::Setup);
}
KeyCode::Char('q') => {
exit.write_default();
}
_ => {}
},
TuiState::InGame => match gs {
GameState::Setup => match message.code {
_ => {}
},
GameState::Play => match message.code {
KeyCode::Char('q') => {
exit.write_default();
}
_ => {}
},
_ => todo!(),
_ => unreachable!("TuiState::InGame but GameState invalid")
},
}
}
}

View file

@ -1,32 +1,35 @@
use bevy::prelude::*;
use bevy::{platform::collections::HashMap, prelude::*};
use ratatui::widgets::Paragraph;
// use jong::game::hand::HandTiles;
use jong::tiles::Tile;
use jong::{
game::{hand::Hand, player::Player},
tiles::Tile,
};
use crate::tui::render::tiles;
#[derive(Resource, Default)]
pub(crate) struct RenderedHand(pub(crate) Vec<Vec<Paragraph<'static>>>);
pub(crate) struct RenderedHand(pub(crate) HashMap<Entity, Vec<Paragraph<'static>>>);
// pub(crate) fn render_changed_hand(
// hands: Populated<&Children, Changed<HandTiles>>,
// tiles: Populated<&Tile>,
// mut target: ResMut<RenderedHand>,
// ) -> Result {
// let mut rendered = vec![];
pub(crate) fn render_hand(
tiles: Populated<&Tile>,
player_hands: Populated<(Entity, &Children), (With<Player>, Changed<Hand>)>,
hands: Populated<&Children, (Changed<Hand>, Without<Player>)>,
mut target: ResMut<RenderedHand>,
) -> Result {
let mut rendered = HashMap::new();
// for hand in hands {
// let tiles = hand
// .iter()
// .map(|inhand| tiles.get(inhand).map(tiles::draw_tile).unwrap())
// .collect();
for (player_ent, hand) in player_hands {
let hand = hand.iter().next().unwrap();
let tiles = hands
.get(hand)?
.iter()
.map(|it| tiles.get(it).map(tiles::draw_tile).unwrap())
.collect();
rendered.insert(player_ent, tiles);
}
// rendered.push(tiles);
// }
target.0 = rendered;
// target.0 = rendered;
// trace!("render_changed_hand");
// Ok(())
// }
Ok(())
}

View file

@ -1,10 +1,12 @@
use bevy::prelude::*;
use bevy_ratatui::RatatuiContext;
use jong::game::player::{MainPlayer, Player};
use crate::tui::render::hand;
pub(crate) fn draw_ingame(
rendered_hand: Res<hand::RenderedHand>,
main_player: Single<Entity, (With<Player>, With<MainPlayer>)>,
mut tui_ctx: ResMut<RatatuiContext>,
) -> Result {
use ratatui::layout::Flex;
@ -17,8 +19,9 @@ pub(crate) fn draw_ingame(
let mut area = frame.area();
area.height = 4;
let areas = layout.areas::<13>(area);
if let Some(tiles) = rendered_hand.0.first() {
for (tile, area) in tiles.iter().zip(areas.iter()) {
// 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);
}
}