diff --git a/src/main.rs b/src/main.rs index 2eff81c..8e86411 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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::(); - app.add_event::(); app.add_systems( OnEnter(View::LoadingMenu), diff --git a/src/map.rs b/src/map.rs index 61aff28..1680157 100644 --- a/src/map.rs +++ b/src/map.rs @@ -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); + #[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, - mut loads: EventWriter, -) { +pub fn init(mut commands: Commands, assets: Res) { // 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::(); 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, -) { +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); } } }