Revert "Can't seem to get an eventwriter in the context of an asynccomputetask -- at least not easily"

This reverts commit 315fb69a32.
This commit is contained in:
Daniel Flanagan 2024-08-02 23:32:41 -05:00
parent 315fb69a32
commit 18bdb52a7f
2 changed files with 22 additions and 33 deletions

View file

@ -14,7 +14,6 @@ use bevy::audio::{AudioPlugin, SpatialScale};
use bevy::window::{PrimaryWindow, WindowMode};
use bevy_ecs_tilemap::TilemapPlugin;
use input::Input;
use map::TileLoadEvent;
use prelude::*;
use statue::SpawnStatueEvent;
@ -93,7 +92,6 @@ fn main() -> AppExit {
);
app.add_event::<SpawnStatueEvent>();
app.add_event::<TileLoadEvent>();
app.add_systems(
OnEnter(View::LoadingMenu),

View file

@ -1,5 +1,10 @@
use std::sync::mpsc::{channel, Receiver};
use crate::{inspector::HideFromInspector, prelude::*};
use bevy::tasks::{block_on, futures_lite::future, AsyncComputeTaskPool, Task};
use bevy::{
ecs::{system::SystemState, world::CommandQueue},
tasks::{block_on, futures_lite::future, AsyncComputeTaskPool, Task},
};
// use bevy::{
// ecs::world::CommandQueue,
// tasks::{AsyncComputeTaskPool, Task},
@ -12,17 +17,13 @@ use rand_seeder::Seeder;
#[derive(Component, Debug)]
pub struct Tilemap;
#[derive(Resource, Debug)]
pub struct TileChan(Receiver<TileBundle>);
#[derive(Component, Debug)]
pub struct TileLoader(Task<()>);
pub struct TileInit(Task<()>);
#[derive(Event)]
pub struct TileLoadEvent(TileBundle);
pub fn init(
mut commands: Commands,
assets: Res<AssetServer>,
mut loads: EventWriter<TileLoadEvent>,
) {
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?
let mut rng: Pcg64 = Seeder::from("default_seed").make_rng();
@ -52,6 +53,7 @@ pub fn init(
let pool = AsyncComputeTaskPool::get();
let task = pool.spawn(async move {
let (tx, rx) = channel::<TileBundle>();
for x in 0..size.x {
for y in 0..size.y {
let position = TilePos::new(x, y);
@ -60,38 +62,27 @@ pub fn init(
} else {
0
});
loads.send(TileLoadEvent(TileBundle {
tx.send(TileBundle {
position,
tilemap_id: TilemapId(tilemap),
texture_index,
..default()
}));
..Default::default()
})
.unwrap();
}
}
return ();
tx
});
commands.entity(tilemap).insert(TileLoader(task));
commands.entity(tilemap).insert(TileInit(task));
}
pub fn tile_loaders(
mut commands: Commands,
mut storage: Query<&mut TileStorage>,
mut tasks: Query<&mut TileLoader>,
mut loads: EventReader<TileLoadEvent>,
) {
pub fn tile_loaders(mut commands: Commands, mut tasks: Query<&mut TileInit>) {
// TODO: to avoid locking up the universe we only want to handle a certain number per iteration (Update)
let mut storage = storage.single_mut();
for ev in loads.read() {
let ref_position = &ev.0.position;
let tile = commands.spawn((HideFromInspector, ev.0)).id();
storage.set(ref_position, tile);
}
for mut task in &mut tasks {
if block_on(future::poll_once(&mut task.0)).is_some() {
// TODO: transition to game?
if let Some(mut commands_queue) = block_on(future::poll_once(&mut task.0)) {
// append the returned command queue to have it execute later
commands.append(&mut commands_queue);
}
}
}