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,46 +93,44 @@ 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(
.add_systems(OnExit(View::MainMenu), main_menu::exit) OnEnter(View::LoadingGame),
.add_systems( (player::startup, statue::startup, map::init),
OnEnter(View::LoadingGame), )
(player::startup, statue::startup, map::init), .add_systems(
) OnExit(View::InGame),
.add_systems( (player::exit, statue::exit, map::exit),
OnExit(View::InGame), )
(player::exit, statue::exit, map::exit), .add_systems(
) Update,
.add_systems(
Update,
(
load_menu.run_if(in_state(View::LoadingMenu)),
load_game.run_if(in_state(View::LoadingGame)),
map::tile_loaders.run_if(in_state(View::LoadingGame)),
input::process_input,
(update,).after(input::process_input),
( (
// map::update, load_menu.run_if(in_state(View::LoadingMenu)),
player::control, load_game.run_if(in_state(View::LoadingGame)),
player::meow_on_r, map::tile_loaders.run_if(in_state(View::LoadingGame)),
player::player_debug_info, input::process_input,
statue::spawn_statue, (update,).after(input::process_input),
movement::update_velocity_by_heading, (
movement::resolve_velocity.after(movement::update_velocity_by_heading), // map::update,
movement::ysort.after(movement::update_velocity_by_heading), player::control,
camera::update player::meow_on_r,
.after(player::control) player::player_debug_info,
.after(movement::resolve_velocity), statue::spawn_statue,
) movement::update_velocity_by_heading,
.after(input::process_input) movement::resolve_velocity.after(movement::update_velocity_by_heading),
.in_set(InGameSet), movement::ysort.after(movement::update_velocity_by_heading),
(main_menu::update).in_set(MainMenuSet), camera::update
), .after(player::control)
) .after(movement::resolve_velocity),
.insert_resource(ClearColor(Color::srgb(0.3, 0.1, 0.5))); )
.after(input::process_input)
.in_set(InGameSet),
(main_menu::update).in_set(MainMenuSet),
),
)
.insert_resource(ClearColor(Color::srgb(0.3, 0.1, 0.5)));
app.run() app.run()
} }

View file

@ -73,47 +73,52 @@ 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 chan = chan.0.lock().unwrap(); let (tilemap, mut storage) = query.single_mut();
let mut counter = 0;
let mut tiles_done = false; if let Ok(chan) = chan.get_single_mut() {
loop { let chan = chan.0.lock().unwrap();
match chan.recv() { tiles_done = false;
Ok(ev) => { let mut counter = 0;
let ref_position = &ev.position; loop {
let tile = commands.spawn((HideFromInspector, ev)).id(); match chan.recv() {
storage.set(ref_position, tile); Ok(ev) => {
counter += 1; let ref_position = &ev.position;
if counter > 5_000 { let tile = commands.spawn((HideFromInspector, ev)).id();
info!("Finished 100k!"); storage.set(ref_position, tile);
counter += 1;
if counter >= MAX_TILES_TO_LOAD_IN_ONE_UPDATE {
info!("Finished {}!", counter);
break;
}
}
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; break;
} }
} }
Err(err) => {
error!("{err:#?}");
tiles_done = true;
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()