turnstates
This commit is contained in:
parent
8a8a702a08
commit
6e6b792a58
7 changed files with 133 additions and 41 deletions
|
|
@ -1,13 +1,16 @@
|
|||
use bevy::{ecs::relationship::RelationshipSourceCollection, prelude::*};
|
||||
|
||||
use crate::{
|
||||
game::{player::Player /* wall::WallTiles */},
|
||||
game::{GameState, player::Player, wall::Wall /* wall::WallTiles */},
|
||||
tile::Tile,
|
||||
};
|
||||
|
||||
#[derive(Component)]
|
||||
pub struct Hand;
|
||||
|
||||
#[derive(Component)]
|
||||
pub struct Drawn(pub Entity);
|
||||
|
||||
// #[derive(Component, Default)]
|
||||
// enum SortHand {
|
||||
// #[default]
|
||||
|
|
@ -26,3 +29,38 @@ pub(crate) fn sort_hands(
|
|||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn shuffle_deal(
|
||||
mut commands: Commands,
|
||||
tiles: Populated<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 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(())
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,3 +10,9 @@ pub struct Points(pub isize);
|
|||
|
||||
#[derive(Component)]
|
||||
pub struct MainPlayer;
|
||||
|
||||
#[derive(Component)]
|
||||
pub struct Dealer;
|
||||
|
||||
#[derive(Component)]
|
||||
pub struct Tsumo(pub Entity);
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
use bevy::prelude::*;
|
||||
use strum::{EnumCount, FromRepr};
|
||||
|
||||
use crate::tile::*;
|
||||
use crate::{EnumNextCycle, game::GameState};
|
||||
|
||||
#[derive(Component)]
|
||||
pub(crate) struct Dice(u8, u8);
|
||||
|
|
@ -40,3 +41,46 @@ impl Default for MatchSettings {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Component, Clone, Copy, FromRepr, EnumCount)]
|
||||
pub enum Wind {
|
||||
Ton,
|
||||
Nan,
|
||||
Shaa,
|
||||
Pei,
|
||||
}
|
||||
|
||||
impl EnumNextCycle for Wind {
|
||||
fn next(&self) -> Self {
|
||||
if (*self as usize + 1) >= Self::COUNT {
|
||||
Self::from_repr(0).unwrap()
|
||||
} else {
|
||||
Self::from_repr(*self as usize + 1).unwrap()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Resource)]
|
||||
pub(crate) struct CurrentPlayer(pub Entity);
|
||||
|
||||
#[derive(SubStates, Default, Clone, Copy, PartialEq, Eq, Hash, Debug, FromRepr, EnumCount)]
|
||||
#[source(GameState = GameState::Play)]
|
||||
pub(crate) enum TurnState {
|
||||
#[default]
|
||||
Tsumo,
|
||||
Menzen,
|
||||
RiichiKan,
|
||||
Discard,
|
||||
RonChiiPonKan,
|
||||
End,
|
||||
}
|
||||
|
||||
impl EnumNextCycle for TurnState {
|
||||
fn next(&self) -> Self {
|
||||
if (*self as usize + 1) >= Self::COUNT {
|
||||
Self::from_repr(0).unwrap()
|
||||
} else {
|
||||
Self::from_repr(*self as usize + 1).unwrap()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
use bevy::prelude::*;
|
||||
use rand::seq::SliceRandom;
|
||||
|
||||
use crate::tile::Tile;
|
||||
|
||||
#[derive(Component)]
|
||||
pub struct Wall;
|
||||
|
||||
#[derive(Component)]
|
||||
pub struct Dead;
|
||||
|
|
|
|||
|
|
@ -1,5 +1,7 @@
|
|||
#![allow(unused)]
|
||||
|
||||
pub mod game;
|
||||
pub mod tile;
|
||||
pub mod yakus;
|
||||
|
||||
trait EnumNextCycle {
|
||||
fn next(&self) -> Self;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,3 @@
|
|||
#![allow(unused)]
|
||||
|
||||
use bevy::{log::LogPlugin, prelude::*};
|
||||
use clap::{Parser, Subcommand};
|
||||
use tracing::Level;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue