simplify game setup
This commit is contained in:
parent
f4c4339204
commit
30d19ed9d9
8 changed files with 145 additions and 171 deletions
|
|
@ -1,58 +1,29 @@
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
game::{player::Player, wall::WallTiles},
|
game::{player::Player /* wall::WallTiles */},
|
||||||
tiles::Tile,
|
tiles::Tile,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
pub struct Hand;
|
pub struct Hand;
|
||||||
|
|
||||||
#[derive(Component)]
|
// #[derive(Component, Default)]
|
||||||
#[relationship(relationship_target = HandTiles)]
|
// enum SortHand {
|
||||||
pub struct InHand(pub Entity);
|
// #[default]
|
||||||
|
// Unsorted,
|
||||||
|
// Sort,
|
||||||
|
// Manual,
|
||||||
|
// }
|
||||||
|
|
||||||
#[derive(Component)]
|
|
||||||
#[relationship_target(relationship = InHand, linked_spawn)]
|
|
||||||
pub struct HandTiles(Vec<Entity>);
|
|
||||||
|
|
||||||
pub(crate) fn deal_hands(
|
|
||||||
mut commands: Commands,
|
|
||||||
walltiles: Single<&WallTiles>,
|
|
||||||
walltiles_entity: Single<Entity, With<WallTiles>>,
|
|
||||||
players: Populated<Entity, With<Player>>,
|
|
||||||
) -> Result {
|
|
||||||
let mut wall = walltiles.iter().collect::<Vec<_>>();
|
|
||||||
|
|
||||||
for player_entity in players {
|
|
||||||
let hand = wall.split_off(13);
|
|
||||||
|
|
||||||
commands
|
|
||||||
.get_entity(*walltiles_entity)?
|
|
||||||
.remove_children(&hand);
|
|
||||||
|
|
||||||
let handtiles = commands.spawn((Hand, HandTiles(hand))).id();
|
|
||||||
|
|
||||||
commands
|
|
||||||
.get_entity(player_entity)?
|
|
||||||
.add_children(&[handtiles]);
|
|
||||||
}
|
|
||||||
|
|
||||||
trace!("dealt hands");
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
#[allow(clippy::type_complexity)]
|
|
||||||
pub(crate) fn sort_hands(
|
pub(crate) fn sort_hands(
|
||||||
mut commands: Commands,
|
|
||||||
tiles: Populated<&Tile>,
|
tiles: Populated<&Tile>,
|
||||||
mut hands: Populated<&mut Children, Changed<HandTiles>>,
|
mut hands: Populated<&mut Children, (With<Player>, Changed<Hand>)>,
|
||||||
) -> Result {
|
) -> Result {
|
||||||
for (mut children) in hands {
|
for mut hand in hands {
|
||||||
children.sort_unstable_by_key(|e| tiles.get(*e).unwrap().suit);
|
hand.sort_unstable_by_key(|e| tiles.get(*e).unwrap().suit);
|
||||||
trace!("sorted a hand")
|
debug!("sorted: {hand:?}")
|
||||||
}
|
}
|
||||||
|
|
||||||
trace!("sort_hands");
|
trace!("sort_hands");
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
||||||
137
src/game/mod.rs
137
src/game/mod.rs
|
|
@ -1,12 +1,17 @@
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
game::player::MainPlayer,
|
game::{
|
||||||
|
hand::Hand,
|
||||||
|
player::{MainPlayer, Player},
|
||||||
|
wall::Wall,
|
||||||
|
},
|
||||||
tiles::{self, *},
|
tiles::{self, *},
|
||||||
};
|
};
|
||||||
|
|
||||||
pub mod hand;
|
pub mod hand;
|
||||||
pub mod player;
|
pub mod player;
|
||||||
|
pub mod round;
|
||||||
pub mod wall;
|
pub mod wall;
|
||||||
|
|
||||||
#[derive(States, Default, Hash, Clone, Eq, Debug, PartialEq, Copy)]
|
#[derive(States, Default, Hash, Clone, Eq, Debug, PartialEq, Copy)]
|
||||||
|
|
@ -14,98 +19,76 @@ pub enum GameState {
|
||||||
#[default]
|
#[default]
|
||||||
None,
|
None,
|
||||||
Setup,
|
Setup,
|
||||||
// Deal,
|
Deal,
|
||||||
Play,
|
Play,
|
||||||
Score,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Riichi;
|
pub struct Riichi;
|
||||||
impl Plugin for Riichi {
|
impl Plugin for Riichi {
|
||||||
fn build(&self, app: &mut App) {
|
fn build(&self, app: &mut App) {
|
||||||
app.init_resource::<Compass>()
|
app
|
||||||
.add_systems(Startup, init_match)
|
// start stopper
|
||||||
.add_systems(Startup, tiles::init_tiles)
|
|
||||||
.init_state::<GameState>()
|
.init_state::<GameState>()
|
||||||
.add_systems(OnEnter(GameState::Setup), (wall::build_wall, hand::deal_hands, setup_done).chain())
|
.init_resource::<round::MatchSettings>()
|
||||||
.add_systems(Update, (hand::sort_hands).run_if(in_state(GameState::Play)))
|
.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
|
// semicolon stopper
|
||||||
;
|
;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn setup_done(mut next: ResMut<NextState<GameState>>) {
|
fn shuffle_deal(
|
||||||
next.set(GameState::Play);
|
mut commands: Commands,
|
||||||
trace!("setup_done");
|
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) fn setup(
|
||||||
pub(crate) struct Dice(u8, u8);
|
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)]
|
let bundle = (player, points, Hand);
|
||||||
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,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Default for Compass {
|
if i == 1 {
|
||||||
fn default() -> Self {
|
commands.spawn((bundle, MainPlayer));
|
||||||
Self {
|
} else {
|
||||||
prevalent_wind: Wind::Ton,
|
commands.spawn(bundle);
|
||||||
round: 1,
|
|
||||||
dealer_wind: Wind::Ton,
|
|
||||||
riichi: 0,
|
|
||||||
honba: 0,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
commands.spawn(Wall);
|
||||||
#[derive(Resource)]
|
|
||||||
pub(crate) struct MatchSettings {
|
next_gamestate.set(GameState::Deal);
|
||||||
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,
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,6 @@
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
|
|
||||||
use crate::{
|
#[derive(Component, Debug)]
|
||||||
game::wall::{InWall, Wall},
|
|
||||||
tiles::Tile,
|
|
||||||
};
|
|
||||||
|
|
||||||
#[derive(Component)]
|
|
||||||
pub struct Player {
|
pub struct Player {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
}
|
}
|
||||||
|
|
|
||||||
42
src/game/round.rs
Normal file
42
src/game/round.rs
Normal file
|
|
@ -0,0 +1,42 @@
|
||||||
|
use bevy::prelude::*;
|
||||||
|
|
||||||
|
use crate::tiles::*;
|
||||||
|
|
||||||
|
#[derive(Component)]
|
||||||
|
pub(crate) struct Dice(u8, u8);
|
||||||
|
|
||||||
|
#[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,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for Compass {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self {
|
||||||
|
prevalent_wind: Wind::Ton,
|
||||||
|
round: 1,
|
||||||
|
dealer_wind: Wind::Ton,
|
||||||
|
riichi: 0,
|
||||||
|
honba: 0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Resource)]
|
||||||
|
pub(crate) struct MatchSettings {
|
||||||
|
pub(crate) starting_points: isize,
|
||||||
|
pub(crate) player_count: u8,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for MatchSettings {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self {
|
||||||
|
starting_points: 25000,
|
||||||
|
player_count: 4,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -5,22 +5,3 @@ use crate::tiles::Tile;
|
||||||
|
|
||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
pub struct Wall;
|
pub struct Wall;
|
||||||
|
|
||||||
#[derive(Component)]
|
|
||||||
#[relationship_target(relationship = InWall, linked_spawn)]
|
|
||||||
pub struct WallTiles(Vec<Entity>);
|
|
||||||
|
|
||||||
#[derive(Component)]
|
|
||||||
#[relationship(relationship_target = WallTiles)]
|
|
||||||
pub struct InWall(pub Entity);
|
|
||||||
|
|
||||||
pub(crate) fn build_wall(mut commands: Commands, tiles: Query<Entity, With<Tile>>) {
|
|
||||||
let mut rng = rand::rng();
|
|
||||||
|
|
||||||
let mut shuffled = tiles.iter().collect::<Vec<_>>();
|
|
||||||
shuffled.shuffle(&mut rng);
|
|
||||||
|
|
||||||
commands.spawn((Wall, WallTiles(shuffled)));
|
|
||||||
|
|
||||||
trace!("build_wall");
|
|
||||||
}
|
|
||||||
|
|
|
||||||
12
src/tiles.rs
12
src/tiles.rs
|
|
@ -37,27 +37,29 @@ pub enum Dragon {
|
||||||
pub struct Dora;
|
pub struct Dora;
|
||||||
|
|
||||||
pub fn init_tiles(mut commands: Commands) {
|
pub fn init_tiles(mut commands: Commands) {
|
||||||
|
let mut tiles = vec![];
|
||||||
for _ in 0..4 {
|
for _ in 0..4 {
|
||||||
for i in 1..=9 {
|
for i in 1..=9 {
|
||||||
commands.spawn(Tile {
|
tiles.push(Tile {
|
||||||
suit: Suit::Pin(Rank(i)),
|
suit: Suit::Pin(Rank(i)),
|
||||||
});
|
});
|
||||||
commands.spawn(Tile {
|
tiles.push(Tile {
|
||||||
suit: Suit::Sou(Rank(i)),
|
suit: Suit::Sou(Rank(i)),
|
||||||
});
|
});
|
||||||
commands.spawn(Tile {
|
tiles.push(Tile {
|
||||||
suit: Suit::Man(Rank(i)),
|
suit: Suit::Man(Rank(i)),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
for i in 0..4 {
|
for i in 0..4 {
|
||||||
commands.spawn(Tile {
|
tiles.push(Tile {
|
||||||
suit: Suit::Wind(Wind::from_repr(i).unwrap()),
|
suit: Suit::Wind(Wind::from_repr(i).unwrap()),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
for i in 0..3 {
|
for i in 0..3 {
|
||||||
commands.spawn(Tile {
|
tiles.push(Tile {
|
||||||
suit: Suit::Dragon(Dragon::from_repr(i).unwrap()),
|
suit: Suit::Dragon(Dragon::from_repr(i).unwrap()),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
commands.spawn_batch(tiles);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,9 +6,9 @@ use bevy_ratatui::event::KeyMessage;
|
||||||
use ratatui::{text::ToSpan, widgets::Paragraph};
|
use ratatui::{text::ToSpan, widgets::Paragraph};
|
||||||
|
|
||||||
use jong::game::GameState;
|
use jong::game::GameState;
|
||||||
use jong::game::wall::InWall;
|
// use jong::game::wall::InWall;
|
||||||
|
|
||||||
use crate::tui::console::ConsoleState;
|
use crate::tui::{console::ConsoleState, menu::draw_mainmenu, render::ingame::draw_ingame};
|
||||||
|
|
||||||
mod console;
|
mod console;
|
||||||
mod menu;
|
mod menu;
|
||||||
|
|
@ -54,7 +54,7 @@ impl Plugin for RiichiTui {
|
||||||
|
|
||||||
// console
|
// console
|
||||||
.init_state::<console::ConsoleState>()
|
.init_state::<console::ConsoleState>()
|
||||||
.add_systems(Update, console::draw_console.run_if(in_state(console::ConsoleState::Open)))
|
.add_systems(Update, console::draw_console.after_ignore_deferred(draw_mainmenu).after_ignore_deferred(draw_ingame).run_if(in_state(console::ConsoleState::Open)))
|
||||||
|
|
||||||
// general setup
|
// general setup
|
||||||
.init_state::<TuiState>()
|
.init_state::<TuiState>()
|
||||||
|
|
@ -67,7 +67,7 @@ impl Plugin for RiichiTui {
|
||||||
// gaming
|
// gaming
|
||||||
.init_resource::<render::hand::RenderedHand>()
|
.init_resource::<render::hand::RenderedHand>()
|
||||||
.add_systems(Update, render::ingame::draw_ingame.run_if(in_state(InGame)))
|
.add_systems(Update, render::ingame::draw_ingame.run_if(in_state(InGame)))
|
||||||
.add_systems(Update, render::hand::render_changed_hand.run_if(in_state(InGame).and(in_state(GameState::Play))))
|
// .add_systems(Update, render::hand::render_changed_hand.run_if(in_state(InGame).and(in_state(GameState::Play))))
|
||||||
|
|
||||||
// semicolon stopper
|
// semicolon stopper
|
||||||
;
|
;
|
||||||
|
|
@ -119,7 +119,7 @@ pub(crate) fn input_system(
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
},
|
},
|
||||||
GameState::Score => todo!(),
|
_ => todo!(),
|
||||||
_ => unreachable!("TuiState::InGame but GameState invalid")
|
_ => unreachable!("TuiState::InGame but GameState invalid")
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
use ratatui::widgets::Paragraph;
|
use ratatui::widgets::Paragraph;
|
||||||
|
|
||||||
use jong::game::hand::HandTiles;
|
// use jong::game::hand::HandTiles;
|
||||||
use jong::tiles::Tile;
|
use jong::tiles::Tile;
|
||||||
|
|
||||||
use crate::tui::render::tiles;
|
use crate::tui::render::tiles;
|
||||||
|
|
@ -9,24 +9,24 @@ use crate::tui::render::tiles;
|
||||||
#[derive(Resource, Default)]
|
#[derive(Resource, Default)]
|
||||||
pub(crate) struct RenderedHand(pub(crate) Vec<Vec<Paragraph<'static>>>);
|
pub(crate) struct RenderedHand(pub(crate) Vec<Vec<Paragraph<'static>>>);
|
||||||
|
|
||||||
pub(crate) fn render_changed_hand(
|
// pub(crate) fn render_changed_hand(
|
||||||
hands: Populated<&Children, Changed<HandTiles>>,
|
// hands: Populated<&Children, Changed<HandTiles>>,
|
||||||
tiles: Populated<&Tile>,
|
// tiles: Populated<&Tile>,
|
||||||
mut target: ResMut<RenderedHand>,
|
// mut target: ResMut<RenderedHand>,
|
||||||
) -> Result {
|
// ) -> Result {
|
||||||
let mut rendered = vec![];
|
// let mut rendered = vec![];
|
||||||
|
|
||||||
for hand in hands {
|
// for hand in hands {
|
||||||
let tiles = hand
|
// let tiles = hand
|
||||||
.iter()
|
// .iter()
|
||||||
.map(|inhand| tiles.get(inhand).map(tiles::draw_tile).unwrap())
|
// .map(|inhand| tiles.get(inhand).map(tiles::draw_tile).unwrap())
|
||||||
.collect();
|
// .collect();
|
||||||
|
|
||||||
rendered.push(tiles);
|
// rendered.push(tiles);
|
||||||
}
|
// }
|
||||||
|
|
||||||
target.0 = rendered;
|
// target.0 = rendered;
|
||||||
|
|
||||||
trace!("render_changed_hand");
|
// trace!("render_changed_hand");
|
||||||
Ok(())
|
// Ok(())
|
||||||
}
|
// }
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue