big ass input system
This commit is contained in:
parent
81cb5c24d4
commit
a6079103a4
6 changed files with 73 additions and 44 deletions
|
|
@ -30,7 +30,8 @@ pub(crate) fn deal_hands(
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn sort_hand(
|
#[allow(clippy::type_complexity)]
|
||||||
|
pub fn sort_hands(
|
||||||
mut commands: Commands,
|
mut commands: Commands,
|
||||||
tiles: Populated<&Tile>,
|
tiles: Populated<&Tile>,
|
||||||
handtiles_entity: Single<Entity, With<HandTiles>>,
|
handtiles_entity: Single<Entity, With<HandTiles>>,
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,5 @@
|
||||||
use bevy::input::keyboard::Key;
|
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
use bevy_ratatui::RatatuiContext;
|
use bevy_ratatui::RatatuiContext;
|
||||||
use bevy_ratatui::event::KeyMessage;
|
|
||||||
use ratatui::crossterm::event::KeyCode;
|
|
||||||
use ratatui::widgets::Block;
|
use ratatui::widgets::Block;
|
||||||
use tui_logger::TuiLoggerWidget;
|
use tui_logger::TuiLoggerWidget;
|
||||||
|
|
||||||
|
|
@ -24,22 +21,6 @@ impl std::ops::Not for ConsoleState {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn toggle_console(
|
|
||||||
// input: Res<ButtonInput<Key>>,
|
|
||||||
mut messages: MessageReader<KeyMessage>,
|
|
||||||
curr_state: Res<State<ConsoleState>>,
|
|
||||||
mut next_state: ResMut<NextState<ConsoleState>>,
|
|
||||||
) {
|
|
||||||
// if input.just_pressed(Key::Character("`".into())) {
|
|
||||||
// next_state.set(!*curr_state.get());
|
|
||||||
// }
|
|
||||||
for message in messages.read() {
|
|
||||||
if let KeyCode::Char('`') = message.code {
|
|
||||||
next_state.set(!*curr_state.get());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn draw_console(mut tui_ctx: ResMut<RatatuiContext>) -> Result {
|
pub(crate) fn draw_console(mut tui_ctx: ResMut<RatatuiContext>) -> Result {
|
||||||
tui_ctx.draw(|frame| {
|
tui_ctx.draw(|frame| {
|
||||||
let block = Block::bordered().title("console");
|
let block = Block::bordered().title("console");
|
||||||
|
|
|
||||||
|
|
@ -27,24 +27,3 @@ pub(crate) fn draw_mainmenu(
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn mainmenu_input(
|
|
||||||
// input: Res<ButtonInput<Key>>,
|
|
||||||
mut messages: MessageReader<KeyMessage>,
|
|
||||||
mut next_tuistate: ResMut<NextState<(TuiState)>>,
|
|
||||||
mut next_gamestate: ResMut<NextState<(GameState)>>,
|
|
||||||
mut exit: MessageWriter<AppExit>,
|
|
||||||
) {
|
|
||||||
for message in messages.read() {
|
|
||||||
match message.code {
|
|
||||||
KeyCode::Char('p') => {
|
|
||||||
next_tuistate.set(TuiState::InGame);
|
|
||||||
next_gamestate.set(GameState::Setup);
|
|
||||||
}
|
|
||||||
KeyCode::Char('q') => {
|
|
||||||
exit.write_default();
|
|
||||||
}
|
|
||||||
_ => {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -2,17 +2,20 @@ use std::time::Duration;
|
||||||
|
|
||||||
use bevy::{app::ScheduleRunnerPlugin, prelude::*, state::app::StatesPlugin};
|
use bevy::{app::ScheduleRunnerPlugin, prelude::*, state::app::StatesPlugin};
|
||||||
use bevy_ratatui::RatatuiPlugins;
|
use bevy_ratatui::RatatuiPlugins;
|
||||||
|
use bevy_ratatui::event::KeyMessage;
|
||||||
use ratatui::{text::ToSpan, widgets::Paragraph};
|
use ratatui::{text::ToSpan, widgets::Paragraph};
|
||||||
|
|
||||||
use jong::game::GameState;
|
use jong::game::GameState;
|
||||||
use jong::game::wall::InWall;
|
use jong::game::wall::InWall;
|
||||||
|
|
||||||
|
use crate::tui::console::ConsoleState;
|
||||||
|
|
||||||
mod console;
|
mod console;
|
||||||
mod menu;
|
mod menu;
|
||||||
mod render;
|
mod render;
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, States, Default)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, States, Default)]
|
||||||
enum TuiState {
|
pub(crate) enum TuiState {
|
||||||
#[default]
|
#[default]
|
||||||
MainMenu,
|
MainMenu,
|
||||||
InGame,
|
InGame,
|
||||||
|
|
@ -56,9 +59,10 @@ impl Plugin for RiichiTui {
|
||||||
// general setup
|
// general setup
|
||||||
.init_state::<TuiState>()
|
.init_state::<TuiState>()
|
||||||
.add_computed_state::<InGame>()
|
.add_computed_state::<InGame>()
|
||||||
|
.add_systems(Update, input_system)
|
||||||
|
|
||||||
// main menu
|
// main menu
|
||||||
.add_systems(Update, (menu::draw_mainmenu, menu::mainmenu_input).run_if(in_state(TuiState::MainMenu)))
|
.add_systems(Update, menu::draw_mainmenu.run_if(in_state(TuiState::MainMenu)))
|
||||||
|
|
||||||
// gaming
|
// gaming
|
||||||
.init_resource::<render::hand::RenderedHand>()
|
.init_resource::<render::hand::RenderedHand>()
|
||||||
|
|
@ -70,3 +74,55 @@ impl Plugin for RiichiTui {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[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();
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
},
|
||||||
|
GameState::Score => todo!(),
|
||||||
|
_ => unreachable!("TuiState::InGame but GameState invalid")
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -14,6 +14,18 @@ pub(crate) fn render_changed_hand(
|
||||||
tiles: Populated<&Tile>,
|
tiles: Populated<&Tile>,
|
||||||
mut target: ResMut<RenderedHand>,
|
mut target: ResMut<RenderedHand>,
|
||||||
) -> Result {
|
) -> Result {
|
||||||
|
let mut rendered = vec![];
|
||||||
|
|
||||||
|
for hand in hands {
|
||||||
|
let hand_tiles = hand
|
||||||
|
.iter()
|
||||||
|
.map(|inhand| -> Result<_> { Ok(tiles.get(inhand).map(tiles::draw_tile)?) })
|
||||||
|
.collect::<Result<Vec<_>>>()?;
|
||||||
|
rendered.push(hand_tiles);
|
||||||
|
}
|
||||||
|
|
||||||
|
target.0 = rendered;
|
||||||
|
|
||||||
trace!("render_changed_hand");
|
trace!("render_changed_hand");
|
||||||
|
|
||||||
render_hand(hand, tiles, target)?;
|
render_hand(hand, tiles, target)?;
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,7 @@ pub(crate) fn draw_ingame(
|
||||||
use ratatui::prelude::*;
|
use ratatui::prelude::*;
|
||||||
|
|
||||||
tui_ctx.draw(|frame| {
|
tui_ctx.draw(|frame| {
|
||||||
debug!("{}", frame.area());
|
// debug!("{}", frame.area());
|
||||||
|
|
||||||
let layout = Layout::horizontal(vec![Constraint::Max(5); 13]).flex(Flex::Start);
|
let layout = Layout::horizontal(vec![Constraint::Max(5); 13]).flex(Flex::Start);
|
||||||
let mut area = frame.area();
|
let mut area = frame.area();
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue