use bevy::prelude::*; use crate::{ game::{ hand::Hand, player::{MainPlayer, Player}, wall::Wall, }, tile::{self, *}, }; pub mod hand; pub mod player; pub mod round; pub mod wall; #[derive(States, Default, Hash, Clone, Eq, Debug, PartialEq, Copy)] pub enum GameState { #[default] None, Setup, Deal, Play, } pub struct Riichi; impl Plugin for Riichi { fn build(&self, app: &mut App) { app // start stopper .init_state::() .init_resource::() .init_resource::() .add_systems(Startup, tile::init_tiles) .add_systems(OnEnter(GameState::Setup), setup) .add_systems(OnEnter(GameState::Deal), shuffle_deal) .add_systems(Update, hand::sort_hands.run_if(in_state(GameState::Play))) // semicolon stopper ; } } fn shuffle_deal( mut commands: Commands, tiles: Populated>, players: Populated>, wall_ent: Single>, mut next_gamestate: ResMut>, ) -> Result { use rand::seq::SliceRandom; let mut rng = rand::rng(); let mut walltiles: Vec<_> = tiles.iter().collect(); walltiles.shuffle(&mut rng); // commands.get_entity(*wall_ent)?.replace_children(&walltiles); for player_ent in players { let handtiles = walltiles.split_off(walltiles.len() - 13); // commands.get_entity(*wall_ent)?.remove_children(&handtiles); let hand_ent = commands.spawn(Hand).add_children(&handtiles).id(); debug!("hand_ent: {hand_ent:?}"); commands .get_entity(player_ent)? .replace_children(&[hand_ent]); debug!("dealt to player_ent {player_ent:?}"); } commands.get_entity(*wall_ent)?.replace_children(&walltiles); next_gamestate.set(GameState::Play); Ok(()) } pub(crate) fn setup( mut commands: Commands, matchsettings: Res, // mut compass: ResMut tiles: Query>, mut next_gamestate: ResMut>, ) { for i in 1..=matchsettings.player_count { let player = player::Player { name: format!("Player {}", i), }; let points = player::Points(matchsettings.starting_points); let bundle = (player, points, Hand); if i == 1 { commands.spawn((bundle, MainPlayer)); } else { commands.spawn(bundle); } } commands.spawn(Wall); next_gamestate.set(GameState::Deal); }