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_event::<SpawnStatueEvent>();
app.add_systems( app.add_systems(Startup, (startup, camera::startup));
OnEnter(View::LoadingMenu), app.add_systems(OnEnter(View::LoadingMenu), (main_menu::startup,))
((startup, camera::startup), main_menu::startup),
)
.add_systems(OnExit(View::MainMenu), main_menu::exit) .add_systems(OnExit(View::MainMenu), main_menu::exit)
.add_systems( .add_systems(
OnEnter(View::LoadingGame), 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)))); .insert((TileInit(task), TileChan(Mutex::new(rx))));
} }
const MAX_TILES_TO_LOAD_IN_ONE_UPDATE: usize = 5_000;
pub fn tile_loaders( pub fn tile_loaders(
mut commands: Commands, 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 tasks: Query<&mut TileInit>,
mut app_state: ResMut<NextState<View>>, mut app_state: ResMut<NextState<View>>,
) { ) {
// TODO: to avoid locking up the universe we only want to handle a certain number per iteration (Update) // 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(); let chan = chan.0.lock().unwrap();
tiles_done = false;
let mut counter = 0; let mut counter = 0;
let mut tiles_done = false;
loop { loop {
match chan.recv() { match chan.recv() {
Ok(ev) => { Ok(ev) => {
@ -91,29 +96,29 @@ pub fn tile_loaders(
let tile = commands.spawn((HideFromInspector, ev)).id(); let tile = commands.spawn((HideFromInspector, ev)).id();
storage.set(ref_position, tile); storage.set(ref_position, tile);
counter += 1; counter += 1;
if counter > 5_000 { if counter >= MAX_TILES_TO_LOAD_IN_ONE_UPDATE {
info!("Finished 100k!"); info!("Finished {}!", counter);
break; break;
} }
} }
Err(err) => { Err(_) => {
error!("{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; tiles_done = true;
break; break;
} }
} }
} }
}
info!("Task: {tasks:#?}");
for mut task in &mut tasks { for mut task in &mut tasks {
let poll = future::poll_once(&mut task.0); let poll = future::poll_once(&mut task.0);
info!("Poll: {poll:#?}");
if block_on(poll).is_some() { if block_on(poll).is_some() {
// task is done, remove it so we don't poll it again
commands.entity(tilemap).remove::<TileInit>(); 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 { if tasks.iter().peekable().peek().is_none() && tiles_done {

View file

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