Things work, but there seems to be a weird performance issue

This commit is contained in:
Daniel Flanagan 2024-08-03 00:19:54 -05:00
parent 82c51427cf
commit ee2229b590
3 changed files with 71 additions and 64 deletions

View file

@ -93,10 +93,8 @@ fn main() -> AppExit {
app.add_event::<SpawnStatueEvent>();
app.add_systems(
OnEnter(View::LoadingMenu),
((startup, camera::startup), main_menu::startup),
)
app.add_systems(Startup, (startup, camera::startup));
app.add_systems(OnEnter(View::LoadingMenu), (main_menu::startup,))
.add_systems(OnExit(View::MainMenu), main_menu::exit)
.add_systems(
OnEnter(View::LoadingGame),

View file

@ -73,17 +73,22 @@ pub fn init(mut commands: Commands, assets: Res<AssetServer>) {
.insert((TileInit(task), TileChan(Mutex::new(rx))));
}
const MAX_TILES_TO_LOAD_IN_ONE_UPDATE: usize = 5_000;
pub fn tile_loaders(
mut commands: Commands,
mut query: Query<(Entity, &mut TileStorage, &TileChan), With<Tilemap>>,
mut query: Query<(Entity, &mut TileStorage), With<Tilemap>>,
mut chan: Query<&TileChan, With<Tilemap>>,
mut tasks: Query<&mut TileInit>,
mut app_state: ResMut<NextState<View>>,
) {
// TODO: to avoid locking up the universe we only want to handle a certain number per iteration (Update)
let (tilemap, mut storage, chan) = query.single_mut();
let mut tiles_done = true;
let (tilemap, mut storage) = query.single_mut();
if let Ok(chan) = chan.get_single_mut() {
let chan = chan.0.lock().unwrap();
tiles_done = false;
let mut counter = 0;
let mut tiles_done = false;
loop {
match chan.recv() {
Ok(ev) => {
@ -91,29 +96,29 @@ pub fn tile_loaders(
let tile = commands.spawn((HideFromInspector, ev)).id();
storage.set(ref_position, tile);
counter += 1;
if counter > 5_000 {
info!("Finished 100k!");
if counter >= MAX_TILES_TO_LOAD_IN_ONE_UPDATE {
info!("Finished {}!", counter);
break;
}
}
Err(err) => {
error!("{err:#?}");
Err(_) => {
commands.entity(tilemap).remove::<TileChan>();
if counter > 0 {
info!("Finished {counter}! Probably finishing mapgen soon...");
}
// the channel is likely closed, so let's note that and be done
tiles_done = true;
break;
}
}
}
}
info!("Task: {tasks:#?}");
for mut task in &mut tasks {
let poll = future::poll_once(&mut task.0);
info!("Poll: {poll:#?}");
if block_on(poll).is_some() {
// task is done, remove it so we don't poll it again
commands.entity(tilemap).remove::<TileInit>();
// need to check if the map is fully loaded before switching
// app_state.set(View::InGame);
// TODO: transition to game?
info!("poll was some");
}
}
if tasks.iter().peekable().peek().is_none() && tiles_done {

View file

@ -84,6 +84,10 @@ pub fn startup(
.spawn((
Crosshair,
Name::new("Crosshair"),
VisibilityBundle {
visibility: Visibility::Inherited,
..default()
},
TransformBundle {
local: transform,
..default()