(wip) crash in render_hand, hand doesn't seem to be spawned
This commit is contained in:
parent
e2d554f321
commit
b512e38b26
3 changed files with 65 additions and 57 deletions
|
|
@ -1,15 +1,14 @@
|
||||||
use bevy::platform::collections::HashMap;
|
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
use bevy_spacetimedb::{
|
use bevy_spacetimedb::{
|
||||||
ReadInsertUpdateMessage, ReadStdbConnectedMessage, ReadStdbDisconnectedMessage,
|
ReadInsertUpdateMessage, ReadStdbConnectedMessage, ReadStdbDisconnectedMessage,
|
||||||
ReadUpdateMessage, StdbPlugin,
|
ReadUpdateMessage, StdbPlugin,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use jong_db::{self, add_bot, set_ready};
|
||||||
use jong_db::{
|
use jong_db::{
|
||||||
self, BotTableAccess, DbConnection, LobbyTableAccess, PlayerHand, PlayerTableAccess,
|
BotTableAccess, DbConnection, LobbyTableAccess, PlayerHand, PlayerTableAccess, RemoteTables,
|
||||||
RemoteTables, ViewClosedHandsTableAccess, ViewHandTableAccess as _,
|
ViewClosedHandsTableAccess, ViewHandTableAccess,
|
||||||
};
|
};
|
||||||
use jong_db::{add_bot, set_ready};
|
|
||||||
use jong_types::*;
|
use jong_types::*;
|
||||||
use spacetimedb_sdk::Table;
|
use spacetimedb_sdk::Table;
|
||||||
|
|
||||||
|
|
@ -46,8 +45,7 @@ impl Plugin for Riichi {
|
||||||
.add_systems(Startup, subscriptions)
|
.add_systems(Startup, subscriptions)
|
||||||
.add_observer(on_subscribed)
|
.add_observer(on_subscribed)
|
||||||
.add_systems(Update, (on_connect, on_disconnect))
|
.add_systems(Update, (on_connect, on_disconnect))
|
||||||
.add_systems(Update, on_lobby_insert_update)
|
.add_systems(Update, (on_lobby_insert_update, on_player_insert_update))
|
||||||
.add_systems(Update, on_player_insert_update)
|
|
||||||
.add_systems(
|
.add_systems(
|
||||||
Update,
|
Update,
|
||||||
(on_view_hand_update)
|
(on_view_hand_update)
|
||||||
|
|
@ -56,7 +54,7 @@ impl Plugin for Riichi {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn on_connect(stdb: SpacetimeDB, mut messages: ReadStdbConnectedMessage, _commands: Commands) {
|
fn on_connect(stdb: SpacetimeDB, mut messages: ReadStdbConnectedMessage) {
|
||||||
for msg in messages.read() {
|
for msg in messages.read() {
|
||||||
info!("you're now jongline");
|
info!("you're now jongline");
|
||||||
|
|
||||||
|
|
@ -139,10 +137,13 @@ fn on_subscribed(
|
||||||
|
|
||||||
fn spawn_main_player(
|
fn spawn_main_player(
|
||||||
stdb: &SpacetimeDB,
|
stdb: &SpacetimeDB,
|
||||||
|
|
||||||
commands: &mut Commands,
|
commands: &mut Commands,
|
||||||
next_turnstate: &mut ResMut<NextState<TurnState>>,
|
next_turnstate: &mut ResMut<NextState<TurnState>>,
|
||||||
|
|
||||||
player: &jong_db::Player,
|
player: &jong_db::Player,
|
||||||
) {
|
) {
|
||||||
|
trace!("spawn_main_player");
|
||||||
let main_player = commands
|
let main_player = commands
|
||||||
.spawn((
|
.spawn((
|
||||||
Player {
|
Player {
|
||||||
|
|
@ -152,6 +153,17 @@ fn spawn_main_player(
|
||||||
))
|
))
|
||||||
.id();
|
.id();
|
||||||
if let Some(player_hand) = stdb.db().view_hand().iter().next() {
|
if let Some(player_hand) = stdb.db().view_hand().iter().next() {
|
||||||
|
spawn_main_hand(commands, next_turnstate, main_player, &player_hand);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn spawn_main_hand(
|
||||||
|
commands: &mut Commands,
|
||||||
|
next_turnstate: &mut ResMut<NextState<TurnState>>,
|
||||||
|
main_player: Entity,
|
||||||
|
|
||||||
|
player_hand: &PlayerHand,
|
||||||
|
) {
|
||||||
let hand_tiles: Vec<_> = player_hand
|
let hand_tiles: Vec<_> = player_hand
|
||||||
.hand
|
.hand
|
||||||
.iter()
|
.iter()
|
||||||
|
|
@ -164,10 +176,12 @@ fn spawn_main_player(
|
||||||
.collect();
|
.collect();
|
||||||
let pond = commands.spawn(Hand).add_children(&pond_tiles).id();
|
let pond = commands.spawn(Hand).add_children(&pond_tiles).id();
|
||||||
let hand = commands.spawn(Pond).add_children(&hand_tiles).id();
|
let hand = commands.spawn(Pond).add_children(&hand_tiles).id();
|
||||||
commands.entity(main_player).add_children(&[pond, hand]);
|
let children = commands.entity(main_player).add_children(&[pond, hand]).id();
|
||||||
|
|
||||||
|
debug!("main_hand: {:?}\n main_pond: {:?}, children: {:?}", hand_tiles, pond_tiles, children);
|
||||||
|
|
||||||
if player_hand.turn_state == jong_db::TurnState::Tsumo
|
if player_hand.turn_state == jong_db::TurnState::Tsumo
|
||||||
&& let Some(drawn_dbt) = player_hand.working_tile
|
&& let Some(drawn_dbt) = &player_hand.working_tile
|
||||||
{
|
{
|
||||||
let drawn = commands
|
let drawn = commands
|
||||||
.spawn((Drawn, Tile::from(&drawn_dbt.tile), TileId(drawn_dbt.id)))
|
.spawn((Drawn, Tile::from(&drawn_dbt.tile), TileId(drawn_dbt.id)))
|
||||||
|
|
@ -175,7 +189,6 @@ fn spawn_main_player(
|
||||||
commands.entity(main_player).add_child(drawn);
|
commands.entity(main_player).add_child(drawn);
|
||||||
}
|
}
|
||||||
next_turnstate.set(player_hand.turn_state.into());
|
next_turnstate.set(player_hand.turn_state.into());
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn spawn_other_player(stdb: &SpacetimeDB, commands: &mut Commands, player: &jong_db::Player) {
|
fn spawn_other_player(stdb: &SpacetimeDB, commands: &mut Commands, player: &jong_db::Player) {
|
||||||
|
|
@ -204,6 +217,7 @@ fn on_player_insert_update(
|
||||||
) {
|
) {
|
||||||
for msg in messages.read() {
|
for msg in messages.read() {
|
||||||
if main_player.is_none() && msg.new.identity == stdb.identity() {
|
if main_player.is_none() && msg.new.identity == stdb.identity() {
|
||||||
|
trace!("spawn_main_player");
|
||||||
spawn_main_player(&stdb, &mut commands, &mut next_turnstate, &msg.new);
|
spawn_main_player(&stdb, &mut commands, &mut next_turnstate, &msg.new);
|
||||||
} else if other_players.iter().any(|p| {
|
} else if other_players.iter().any(|p| {
|
||||||
if let PlayerOrBot::Player { id } = &p.id {
|
if let PlayerOrBot::Player { id } = &p.id {
|
||||||
|
|
@ -275,19 +289,23 @@ fn on_view_hand_update(
|
||||||
mut commands: Commands,
|
mut commands: Commands,
|
||||||
tiles: Query<(Entity, &TileId)>,
|
tiles: Query<(Entity, &TileId)>,
|
||||||
|
|
||||||
main_player: Single<&Children, With<MainPlayer>>,
|
main_player: Single<(Entity, Option<&Children>), With<MainPlayer>>,
|
||||||
|
|
||||||
hand: Query<Entity, With<Hand>>,
|
hand: Query<Entity, With<Hand>>,
|
||||||
pond: Query<Entity, With<Pond>>,
|
pond: Query<Entity, With<Pond>>,
|
||||||
// drawn: Option<Single<Entity, With<Drawn>>>,
|
// drawn: Option<Single<Entity, With<Drawn>>>,
|
||||||
mut next_turnstate: ResMut<NextState<jong_types::states::TurnState>>,
|
mut next_turnstate: ResMut<NextState<jong_types::states::TurnState>>,
|
||||||
) {
|
) {
|
||||||
trace!("on_view_hand_update");
|
|
||||||
|
|
||||||
// TODO can this and similar run at startup or on play/reconnect?
|
// TODO can this and similar run at startup or on play/reconnect?
|
||||||
for msg in messages.read() {
|
for msg in messages.read() {
|
||||||
trace!("new hand: {:?}", msg.new);
|
trace!("new hand: {:?}", msg.new);
|
||||||
|
|
||||||
|
if main_player.1.is_none() {
|
||||||
|
trace!("spawn_main_hand, {:?}", *main_player);
|
||||||
|
spawn_main_hand(&mut commands, &mut next_turnstate, main_player.0, &msg.new);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
let hand_tiles: Vec<_> = msg
|
let hand_tiles: Vec<_> = msg
|
||||||
.new
|
.new
|
||||||
.hand
|
.hand
|
||||||
|
|
@ -299,10 +317,6 @@ fn on_view_hand_update(
|
||||||
.unwrap_or_else(|| commands.spawn((Tile::from(&dbt.tile), TileId(dbt.id))).id())
|
.unwrap_or_else(|| commands.spawn((Tile::from(&dbt.tile), TileId(dbt.id))).id())
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
commands
|
|
||||||
.entity(hand.iter().find(|e| main_player.contains(e)).unwrap())
|
|
||||||
.replace_children(&hand_tiles);
|
|
||||||
|
|
||||||
let pond_tiles: Vec<_> = msg
|
let pond_tiles: Vec<_> = msg
|
||||||
.new
|
.new
|
||||||
.pond
|
.pond
|
||||||
|
|
@ -314,8 +328,20 @@ fn on_view_hand_update(
|
||||||
.unwrap_or_else(|| commands.spawn((Tile::from(&dbt.tile), TileId(dbt.id))).id())
|
.unwrap_or_else(|| commands.spawn((Tile::from(&dbt.tile), TileId(dbt.id))).id())
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
commands
|
commands
|
||||||
.entity(pond.iter().find(|e| main_player.contains(e)).unwrap())
|
.entity(
|
||||||
|
hand.iter()
|
||||||
|
.find(|e| main_player.1.is_some_and(|mp| mp.contains(e)))
|
||||||
|
.unwrap(),
|
||||||
|
)
|
||||||
|
.replace_children(&hand_tiles);
|
||||||
|
commands
|
||||||
|
.entity(
|
||||||
|
pond.iter()
|
||||||
|
.find(|e| main_player.1.is_some_and(|mp| mp.contains(e)))
|
||||||
|
.unwrap(),
|
||||||
|
)
|
||||||
.replace_children(&pond_tiles);
|
.replace_children(&pond_tiles);
|
||||||
|
|
||||||
match msg.new.turn_state {
|
match msg.new.turn_state {
|
||||||
|
|
|
||||||
|
|
@ -88,7 +88,6 @@ impl Plugin for TuiPlugin {
|
||||||
(render::render_main_hand, render::render_main_pond)
|
(render::render_main_hand, render::render_main_pond)
|
||||||
.run_if(in_state(GameState::Play)),
|
.run_if(in_state(GameState::Play)),
|
||||||
render::render,
|
render::render,
|
||||||
query_tester,
|
|
||||||
)
|
)
|
||||||
.chain()
|
.chain()
|
||||||
.in_set(TuiSet::Render),
|
.in_set(TuiSet::Render),
|
||||||
|
|
@ -96,24 +95,6 @@ impl Plugin for TuiPlugin {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn query_tester(
|
|
||||||
main_player: Single<&Children, With<MainPlayer>>,
|
|
||||||
// hand: Query<(&Children, Entity), With<Hand>>,
|
|
||||||
) {
|
|
||||||
trace!("owo");
|
|
||||||
// let hand = hand
|
|
||||||
// .iter()
|
|
||||||
// .find_map(|(c, e)| {
|
|
||||||
// if main_player.contains(&e) {
|
|
||||||
// Some(c)
|
|
||||||
// } else {
|
|
||||||
// None
|
|
||||||
// }
|
|
||||||
// })
|
|
||||||
// .unwrap();
|
|
||||||
// debug!("{hand:?}");
|
|
||||||
}
|
|
||||||
|
|
||||||
fn discard_tile(
|
fn discard_tile(
|
||||||
stdb: SpacetimeDB,
|
stdb: SpacetimeDB,
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -115,6 +115,7 @@ pub(crate) fn render_main_hand(
|
||||||
let hand: Vec<_> = hand
|
let hand: Vec<_> = hand
|
||||||
.iter()
|
.iter()
|
||||||
.find_map(|(c, e)| {
|
.find_map(|(c, e)| {
|
||||||
|
debug!("main_player children: {:?}", *main_player);
|
||||||
if main_player.contains(&e) {
|
if main_player.contains(&e) {
|
||||||
Some(c)
|
Some(c)
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue