jong/src/game/mod.rs

104 lines
2.6 KiB
Rust
Raw Normal View History

2026-01-08 20:49:19 -08:00
use bevy::prelude::*;
2026-01-12 21:57:08 -08:00
use crate::{
2026-01-13 00:01:38 -08:00
game::{
hand::Hand,
player::{MainPlayer, Player},
wall::Wall,
},
2026-01-12 21:57:08 -08:00
tiles::{self, *},
};
2026-01-08 20:49:19 -08:00
2026-01-12 01:54:59 -08:00
pub mod hand;
2026-01-11 20:10:30 -08:00
pub mod player;
2026-01-13 00:01:38 -08:00
pub mod round;
2026-01-09 06:54:17 -08:00
pub mod wall;
2026-01-08 23:58:26 -08:00
#[derive(States, Default, Hash, Clone, Eq, Debug, PartialEq, Copy)]
2026-01-11 20:10:30 -08:00
pub enum GameState {
#[default]
2026-01-12 01:08:27 -08:00
None,
2026-01-11 20:10:30 -08:00
Setup,
2026-01-13 00:01:38 -08:00
Deal,
2026-01-11 20:10:30 -08:00
Play,
}
2026-01-08 20:49:19 -08:00
2026-01-11 20:10:30 -08:00
pub struct Riichi;
2026-01-08 20:49:19 -08:00
impl Plugin for Riichi {
fn build(&self, app: &mut App) {
2026-01-13 00:01:38 -08:00
app
// start stopper
2026-01-11 20:10:30 -08:00
.init_state::<GameState>()
2026-01-13 00:01:38 -08:00
.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)))
2026-01-09 06:54:17 -08:00
// semicolon stopper
;
2026-01-08 20:49:19 -08:00
}
}
2026-01-13 00:01:38 -08:00
fn shuffle_deal(
mut commands: Commands,
2026-01-13 01:08:14 -08:00
tiles: Populated<Entity, With<Tile>>,
2026-01-13 00:01:38 -08:00
players: Populated<Entity, With<Player>>,
wall_ent: Single<Entity, With<Wall>>,
mut next_gamestate: ResMut<NextState<GameState>>,
) -> Result {
use rand::seq::SliceRandom;
2026-01-12 01:08:27 -08:00
2026-01-13 00:01:38 -08:00
let mut rng = rand::rng();
2026-01-13 01:08:14 -08:00
let mut walltiles: Vec<_> = tiles.iter().collect();
walltiles.shuffle(&mut rng);
2026-01-08 20:49:19 -08:00
2026-01-13 01:08:14 -08:00
// commands.get_entity(*wall_ent)?.replace_children(&walltiles);
2026-01-08 20:49:19 -08:00
2026-01-13 00:01:38 -08:00
for player_ent in players {
2026-01-13 01:08:14 -08:00
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:?}");
2026-01-08 20:49:19 -08:00
}
2026-01-13 01:08:14 -08:00
commands.get_entity(*wall_ent)?.replace_children(&walltiles);
2026-01-13 00:01:38 -08:00
next_gamestate.set(GameState::Play);
Ok(())
2026-01-08 20:49:19 -08:00
}
2026-01-13 00:01:38 -08:00
pub(crate) fn setup(
2026-01-08 20:49:19 -08:00
mut commands: Commands,
2026-01-13 00:01:38 -08:00
matchsettings: Res<round::MatchSettings>,
2026-01-08 23:58:26 -08:00
// mut compass: ResMut<Compass>
2026-01-13 00:01:38 -08:00
tiles: Query<Entity, With<Tile>>,
mut next_gamestate: ResMut<NextState<GameState>>,
2026-01-08 20:49:19 -08:00
) {
2026-01-13 00:01:38 -08:00
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);
2026-01-08 20:49:19 -08:00
2026-01-13 00:01:38 -08:00
if i == 1 {
commands.spawn((bundle, MainPlayer));
} else {
commands.spawn(bundle);
}
}
2026-01-08 20:49:19 -08:00
2026-01-13 00:01:38 -08:00
commands.spawn(Wall);
2026-01-08 20:49:19 -08:00
2026-01-13 00:01:38 -08:00
next_gamestate.set(GameState::Deal);
2026-01-08 20:49:19 -08:00
}