turnstates
This commit is contained in:
parent
8a8a702a08
commit
6e6b792a58
7 changed files with 133 additions and 41 deletions
|
|
@ -1,9 +1,12 @@
|
|||
use bevy::prelude::*;
|
||||
use strum::{EnumCount, FromRepr};
|
||||
|
||||
use crate::{
|
||||
EnumNextCycle,
|
||||
game::{
|
||||
hand::Hand,
|
||||
hand::{Drawn, Hand},
|
||||
player::{MainPlayer, Player},
|
||||
round::{CurrentPlayer, TurnState, Wind},
|
||||
wall::Wall,
|
||||
},
|
||||
tile::{self, *},
|
||||
|
|
@ -29,50 +32,45 @@ impl Plugin for Riichi {
|
|||
app
|
||||
// start stopper
|
||||
.init_state::<GameState>()
|
||||
.add_sub_state::<TurnState>()
|
||||
.init_resource::<round::MatchSettings>()
|
||||
.init_resource::<round::Compass>()
|
||||
.add_systems(Startup, tile::init_tiles)
|
||||
.add_systems(OnEnter(GameState::Setup), setup)
|
||||
.add_systems(OnEnter(GameState::Deal), shuffle_deal)
|
||||
.add_systems(OnEnter(GameState::Deal), hand::shuffle_deal)
|
||||
.add_systems(Update, hand::sort_hands.run_if(in_state(GameState::Play)))
|
||||
// .add_systems(Update, turn_manager.run_if(in_state(GameState::Play)))
|
||||
.add_systems(OnEnter(TurnState::Tsumo), tsumo)
|
||||
// semicolon stopper
|
||||
;
|
||||
}
|
||||
}
|
||||
|
||||
fn shuffle_deal(
|
||||
// struct TurnEvent {
|
||||
// pub next: Option<Entity>,
|
||||
// pub prev: Option<Entity>,
|
||||
// }
|
||||
|
||||
// fn turn_manager() {}
|
||||
|
||||
fn tsumo(
|
||||
mut commands: Commands,
|
||||
tiles: Populated<Entity, With<Tile>>,
|
||||
players: Populated<Entity, With<Player>>,
|
||||
curr_player: Res<CurrentPlayer>,
|
||||
// players: Populated<Entity, With<Player>>,
|
||||
wall_ent: Single<Entity, With<Wall>>,
|
||||
mut next_gamestate: ResMut<NextState<GameState>>,
|
||||
) -> Result {
|
||||
use rand::seq::SliceRandom;
|
||||
walltiles: Single<&Children, With<Wall>>,
|
||||
curr_turnstate: Res<State<TurnState>>,
|
||||
mut next_turnstate: ResMut<NextState<TurnState>>,
|
||||
) {
|
||||
trace!("tsumo for: {:?}", curr_player.0);
|
||||
|
||||
let mut rng = rand::rng();
|
||||
let mut walltiles: Vec<_> = tiles.iter().collect();
|
||||
walltiles.shuffle(&mut rng);
|
||||
let drawn = walltiles.last().unwrap();
|
||||
commands.entity(*wall_ent).remove_child(*drawn);
|
||||
commands.entity(curr_player.0).insert(Drawn(*drawn));
|
||||
|
||||
// commands.get_entity(*wall_ent)?.replace_children(&walltiles);
|
||||
debug!("wall: {:?}", *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(())
|
||||
next_turnstate.set(curr_turnstate.next());
|
||||
}
|
||||
|
||||
pub(crate) fn setup(
|
||||
|
|
@ -88,10 +86,16 @@ pub(crate) fn setup(
|
|||
};
|
||||
let points = player::Points(matchsettings.starting_points);
|
||||
|
||||
let bundle = (player, points, Hand);
|
||||
let bundle = (
|
||||
player,
|
||||
points,
|
||||
Hand,
|
||||
Wind::from_repr((i - 1) as usize).unwrap(),
|
||||
);
|
||||
|
||||
if i == 1 {
|
||||
commands.spawn((bundle, MainPlayer));
|
||||
let player = commands.spawn((bundle, MainPlayer)).id();
|
||||
commands.insert_resource(CurrentPlayer(player));
|
||||
} else {
|
||||
commands.spawn(bundle);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue