WIP exclusive system?
This commit is contained in:
parent
ee2229b590
commit
692fbed157
1 changed files with 34 additions and 17 deletions
51
src/map.rs
51
src/map.rs
|
@ -1,5 +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::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::*;
|
||||||
|
@ -75,16 +76,33 @@ 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(
|
||||||
mut commands: Commands,
|
world: &mut World,
|
||||||
mut query: Query<(Entity, &mut TileStorage), With<Tilemap>>,
|
params: &mut SystemState<(
|
||||||
mut chan: Query<&TileChan, With<Tilemap>>,
|
Query<(Entity, &mut TileStorage), With<Tilemap>>,
|
||||||
mut tasks: Query<&mut TileInit>,
|
Query<&TileChan, With<Tilemap>>,
|
||||||
mut app_state: ResMut<NextState<View>>,
|
Query<&mut TileInit>,
|
||||||
|
ResMut<NextState<View>>,
|
||||||
|
)>,
|
||||||
) {
|
) {
|
||||||
// TODO: to avoid locking up the universe we only want to handle a certain number per iteration (Update)
|
let (mut query, mut chan, mut tasks, mut next_view) = params.get_mut(world);
|
||||||
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;
|
||||||
|
@ -93,7 +111,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 = commands.spawn((HideFromInspector, ev)).id();
|
let tile = world.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 {
|
||||||
|
@ -102,7 +120,7 @@ pub fn tile_loaders(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
commands.entity(tilemap).remove::<TileChan>();
|
world.entity_mut(tilemap).remove::<TileChan>();
|
||||||
if counter > 0 {
|
if counter > 0 {
|
||||||
info!("Finished {counter}! Probably finishing mapgen soon...");
|
info!("Finished {counter}! Probably finishing mapgen soon...");
|
||||||
}
|
}
|
||||||
|
@ -114,16 +132,15 @@ pub fn tile_loaders(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for mut task in &mut tasks {
|
if remove_tasks {
|
||||||
let poll = future::poll_once(&mut task.0);
|
world.entity_mut(tilemap).remove::<TileInit>();
|
||||||
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>>) {
|
||||||
|
|
Loading…
Reference in a new issue