Things work, but there seems to be a weird performance issue
This commit is contained in:
parent
82c51427cf
commit
ee2229b590
76
src/main.rs
76
src/main.rs
|
@ -93,46 +93,44 @@ fn main() -> AppExit {
|
|||
|
||||
app.add_event::<SpawnStatueEvent>();
|
||||
|
||||
app.add_systems(
|
||||
OnEnter(View::LoadingMenu),
|
||||
((startup, camera::startup), main_menu::startup),
|
||||
)
|
||||
.add_systems(OnExit(View::MainMenu), main_menu::exit)
|
||||
.add_systems(
|
||||
OnEnter(View::LoadingGame),
|
||||
(player::startup, statue::startup, map::init),
|
||||
)
|
||||
.add_systems(
|
||||
OnExit(View::InGame),
|
||||
(player::exit, statue::exit, map::exit),
|
||||
)
|
||||
.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),
|
||||
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),
|
||||
(player::startup, statue::startup, map::init),
|
||||
)
|
||||
.add_systems(
|
||||
OnExit(View::InGame),
|
||||
(player::exit, statue::exit, map::exit),
|
||||
)
|
||||
.add_systems(
|
||||
Update,
|
||||
(
|
||||
// map::update,
|
||||
player::control,
|
||||
player::meow_on_r,
|
||||
player::player_debug_info,
|
||||
statue::spawn_statue,
|
||||
movement::update_velocity_by_heading,
|
||||
movement::resolve_velocity.after(movement::update_velocity_by_heading),
|
||||
movement::ysort.after(movement::update_velocity_by_heading),
|
||||
camera::update
|
||||
.after(player::control)
|
||||
.after(movement::resolve_velocity),
|
||||
)
|
||||
.after(input::process_input)
|
||||
.in_set(InGameSet),
|
||||
(main_menu::update).in_set(MainMenuSet),
|
||||
),
|
||||
)
|
||||
.insert_resource(ClearColor(Color::srgb(0.3, 0.1, 0.5)));
|
||||
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,
|
||||
player::control,
|
||||
player::meow_on_r,
|
||||
player::player_debug_info,
|
||||
statue::spawn_statue,
|
||||
movement::update_velocity_by_heading,
|
||||
movement::resolve_velocity.after(movement::update_velocity_by_heading),
|
||||
movement::ysort.after(movement::update_velocity_by_heading),
|
||||
camera::update
|
||||
.after(player::control)
|
||||
.after(movement::resolve_velocity),
|
||||
)
|
||||
.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()
|
||||
}
|
||||
|
||||
|
|
55
src/map.rs
55
src/map.rs
|
@ -73,47 +73,52 @@ 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 chan = chan.0.lock().unwrap();
|
||||
let mut counter = 0;
|
||||
let mut tiles_done = false;
|
||||
loop {
|
||||
match chan.recv() {
|
||||
Ok(ev) => {
|
||||
let ref_position = &ev.position;
|
||||
let tile = commands.spawn((HideFromInspector, ev)).id();
|
||||
storage.set(ref_position, tile);
|
||||
counter += 1;
|
||||
if counter > 5_000 {
|
||||
info!("Finished 100k!");
|
||||
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;
|
||||
loop {
|
||||
match chan.recv() {
|
||||
Ok(ev) => {
|
||||
let ref_position = &ev.position;
|
||||
let tile = commands.spawn((HideFromInspector, ev)).id();
|
||||
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;
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
error!("{err:#?}");
|
||||
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 {
|
||||
|
|
|
@ -84,6 +84,10 @@ pub fn startup(
|
|||
.spawn((
|
||||
Crosshair,
|
||||
Name::new("Crosshair"),
|
||||
VisibilityBundle {
|
||||
visibility: Visibility::Inherited,
|
||||
..default()
|
||||
},
|
||||
TransformBundle {
|
||||
local: transform,
|
||||
..default()
|
||||
|
|
Loading…
Reference in a new issue