we start printing again

This commit is contained in:
Tao Tien 2026-01-12 01:54:59 -08:00
parent 65ea256436
commit 59399c3590
7 changed files with 114 additions and 52 deletions

54
src/game/hand.rs Normal file
View file

@ -0,0 +1,54 @@
use bevy::prelude::*;
use crate::{game::wall::WallTiles, tiles::Tile};
#[derive(Component)]
pub struct Hand;
#[derive(Component)]
#[relationship(relationship_target = HandTiles)]
pub struct InHand(pub Entity);
#[derive(Component)]
#[relationship_target(relationship = InHand, linked_spawn)]
pub struct HandTiles(Vec<Entity>);
pub(crate) fn deal_hands(
mut commands: Commands,
walltiles: Single<&WallTiles>,
walltiles_entity: Single<Entity, With<WallTiles>>,
) -> Result {
let hand = walltiles.iter().collect::<Vec<_>>();
commands
.get_entity(*walltiles_entity)?
.remove_children(hand.last_chunk::<13>().unwrap());
commands.spawn((Hand, HandTiles(hand)));
trace!("dealt hands");
Ok(())
}
pub(crate) fn sort_hand(
mut commands: Commands,
tiles: Populated<&Tile>,
handtiles_entity: Single<Entity, With<HandTiles>>,
handtiles: Single<&HandTiles, Changed<HandTiles>>,
) -> Result {
let mut hand: Vec<_> = handtiles
.iter()
.map(|e| -> Result<(_, _)> { Ok((tiles.get(e)?, e)) })
.collect::<Result<_>>()?;
hand.sort_by_key(|(t, _)| t.suit);
let hand: Vec<_> = hand.iter().map(|(_, e)| *e).collect();
commands
.get_entity(*handtiles_entity)?
.replace_children(&hand);
trace!("sort_hand");
Ok(())
}

View file

@ -3,6 +3,7 @@ use tracing::instrument;
use crate::tiles::{self, *}; use crate::tiles::{self, *};
pub mod hand;
pub mod player; pub mod player;
pub mod wall; pub mod wall;
@ -23,7 +24,8 @@ impl Plugin for Riichi {
.add_systems(Startup, init_match) .add_systems(Startup, init_match)
.add_systems(Startup, tiles::init_tiles) .add_systems(Startup, tiles::init_tiles)
.init_state::<GameState>() .init_state::<GameState>()
.add_systems(OnEnter(GameState::Setup), (wall::build_wall, player::deal_hands, setup_done).chain()) .add_systems(OnEnter(GameState::Setup), (wall::build_wall, hand::deal_hands, setup_done).chain())
// .add_systems(Update, systems)
// semicolon stopper // semicolon stopper
; ;
} }
@ -31,6 +33,7 @@ impl Plugin for Riichi {
fn setup_done(mut next: ResMut<NextState<GameState>>) { fn setup_done(mut next: ResMut<NextState<GameState>>) {
next.set(GameState::Play); next.set(GameState::Play);
trace!("setup_done");
} }
#[derive(Component)] #[derive(Component)]

View file

@ -1,7 +1,7 @@
use bevy::prelude::*; use bevy::prelude::*;
use crate::{ use crate::{
game::wall::{InWall, Wall, WallTiles}, game::wall::{InWall, Wall},
tiles::Tile, tiles::Tile,
}; };
@ -15,30 +15,3 @@ fn spawn_players(mut commands: Commands) {}
#[derive(Component)] #[derive(Component)]
pub(crate) struct Points(pub isize); pub(crate) struct Points(pub isize);
#[derive(Component)]
pub struct Hand;
#[derive(Component)]
#[relationship_target(relationship = InHand, linked_spawn)]
pub struct HandTiles(Vec<Entity>);
#[derive(Component)]
#[relationship(relationship_target = HandTiles)]
pub struct InHand(pub Entity);
pub(crate) fn deal_hands(
mut commands: Commands,
inwalls: Single<&WallTiles>,
walltiles: Single<Entity, With<WallTiles>>,
) -> Result {
let hand = inwalls.iter().collect::<Vec<_>>();
commands
.get_entity(*walltiles)?
.remove_children(hand.last_chunk::<13>().unwrap());
commands.spawn((Hand, HandTiles(hand)));
trace!("dealt hands");
Ok(())
}

View file

@ -2,26 +2,26 @@ use bevy::{ecs::entity::MapEntities, prelude::*};
use strum::FromRepr; use strum::FromRepr;
use tracing::instrument; use tracing::instrument;
use crate::game::{player::HandTiles, wall::WallTiles}; use crate::game::{hand::HandTiles, wall::WallTiles};
#[derive(Component, Debug)] #[derive(Component, Debug)]
pub struct Tile { pub struct Tile {
pub suit: Suit, pub suit: Suit,
} }
#[derive(MapEntities, Debug)] #[derive(MapEntities, Debug, PartialEq, PartialOrd, Eq, Ord, Clone, Copy)]
pub enum Suit { pub enum Suit {
Man(Rank),
Pin(Rank), Pin(Rank),
Sou(Rank), Sou(Rank),
Man(Rank),
Wind(Wind), Wind(Wind),
Dragon(Dragon), Dragon(Dragon),
} }
#[derive(Deref, DerefMut, Debug)] #[derive(Deref, DerefMut, Debug, PartialEq, PartialOrd, Eq, Ord, Clone, Copy)]
pub struct Rank(pub u8); pub struct Rank(pub u8);
#[derive(FromRepr, Debug)] #[derive(FromRepr, Debug, PartialEq, PartialOrd, Eq, Ord, Clone, Copy)]
pub enum Wind { pub enum Wind {
Ton, Ton,
Nan, Nan,
@ -29,7 +29,7 @@ pub enum Wind {
Pei, Pei,
} }
#[derive(Debug, FromRepr)] #[derive(Debug, FromRepr, PartialEq, PartialOrd, Eq, Ord, Clone, Copy)]
pub enum Dragon { pub enum Dragon {
Haku, Haku,
Hatsu, Hatsu,

View file

@ -32,28 +32,51 @@ impl Plugin for RiichiTui {
RatatuiPlugins { RatatuiPlugins {
// enable_kitty_protocol: todo!(), // enable_kitty_protocol: todo!(),
enable_mouse_capture: true, enable_mouse_capture: true,
// enable_input_forwarding: false, enable_input_forwarding: true,
..Default::default() ..Default::default()
}, },
)) ))
.add_plugins(StatesPlugin) .add_plugins(StatesPlugin)
// setup console
// console
.init_state::<console::ConsoleState>() .init_state::<console::ConsoleState>()
.add_systems(Update, console::toggle_console)
.add_systems(Update, console::draw_console.run_if(in_state(console::ConsoleState::Open))) .add_systems(Update, console::draw_console.run_if(in_state(console::ConsoleState::Open)))
// other setup
// general setup
.init_state::<TuiState>() .init_state::<TuiState>()
.add_computed_state::<InGame>() .add_computed_state::<InGame>()
.add_systems(Update, curr_state)
// 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, menu::mainmenu_input).run_if(in_state(TuiState::MainMenu)))
// gaming // gaming
.add_systems(Update, render::ingame::draw_ingame.run_if(in_state(TuiState::InGame))) .init_resource::<render::hand::RenderedHand>()
.add_systems(OnEnter(GameState::Play), render::hand::render_hand)
.add_systems(Update, render::hand::render_changed_hand.run_if(in_state(InGame).and(in_state(GameState::Play)))) .add_systems(Update, render::hand::render_changed_hand.run_if(in_state(InGame).and(in_state(GameState::Play))))
.add_systems(Update, render::ingame::draw_ingame.run_if(in_state(TuiState::InGame)))
// semicolon stopper // semicolon stopper
; ;
} }
} }
fn curr_state(
curr_gamestate: Option<Res<State<GameState>>>,
curr_tuistate: Res<State<TuiState>>,
curr_ingame: Option<Res<State<InGame>>>,
) {
if let Some(curr_gamestate) = curr_gamestate && curr_gamestate.is_changed(){
trace!("GameState: {curr_gamestate:?}")
}
if curr_tuistate.is_changed() {
trace!("TuiState: {curr_tuistate:?}")
}
if let Some(curr_ingame) = curr_ingame && curr_ingame.is_changed(){
trace!("InGame: {curr_ingame:?}")
}
}
#[derive(Clone, PartialEq, Eq, Hash, Debug)] #[derive(Clone, PartialEq, Eq, Hash, Debug)]
struct InGame; struct InGame;

View file

@ -1,25 +1,37 @@
use bevy::prelude::*; use bevy::prelude::*;
use ratatui::widgets::Paragraph; use ratatui::widgets::Paragraph;
use jong::game::player::HandTiles; use jong::game::hand::HandTiles;
use jong::tiles::Tile; use jong::tiles::Tile;
use crate::tui::render::tiles; use crate::tui::render::tiles;
#[derive(Component)] #[derive(Resource, Default)]
pub(crate) struct RenderedHand<'a>(pub(crate) Vec<Paragraph<'a>>); pub(crate) struct RenderedHand(pub(crate) Vec<Paragraph<'static>>);
pub(crate) fn render_changed_hand( pub(crate) fn render_changed_hand(
hand: Single<&HandTiles, Changed<HandTiles>>, hand: Single<&HandTiles, Changed<HandTiles>>,
// hand_tiles: Query<&HandTiles, With<Hand>>, tiles: Populated<&Tile>,
tiles: Query<&Tile>, mut target: ResMut<RenderedHand>,
mut target: Single<&'static mut RenderedHand>,
) -> Result { ) -> Result {
trace!("render_changed_hand");
render_hand(hand, tiles, target)?;
Ok(())
}
pub(crate) fn render_hand(
hand: Single<&HandTiles, Changed<HandTiles>>,
tiles: Populated<&Tile>,
mut target: ResMut<RenderedHand>,
) -> Result {
trace!("render_hand");
let hand_tiles = hand let hand_tiles = hand
.iter() .iter()
.map(|inhand| -> Result<_> { Ok(tiles.get(inhand).map(tiles::draw_tile)?) }) .map(|inhand| -> Result<_> { Ok(tiles.get(inhand).map(tiles::draw_tile)?) })
.collect::<Result<Vec<_>>>()?; .collect::<Result<Vec<_>>>()?;
target.0 = hand_tiles; target.0 = hand_tiles;
Ok(()) Ok(())

View file

@ -4,10 +4,7 @@ use bevy_ratatui::RatatuiContext;
use crate::tui::render::hand; use crate::tui::render::hand;
pub(crate) fn draw_ingame( pub(crate) fn draw_ingame(
// tiles: Query<&Tile>, rendered_hand: Res<hand::RenderedHand>,
// wall_tiles: Option<Single<&WallTiles, With<Wall>>>,
// hand_tiles: Query<&HandTiles, With<Hand>>,
rendered_hand: Single<&'static hand::RenderedHand>,
mut tui_ctx: ResMut<RatatuiContext>, mut tui_ctx: ResMut<RatatuiContext>,
) -> Result { ) -> Result {
use ratatui::layout::Flex; use ratatui::layout::Flex;