we start printing again
This commit is contained in:
parent
65ea256436
commit
59399c3590
7 changed files with 114 additions and 52 deletions
54
src/game/hand.rs
Normal file
54
src/game/hand.rs
Normal 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(())
|
||||
}
|
||||
|
|
@ -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::<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
|
||||
;
|
||||
}
|
||||
|
|
@ -31,6 +33,7 @@ impl Plugin for Riichi {
|
|||
|
||||
fn setup_done(mut next: ResMut<NextState<GameState>>) {
|
||||
next.set(GameState::Play);
|
||||
trace!("setup_done");
|
||||
}
|
||||
|
||||
#[derive(Component)]
|
||||
|
|
|
|||
|
|
@ -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<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(())
|
||||
}
|
||||
|
|
|
|||
12
src/tiles.rs
12
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,
|
||||
|
|
|
|||
|
|
@ -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::<console::ConsoleState>()
|
||||
.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::<TuiState>()
|
||||
.add_computed_state::<InGame>()
|
||||
.add_systems(Update, curr_state)
|
||||
|
||||
// main menu
|
||||
.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::<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::ingame::draw_ingame.run_if(in_state(TuiState::InGame)))
|
||||
|
||||
// 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)]
|
||||
struct InGame;
|
||||
|
||||
|
|
|
|||
|
|
@ -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<Paragraph<'a>>);
|
||||
#[derive(Resource, Default)]
|
||||
pub(crate) struct RenderedHand(pub(crate) Vec<Paragraph<'static>>);
|
||||
|
||||
pub(crate) fn render_changed_hand(
|
||||
hand: Single<&HandTiles, Changed<HandTiles>>,
|
||||
// hand_tiles: Query<&HandTiles, With<Hand>>,
|
||||
tiles: Query<&Tile>,
|
||||
mut target: Single<&'static mut RenderedHand>,
|
||||
tiles: Populated<&Tile>,
|
||||
mut target: ResMut<RenderedHand>,
|
||||
) -> 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
|
||||
.iter()
|
||||
.map(|inhand| -> Result<_> { Ok(tiles.get(inhand).map(tiles::draw_tile)?) })
|
||||
.collect::<Result<Vec<_>>>()?;
|
||||
|
||||
target.0 = hand_tiles;
|
||||
|
||||
Ok(())
|
||||
|
|
|
|||
|
|
@ -4,10 +4,7 @@ use bevy_ratatui::RatatuiContext;
|
|||
use crate::tui::render::hand;
|
||||
|
||||
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 hand::RenderedHand>,
|
||||
rendered_hand: Res<hand::RenderedHand>,
|
||||
mut tui_ctx: ResMut<RatatuiContext>,
|
||||
) -> Result {
|
||||
use ratatui::layout::Flex;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue