refactor entites to reduce complexity and crashes, switch to Querys
This commit is contained in:
parent
3182916832
commit
c7200b1fd3
8 changed files with 95 additions and 89 deletions
|
|
@ -5,14 +5,14 @@ use crate::{
|
|||
EnumNextCycle,
|
||||
game::{
|
||||
GameMessage, GameState,
|
||||
hand::{DiscardedTile, DrawnTile, Hand},
|
||||
player::Player,
|
||||
hand::{Discarded, Drawn, Hand},
|
||||
player::{CurrentPlayer, Player},
|
||||
wall::Wall,
|
||||
},
|
||||
};
|
||||
|
||||
#[derive(Resource)]
|
||||
pub struct CurrentPlayer(pub Entity);
|
||||
// #[derive(Resource)]
|
||||
// pub struct CurrentPlayer(pub Entity);
|
||||
|
||||
#[derive(Resource)]
|
||||
pub(crate) struct MatchSettings {
|
||||
|
|
@ -58,9 +58,6 @@ pub(crate) enum TurnState {
|
|||
End,
|
||||
}
|
||||
|
||||
#[derive(EntityEvent)]
|
||||
pub struct Discard(pub Entity);
|
||||
|
||||
impl Default for MatchSettings {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
|
|
@ -116,22 +113,22 @@ impl EnumNextCycle for TurnState {
|
|||
|
||||
pub(crate) fn tsumo(
|
||||
mut commands: Commands,
|
||||
curr_player: Res<CurrentPlayer>,
|
||||
// players: Populated<Entity, With<Player>>,
|
||||
wall_ent: Single<Entity, With<Wall>>,
|
||||
|
||||
// curr_player: Res<CurrentPlayer>,
|
||||
curr_player: Single<Entity, With<CurrentPlayer>>,
|
||||
wall: Single<Entity, With<Wall>>,
|
||||
walltiles: Single<&Children, With<Wall>>,
|
||||
|
||||
curr_turnstate: Res<State<TurnState>>,
|
||||
mut next_turnstate: ResMut<NextState<TurnState>>,
|
||||
) {
|
||||
debug!("tsumo for: {:?}", curr_player.0);
|
||||
|
||||
let drawn = walltiles.last().unwrap();
|
||||
commands.entity(*wall_ent).remove_child(*drawn);
|
||||
let drawn_ent = commands.spawn(DrawnTile(*drawn)).id();
|
||||
commands.entity(curr_player.0).add_child(drawn_ent);
|
||||
commands.entity(*wall).remove_child(*drawn);
|
||||
|
||||
debug!("drew: ent: {drawn_ent:?} tile_ent: {:?}", drawn);
|
||||
let drawn = commands.entity(*drawn).insert(Drawn).id();
|
||||
commands.entity(*curr_player).add_child(drawn);
|
||||
|
||||
debug!("tsumo for: {:?}, tile: {:?}", *curr_player, drawn);
|
||||
next_turnstate.set(curr_turnstate.next());
|
||||
}
|
||||
|
||||
|
|
@ -156,39 +153,43 @@ pub(crate) fn discard(
|
|||
mut commands: Commands,
|
||||
mut reader: MessageReader<GameMessage>,
|
||||
|
||||
currplayer: Res<CurrentPlayer>,
|
||||
drawntile: Single<(&DrawnTile, Entity), With<DrawnTile>>,
|
||||
player_hands: Populated<(&Player, &Children), With<Hand>>,
|
||||
hands: Populated<&Children, (With<Hand>, Without<Player>)>,
|
||||
curr_player: Single<Entity, With<CurrentPlayer>>,
|
||||
players: Query<&Children, With<Player>>,
|
||||
mut hands: Query<(&Children, Entity), (With<Hand>, Without<Player>)>,
|
||||
drawn: Single<Entity, With<Drawn>>,
|
||||
|
||||
curr_turnstate: Res<State<TurnState>>,
|
||||
mut next_turnstate: ResMut<NextState<TurnState>>,
|
||||
) {
|
||||
let curr = currplayer.0;
|
||||
let hand = player_hands.get(curr).unwrap().1.iter().next().unwrap();
|
||||
let handtiles = hands.get(hand).unwrap();
|
||||
let (drawntile, drawn_ent) = *drawntile;
|
||||
|
||||
// debug!("discard turn for: {curr:?}");
|
||||
) -> Result {
|
||||
// trace!("discard");
|
||||
let curr_hand = hands.get_mut(players.get(*curr_player)?.iter().next().unwrap())?;
|
||||
|
||||
let mut done = false;
|
||||
while let Some(message) = reader.read().next() {
|
||||
if let GameMessage::Discarded(entity) = message {
|
||||
debug!("{curr:?} discarded: {entity:?}");
|
||||
// commands.entity(drawn_ent).despawn();
|
||||
if *entity == drawntile.0 {
|
||||
} else if handtiles.contains(entity) {
|
||||
if let GameMessage::Discarded(discarded) = message {
|
||||
if *discarded == *drawn {
|
||||
} else if curr_hand.0.contains(discarded) {
|
||||
commands
|
||||
.entity(hand)
|
||||
.remove_child(*entity)
|
||||
.add_child(drawntile.0);
|
||||
.entity(curr_hand.1)
|
||||
.remove_child(*discarded)
|
||||
.add_child(*drawn);
|
||||
} else {
|
||||
panic!("discarded illegal player tile?")
|
||||
panic!("current hand nor drawn tile contains discarded tile")
|
||||
}
|
||||
commands.spawn(DiscardedTile(*entity));
|
||||
next_turnstate.set(curr_turnstate.next());
|
||||
commands
|
||||
.entity(*discarded)
|
||||
.remove::<Drawn>()
|
||||
.insert(Discarded);
|
||||
|
||||
done = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if done {
|
||||
next_turnstate.set(curr_turnstate.next());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn ron_chi_pon_kan(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue