diff --git a/src/game/hand.rs b/src/game/hand.rs new file mode 100644 index 0000000..4daa915 --- /dev/null +++ b/src/game/hand.rs @@ -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); + +pub(crate) fn deal_hands( + mut commands: Commands, + walltiles: Single<&WallTiles>, + walltiles_entity: Single>, +) -> Result { + let hand = walltiles.iter().collect::>(); + + 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>, + handtiles: Single<&HandTiles, Changed>, +) -> Result { + let mut hand: Vec<_> = handtiles + .iter() + .map(|e| -> Result<(_, _)> { Ok((tiles.get(e)?, e)) }) + .collect::>()?; + + 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(()) +} diff --git a/src/game/mod.rs b/src/game/mod.rs index 95b20e1..bf41e6c 100644 --- a/src/game/mod.rs +++ b/src/game/mod.rs @@ -3,6 +3,7 @@ use tracing::instrument; use crate::tiles::{self, *}; +pub mod hand; pub mod player; pub mod wall; @@ -23,7 +24,8 @@ impl Plugin for Riichi { .add_systems(Startup, init_match) .add_systems(Startup, tiles::init_tiles) .init_state::() - .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 ; } @@ -31,6 +33,7 @@ impl Plugin for Riichi { fn setup_done(mut next: ResMut>) { next.set(GameState::Play); + trace!("setup_done"); } #[derive(Component)] diff --git a/src/game/player.rs b/src/game/player.rs index d1d263f..e4e3323 100644 --- a/src/game/player.rs +++ b/src/game/player.rs @@ -1,7 +1,7 @@ use bevy::prelude::*; use crate::{ - game::wall::{InWall, Wall, WallTiles}, + game::wall::{InWall, Wall}, tiles::Tile, }; @@ -15,30 +15,3 @@ fn spawn_players(mut commands: Commands) {} #[derive(Component)] pub(crate) struct Points(pub isize); -#[derive(Component)] -pub struct Hand; - -#[derive(Component)] -#[relationship_target(relationship = InHand, linked_spawn)] -pub struct HandTiles(Vec); - -#[derive(Component)] -#[relationship(relationship_target = HandTiles)] -pub struct InHand(pub Entity); - -pub(crate) fn deal_hands( - mut commands: Commands, - inwalls: Single<&WallTiles>, - walltiles: Single>, -) -> Result { - let hand = inwalls.iter().collect::>(); - - commands - .get_entity(*walltiles)? - .remove_children(hand.last_chunk::<13>().unwrap()); - - commands.spawn((Hand, HandTiles(hand))); - - trace!("dealt hands"); - Ok(()) -} diff --git a/src/tiles.rs b/src/tiles.rs index 655f74d..634e115 100644 --- a/src/tiles.rs +++ b/src/tiles.rs @@ -2,26 +2,26 @@ use bevy::{ecs::entity::MapEntities, prelude::*}; use strum::FromRepr; use tracing::instrument; -use crate::game::{player::HandTiles, wall::WallTiles}; +use crate::game::{hand::HandTiles, wall::WallTiles}; #[derive(Component, Debug)] pub struct Tile { pub suit: Suit, } -#[derive(MapEntities, Debug)] +#[derive(MapEntities, Debug, PartialEq, PartialOrd, Eq, Ord, Clone, Copy)] pub enum Suit { + Man(Rank), Pin(Rank), Sou(Rank), - Man(Rank), Wind(Wind), Dragon(Dragon), } -#[derive(Deref, DerefMut, Debug)] +#[derive(Deref, DerefMut, Debug, PartialEq, PartialOrd, Eq, Ord, Clone, Copy)] pub struct Rank(pub u8); -#[derive(FromRepr, Debug)] +#[derive(FromRepr, Debug, PartialEq, PartialOrd, Eq, Ord, Clone, Copy)] pub enum Wind { Ton, Nan, @@ -29,7 +29,7 @@ pub enum Wind { Pei, } -#[derive(Debug, FromRepr)] +#[derive(Debug, FromRepr, PartialEq, PartialOrd, Eq, Ord, Clone, Copy)] pub enum Dragon { Haku, Hatsu, diff --git a/src/tui/mod.rs b/src/tui/mod.rs index 35b0c94..639b23a 100644 --- a/src/tui/mod.rs +++ b/src/tui/mod.rs @@ -32,28 +32,51 @@ impl Plugin for RiichiTui { RatatuiPlugins { // enable_kitty_protocol: todo!(), enable_mouse_capture: true, - // enable_input_forwarding: false, + enable_input_forwarding: true, ..Default::default() }, )) .add_plugins(StatesPlugin) - // setup console + + // console .init_state::() - .add_systems(Update, console::toggle_console) .add_systems(Update, console::draw_console.run_if(in_state(console::ConsoleState::Open))) - // other setup + + // general setup .init_state::() .add_computed_state::() + .add_systems(Update, curr_state) + // 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 - .add_systems(Update, render::ingame::draw_ingame.run_if(in_state(TuiState::InGame))) + .init_resource::() + .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::ingame::draw_ingame.run_if(in_state(TuiState::InGame))) + // semicolon stopper ; } } +fn curr_state( + curr_gamestate: Option>>, + curr_tuistate: Res>, + curr_ingame: Option>>, +) { + 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)] struct InGame; diff --git a/src/tui/render/hand.rs b/src/tui/render/hand.rs index dc6cce5..9c76106 100644 --- a/src/tui/render/hand.rs +++ b/src/tui/render/hand.rs @@ -1,25 +1,37 @@ use bevy::prelude::*; use ratatui::widgets::Paragraph; -use jong::game::player::HandTiles; +use jong::game::hand::HandTiles; use jong::tiles::Tile; use crate::tui::render::tiles; -#[derive(Component)] -pub(crate) struct RenderedHand<'a>(pub(crate) Vec>); +#[derive(Resource, Default)] +pub(crate) struct RenderedHand(pub(crate) Vec>); pub(crate) fn render_changed_hand( hand: Single<&HandTiles, Changed>, - // hand_tiles: Query<&HandTiles, With>, - tiles: Query<&Tile>, - mut target: Single<&'static mut RenderedHand>, + tiles: Populated<&Tile>, + mut target: ResMut, ) -> Result { + trace!("render_changed_hand"); + + render_hand(hand, tiles, target)?; + + Ok(()) +} + +pub(crate) fn render_hand( + hand: Single<&HandTiles, Changed>, + tiles: Populated<&Tile>, + mut target: ResMut, +) -> Result { + trace!("render_hand"); + let hand_tiles = hand .iter() .map(|inhand| -> Result<_> { Ok(tiles.get(inhand).map(tiles::draw_tile)?) }) .collect::>>()?; - target.0 = hand_tiles; Ok(()) diff --git a/src/tui/render/ingame.rs b/src/tui/render/ingame.rs index 642c3f8..3d407f8 100644 --- a/src/tui/render/ingame.rs +++ b/src/tui/render/ingame.rs @@ -4,10 +4,7 @@ use bevy_ratatui::RatatuiContext; use crate::tui::render::hand; pub(crate) fn draw_ingame( - // tiles: Query<&Tile>, - // wall_tiles: Option>>, - // hand_tiles: Query<&HandTiles, With>, - rendered_hand: Single<&'static hand::RenderedHand>, + rendered_hand: Res, mut tui_ctx: ResMut, ) -> Result { use ratatui::layout::Flex;