simplify game setup

This commit is contained in:
Tao Tien 2026-01-13 00:01:38 -08:00
parent f4c4339204
commit 30d19ed9d9
8 changed files with 145 additions and 171 deletions

View file

@ -1,12 +1,17 @@
use bevy::prelude::*;
use crate::{
game::player::MainPlayer,
game::{
hand::Hand,
player::{MainPlayer, Player},
wall::Wall,
},
tiles::{self, *},
};
pub mod hand;
pub mod player;
pub mod round;
pub mod wall;
#[derive(States, Default, Hash, Clone, Eq, Debug, PartialEq, Copy)]
@ -14,98 +19,76 @@ pub enum GameState {
#[default]
None,
Setup,
// Deal,
Deal,
Play,
Score,
}
pub struct Riichi;
impl Plugin for Riichi {
fn build(&self, app: &mut App) {
app.init_resource::<Compass>()
.add_systems(Startup, init_match)
.add_systems(Startup, tiles::init_tiles)
app
// start stopper
.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)))
.init_resource::<round::MatchSettings>()
.init_resource::<round::Compass>()
.add_systems(Startup, tiles::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 setup_done(mut next: ResMut<NextState<GameState>>) {
next.set(GameState::Play);
trace!("setup_done");
fn shuffle_deal(
mut commands: Commands,
tiles: Query<Entity, With<Tile>>,
players: Populated<Entity, With<Player>>,
wall_ent: Single<Entity, With<Wall>>,
mut next_gamestate: ResMut<NextState<GameState>>,
) -> Result {
use rand::seq::SliceRandom;
let mut rng = rand::rng();
let mut wall: Vec<_> = tiles.iter().collect();
wall.shuffle(&mut rng);
commands.get_entity(*wall_ent)?.replace_children(&wall);
for player_ent in players {
let handtiles = wall.split_off(13);
commands.get_entity(*wall_ent)?.remove_children(&handtiles);
let hand = commands.spawn(Hand).add_children(&handtiles).id();
commands.get_entity(player_ent)?.replace_children(&[hand]);
}
next_gamestate.set(GameState::Play);
Ok(())
}
#[derive(Component)]
pub(crate) struct Dice(u8, u8);
pub(crate) fn setup(
mut commands: Commands,
matchsettings: Res<round::MatchSettings>,
// mut compass: ResMut<Compass>
tiles: Query<Entity, With<Tile>>,
mut next_gamestate: ResMut<NextState<GameState>>,
) {
for i in 1..=matchsettings.player_count {
let player = player::Player {
name: format!("Player {}", i),
};
let points = player::Points(matchsettings.starting_points);
#[derive(Resource)]
pub(crate) struct Compass {
pub(crate) prevalent_wind: Wind,
pub(crate) round: u8,
pub(crate) dealer_wind: Wind,
pub(crate) riichi: usize,
pub(crate) honba: usize,
}
let bundle = (player, points, Hand);
impl Default for Compass {
fn default() -> Self {
Self {
prevalent_wind: Wind::Ton,
round: 1,
dealer_wind: Wind::Ton,
riichi: 0,
honba: 0,
if i == 1 {
commands.spawn((bundle, MainPlayer));
} else {
commands.spawn(bundle);
}
}
}
#[derive(Resource)]
pub(crate) struct MatchSettings {
pub(crate) starting_points: isize,
pub(crate) player_count: u8,
}
pub(crate) fn next_round(_compass: Res<Compass>) {}
pub(crate) fn init_match(
mut commands: Commands,
// mut compass: ResMut<Compass>
) {
let starting = 25000;
let player_count = 4;
commands.insert_resource(MatchSettings {
starting_points: starting,
player_count,
});
let players = (2..=player_count)
.map(|i| {
(
player::Player {
name: format!("Player {i}"),
},
player::Points(starting),
)
})
.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,
// round: 1,
// dealer_wind: todo!(),
// riichi: 0,
// honba: 0,
// }
commands.spawn(Wall);
next_gamestate.set(GameState::Deal);
}