Revert "WIP exclusive system, but I can't seem to make it work nicely"

This reverts commit 5d846fdeff.
This commit is contained in:
Daniel Flanagan 2024-08-04 20:26:59 -05:00
parent 5d846fdeff
commit 155a118bf0

View file

@ -1,7 +1,6 @@
use crate::View; use crate::View;
use crate::{inspector::HideFromInspector, prelude::*}; use crate::{inspector::HideFromInspector, prelude::*};
use bevy::ecs::system::SystemState; use bevy::ecs::system::SystemState;
use bevy::ecs::world::CommandQueue;
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::*;
@ -17,7 +16,7 @@ pub struct Tilemap;
pub struct TileChan(Mutex<Receiver<TileBundle>>); pub struct TileChan(Mutex<Receiver<TileBundle>>);
#[derive(Component, Debug)] #[derive(Component, Debug)]
pub struct TileLoaderTask(Task<()>); pub struct TileInit(Task<()>);
pub fn init(mut commands: Commands, assets: Res<AssetServer>) { pub fn init(mut commands: Commands, assets: Res<AssetServer>) {
// TODO: I'm pretty determined to not have this sieze up the game despite the large number of entities being added. Should work with a "loading" screen and doing this in the background? // TODO: I'm pretty determined to not have this sieze up the game despite the large number of entities being added. Should work with a "loading" screen and doing this in the background?
@ -72,93 +71,76 @@ pub fn init(mut commands: Commands, assets: Res<AssetServer>) {
commands commands
.entity(tilemap) .entity(tilemap)
.insert((TileLoaderTask(task), TileChan(Mutex::new(rx)))); .insert((TileInit(task), TileChan(Mutex::new(rx))));
} }
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, world: &mut World,
tilemap: QueryState<Entity, With<Tilemap>>, params: &mut SystemState<(
storage: &mut QueryState<&mut TileStorage, With<Tilemap>>, Query<(Entity, &mut TileStorage), With<Tilemap>>,
chan: QueryState<&TileChan, With<Tilemap>>, Query<&TileChan, With<Tilemap>>,
task: QueryState<&mut TileLoaderTask, With<Tilemap>>, Query<&mut TileInit>,
params: &mut SystemState<ResMut<NextState<View>>>, ResMut<NextState<View>>,
)>,
) { ) {
// TODO: this state should maybe be converted to a resource or something? let (mut query, mut chan, mut tasks, mut next_view) = params.get_mut(world);
let mut tiles_done = false; let mut tiles_done = true;
let mut task_done = false; let (tilemap, mut storage) = query.single_mut();
type AddingTile = (HideFromInspector, TileBundle);
type AddedTile<'a> = (Entity, &'a TilePos);
let mut tiles_to_add: Vec<AddingTile> = Vec::with_capacity(MAX_TILES_TO_LOAD_IN_ONE_UPDATE);
let mut added_tiles: Vec<AddedTile> = Vec::with_capacity(MAX_TILES_TO_LOAD_IN_ONE_UPDATE);
// let mut task = task; let mut tasks_iter = tasks.iter_mut().peekable();
let mut remove_tasks = false;
// { if tasks_iter.peek().is_none() && tiles_done {
// task_done = true;
// let mut task = task_query.get_single();
// if let Ok(task) = task {
// let poll = future::poll_once(task.0);
// if block_on(poll).is_some() {
// task_done = true;
// // task is done, remove it so we don't poll it again
// world.entity_mut(tilemap).remove::<TileLoaderTask>();
// }
// } else {
// next_view.set(View::InGame)
// }
// let (tilemap, mut storage) = entity_query.single_mut();
// let tiles: Vec<TileBundle> = vec![];
// if let Ok(channel) = channel_query.get_single_mut() {
// let channel = channel.0.lock().unwrap();
// tiles_done = false;
// let mut counter = 0;
// loop {
// match channel.recv() {
// Ok(tile) => {
// let position = &tile.position;
// let tile = world.spawn((HideFromInspector, tile)).id();
// storage.set(position, tile);
// counter += 1;
// if counter >= MAX_TILES_TO_LOAD_IN_ONE_UPDATE {
// info!("Finished {}!", counter);
// break;
// }
// }
// Err(_) => {
// world.entity_mut(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;
// }
// }
// }
// }
for tile in tiles_to_add {
added_tiles.push((id, &tile.1.position));
let id = world.spawn(tile).id();
}
// world.spawn_batch(
// tiles
// .into_iter()
// .map(|t| (HideFromInspector, t))
// .into_iter(),
// );
{
let mut storage = storage.single_mut(world);
// storage.set(&
let mut next_view = params.get_mut(world);
if task_done && tiles_done {
next_view.set(View::InGame) 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() {
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 = world.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(_) => {
world.entity_mut(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;
}
}
}
}
if remove_tasks {
world.entity_mut(tilemap).remove::<TileInit>();
}
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>>) {