Compare commits

..

1 commit

Author SHA1 Message Date
Tao Tien
5504db4e0f big ass input system 2026-01-12 21:36:41 -08:00
8 changed files with 59 additions and 75 deletions

View file

@ -1,9 +1,6 @@
use bevy::prelude::*;
use crate::{
game::{player::Player, wall::WallTiles},
tiles::Tile,
};
use crate::{game::wall::WallTiles, tiles::Tile};
#[derive(Component)]
pub struct Hand;
@ -20,39 +17,38 @@ pub(crate) fn deal_hands(
mut commands: Commands,
walltiles: Single<&WallTiles>,
walltiles_entity: Single<Entity, With<WallTiles>>,
players: Populated<Entity, With<Player>>,
) -> Result {
let mut wall = walltiles.iter().collect::<Vec<_>>();
for player_entity in players {
let hand = wall.split_off(13);
let hand = walltiles.iter().collect::<Vec<_>>();
commands
.get_entity(*walltiles_entity)?
.remove_children(&hand);
.remove_children(hand.last_chunk::<13>().unwrap());
let handtiles = commands.spawn((Hand, HandTiles(hand))).id();
commands
.get_entity(player_entity)?
.add_children(&[handtiles]);
}
commands.spawn((Hand, HandTiles(hand)));
trace!("dealt hands");
Ok(())
}
#[allow(clippy::type_complexity)]
pub(crate) fn sort_hands(
pub(crate) fn sort_hand(
mut commands: Commands,
tiles: Populated<&Tile>,
mut hands: Populated<&mut Children, Changed<HandTiles>>,
handtiles_entity: Single<Entity, With<HandTiles>>,
handtiles: Single<&HandTiles, Changed<HandTiles>>,
) -> Result {
for (mut children) in hands {
children.sort_unstable_by_key(|e| tiles.get(*e).unwrap().suit);
trace!("sorted a hand")
}
let mut hand: Vec<_> = handtiles
.iter()
.map(|e| -> Result<(_, _)> { Ok((tiles.get(e)?, e)) })
.collect::<Result<_>>()?;
trace!("sort_hands");
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

@ -1,9 +1,6 @@
use bevy::prelude::*;
use crate::{
game::player::MainPlayer,
tiles::{self, *},
};
use crate::tiles::{self, *};
pub mod hand;
pub mod player;
@ -27,7 +24,7 @@ impl Plugin for Riichi {
.add_systems(Startup, tiles::init_tiles)
.init_state::<GameState>()
.add_systems(OnEnter(GameState::Setup), (wall::build_wall, hand::deal_hands, setup_done).chain())
.add_systems(Update, (hand::sort_hands).run_if(in_state(GameState::Play)))
.add_systems(Update, (hand::sort_hand).run_if(in_state(GameState::Play)))
// semicolon stopper
;
}
@ -82,7 +79,7 @@ pub(crate) fn init_match(
player_count,
});
let players = (2..=player_count)
let players = (1..=player_count)
.map(|i| {
(
player::Player {
@ -93,13 +90,6 @@ pub(crate) fn init_match(
})
.collect::<Vec<_>>();
commands.spawn_batch(players);
let main_player = (
player::Player {
name: format!("Player {}", 1),
},
player::Points(starting),
);
commands.spawn((main_player, MainPlayer));
// *compass = Compass {
// prevalent_wind: Wind::Ton,

View file

@ -6,12 +6,12 @@ use crate::{
};
#[derive(Component)]
pub struct Player {
pub name: String,
pub(crate) struct Player {
pub(crate) name: String,
}
#[derive(Component)]
pub struct Points(pub isize);
fn spawn_players(mut commands: Commands) {}
#[derive(Component)]
pub struct MainPlayer;
pub(crate) struct Points(pub isize);

View file

@ -43,7 +43,7 @@ fn main() {
tui_logger::init_logger(tui_logger::LevelFilter::Trace).unwrap();
tui_logger::set_env_filter_from_string(FILTERSTRING);
app.add_plugins(tui::RiichiTui)
app.add_plugins(tui::RiichiTui::default())
}
};

View file

@ -5,8 +5,8 @@ use tui_logger::TuiLoggerWidget;
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Default, States)]
pub(crate) enum ConsoleState {
Closed,
#[default]
Closed,
Open,
}

View file

@ -110,17 +110,11 @@ pub(crate) fn input_system(
_ => {}
},
TuiState::InGame => match gs {
GameState::Setup => match message.code {
_ => {}
},
GameState::Play => match message.code {
KeyCode::Char('q') => {
exit.write_default();
}
_ => {}
},
// GameState::None => todo!(),
GameState::Setup => todo!(),
GameState::Play => todo!(),
GameState::Score => todo!(),
_ => unreachable!("TuiState::InGame but GameState invalid")
_ => todo!()
},
}
}

View file

@ -7,26 +7,32 @@ use jong::tiles::Tile;
use crate::tui::render::tiles;
#[derive(Resource, Default)]
pub(crate) struct RenderedHand(pub(crate) Vec<Vec<Paragraph<'static>>>);
pub(crate) struct RenderedHand(pub(crate) Vec<Paragraph<'static>>);
pub(crate) fn render_changed_hand(
hands: Populated<&Children, Changed<HandTiles>>,
hand: Single<&HandTiles, Changed<HandTiles>>,
tiles: Populated<&Tile>,
mut target: ResMut<RenderedHand>,
) -> Result {
let mut rendered = vec![];
for hand in hands {
let tiles = hand
.iter()
.map(|inhand| tiles.get(inhand).map(tiles::draw_tile).unwrap())
.collect();
rendered.push(tiles);
}
target.0 = rendered;
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(())
}

View file

@ -17,11 +17,9 @@ pub(crate) fn draw_ingame(
let mut area = frame.area();
area.height = 4;
let areas = layout.areas::<13>(area);
if let Some(tiles) = rendered_hand.0.first() {
for (tile, area) in tiles.iter().zip(areas.iter()) {
for (tile, area) in rendered_hand.0.iter().zip(areas.iter()) {
frame.render_widget(tile, *area);
}
}
})?;
Ok(())