detect hand change and render it
This commit is contained in:
parent
d506a25716
commit
3417384b86
9 changed files with 138 additions and 68 deletions
|
|
@ -1,6 +1,7 @@
|
|||
use bevy::input::keyboard::Key;
|
||||
use bevy::prelude::*;
|
||||
use bevy_ratatui::RatatuiContext;
|
||||
use ratatui::widgets::Block;
|
||||
use tui_logger::TuiLoggerWidget;
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Default, States)]
|
||||
|
|
@ -33,7 +34,8 @@ pub(crate) fn toggle_console(
|
|||
|
||||
pub(crate) fn draw_console(mut tui_ctx: ResMut<RatatuiContext>) -> Result {
|
||||
tui_ctx.draw(|frame| {
|
||||
frame.render_widget(TuiLoggerWidget::default(), frame.area());
|
||||
let block = Block::bordered().title("console");
|
||||
frame.render_widget(TuiLoggerWidget::default().block(block), frame.area());
|
||||
})?;
|
||||
|
||||
Ok(())
|
||||
|
|
|
|||
|
|
@ -2,14 +2,26 @@ use std::time::Duration;
|
|||
|
||||
use bevy::{app::ScheduleRunnerPlugin, prelude::*, state::app::StatesPlugin};
|
||||
use bevy_ratatui::RatatuiPlugins;
|
||||
use jong::tiles::InWall;
|
||||
use ratatui::{text::ToSpan, widgets::Paragraph};
|
||||
|
||||
use jong::game::GameState;
|
||||
use jong::game::wall::InWall;
|
||||
|
||||
mod console;
|
||||
mod input;
|
||||
mod render;
|
||||
|
||||
pub struct RiichiTui;
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, States, Default)]
|
||||
enum TuiState {
|
||||
MainMenu,
|
||||
#[default]
|
||||
InGame,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct RiichiTui {
|
||||
// player_names: Vec<String>,
|
||||
}
|
||||
|
||||
impl Plugin for RiichiTui {
|
||||
fn build(&self, app: &mut App) {
|
||||
|
|
@ -25,20 +37,19 @@ impl Plugin for RiichiTui {
|
|||
},
|
||||
))
|
||||
.add_plugins(StatesPlugin)
|
||||
|
||||
// setup console
|
||||
.init_state::<console::ConsoleState>()
|
||||
.add_systems(Update, console::toggle_console)
|
||||
.add_systems(Update, console::draw_console.run_if(in_state(console::ConsoleState::Open)))
|
||||
|
||||
.init_state::<TuiState>()
|
||||
.add_systems(Update, input::keyboard_input_system)
|
||||
// .add_systems(Update, input::keyboard_input_system)
|
||||
.add_systems(Update, render::draw_ingame.run_if(in_state(TuiState::InGame)))
|
||||
.add_systems(Update,render::render_changed_hand.run_if(in_state(GameState::Play)))
|
||||
// semicolon stopper
|
||||
;
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, States, Default)]
|
||||
enum TuiState {
|
||||
MainMenu,
|
||||
#[default]
|
||||
InGame,
|
||||
}
|
||||
// fn prompt_names() {}
|
||||
|
|
|
|||
|
|
@ -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(())
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ use ratatui::widgets::Paragraph;
|
|||
|
||||
use jong::tiles::Tile;
|
||||
|
||||
pub(crate) fn draw_tile(tile: &Tile) -> Paragraph<'_> {
|
||||
pub(crate) fn draw_tile(tile: &Tile) -> Paragraph<'static> {
|
||||
use ratatui::prelude::*;
|
||||
|
||||
let block = ratatui::widgets::Block::bordered();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue