Revert "WIP exclusive system?"

This reverts commit 692fbed157.
This commit is contained in:
Daniel Flanagan 2024-08-04 20:27:21 -05:00
parent 155a118bf0
commit 56e5524466

View file

@ -1,6 +1,5 @@
use crate::View; use crate::View;
use crate::{inspector::HideFromInspector, prelude::*}; use crate::{inspector::HideFromInspector, prelude::*};
use bevy::ecs::system::SystemState;
use bevy::tasks::{block_on, futures_lite::future, AsyncComputeTaskPool, Task}; use bevy::tasks::{block_on, futures_lite::future, AsyncComputeTaskPool, Task};
use bevy_ecs_tilemap::prelude::*; use bevy_ecs_tilemap::prelude::*;
use rand::prelude::*; use rand::prelude::*;
@ -76,33 +75,16 @@ pub fn init(mut commands: Commands, assets: Res<AssetServer>) {
const MAX_TILES_TO_LOAD_IN_ONE_UPDATE: usize = 5_000; const MAX_TILES_TO_LOAD_IN_ONE_UPDATE: usize = 5_000;
pub fn tile_loaders( pub fn tile_loaders(
world: &mut World, mut commands: Commands,
params: &mut SystemState<( mut query: Query<(Entity, &mut TileStorage), With<Tilemap>>,
Query<(Entity, &mut TileStorage), With<Tilemap>>, mut chan: Query<&TileChan, With<Tilemap>>,
Query<&TileChan, With<Tilemap>>, mut tasks: Query<&mut TileInit>,
Query<&mut TileInit>, mut app_state: ResMut<NextState<View>>,
ResMut<NextState<View>>,
)>,
) { ) {
let (mut query, mut chan, mut tasks, mut next_view) = params.get_mut(world); // TODO: to avoid locking up the universe we only want to handle a certain number per iteration (Update)
let mut tiles_done = true; let mut tiles_done = true;
let (tilemap, mut storage) = query.single_mut(); let (tilemap, mut storage) = query.single_mut();
let mut tasks_iter = tasks.iter_mut().peekable();
let mut remove_tasks = false;
if tasks_iter.peek().is_none() && tiles_done {
next_view.set(View::InGame)
} else {
for mut task in tasks_iter {
let poll = future::poll_once(&mut task.0);
if block_on(poll).is_some() {
remove_tasks = true;
// task is done, remove it so we don't poll it again
}
}
}
let tile_bundles: Vec<TileBundle> = vec![];
if let Ok(chan) = chan.get_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; tiles_done = false;
@ -111,7 +93,7 @@ pub fn tile_loaders(
match chan.recv() { match chan.recv() {
Ok(ev) => { Ok(ev) => {
let ref_position = &ev.position; let ref_position = &ev.position;
let tile = world.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 >= MAX_TILES_TO_LOAD_IN_ONE_UPDATE { if counter >= MAX_TILES_TO_LOAD_IN_ONE_UPDATE {
@ -120,7 +102,7 @@ pub fn tile_loaders(
} }
} }
Err(_) => { Err(_) => {
world.entity_mut(tilemap).remove::<TileChan>(); commands.entity(tilemap).remove::<TileChan>();
if counter > 0 { if counter > 0 {
info!("Finished {counter}! Probably finishing mapgen soon..."); info!("Finished {counter}! Probably finishing mapgen soon...");
} }
@ -132,15 +114,16 @@ pub fn tile_loaders(
} }
} }
if remove_tasks { for mut task in &mut tasks {
world.entity_mut(tilemap).remove::<TileInit>(); let poll = future::poll_once(&mut task.0);
if block_on(poll).is_some() {
// task is done, remove it so we don't poll it again
commands.entity(tilemap).remove::<TileInit>();
}
}
if tasks.iter().peekable().peek().is_none() && tiles_done {
app_state.set(View::InGame);
} }
world.spawn_batch(
tile_bundles
.into_iter()
.map(|t| (HideFromInspector, t))
.into_iter(),
);
} }
pub fn exit(mut commands: Commands, q: Query<Entity, With<Tilemap>>) { pub fn exit(mut commands: Commands, q: Query<Entity, With<Tilemap>>) {