From deabad05ee805169edeb64a58aef81d5134c51bb Mon Sep 17 00:00:00 2001 From: Daniel Flanagan Date: Fri, 2 Aug 2024 17:04:16 -0500 Subject: [PATCH] Stopping for now --- src/main.rs | 1 + src/map.rs | 65 ++++++++++++++++++++++++++++++++++------------------- 2 files changed, 43 insertions(+), 23 deletions(-) diff --git a/src/main.rs b/src/main.rs index 1cd9aeb..61226a6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -141,6 +141,7 @@ fn load_menu(mut app_state: ResMut>) { } fn load_game(mut app_state: ResMut>) { + // need to check if the map is fully loaded before switching app_state.set(View::InGame); } diff --git a/src/map.rs b/src/map.rs index a97de96..4cb6aa9 100644 --- a/src/map.rs +++ b/src/map.rs @@ -1,4 +1,8 @@ use crate::{inspector::HideFromInspector, prelude::*}; +use bevy::{ + ecs::world::CommandQueue, + tasks::{block_on, futures_lite::future, AsyncComputeTaskPool, Task}, +}; // use bevy::{ // ecs::world::CommandQueue, // tasks::{AsyncComputeTaskPool, Task}, @@ -7,18 +11,16 @@ use bevy_ecs_tilemap::prelude::*; use rand::prelude::*; use rand_pcg::Pcg64; use rand_seeder::Seeder; +use std::sync::mpsc::channel; #[derive(Component, Debug)] pub struct Tilemap; -// #[derive(Component)] -// struct MapGen(Task); +#[derive(Component)] +struct MapGen(Task); pub fn spawn(mut commands: Commands, assets: Res) { - // TODO: I'm pretty determined to not have this sieze up the game. Should work with a "loading" screen. - // let task_pool = AsyncComputeTaskPool::get(); - - // let task = task_pool.spawn(async move {}); + // 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(); let size = TilemapSize::new(1024, 1024); @@ -27,25 +29,32 @@ pub fn spawn(mut commands: Commands, assets: Res) { let mut storage = TileStorage::empty(size); // let mutex_storage = Mutex::new(storage); + let task_pool = AsyncComputeTaskPool::get(); + + let (tx, rx) = channel::(); + let tilebundlegenerator = task_pool.spawn(async move { + // this task is for generating tilebundles + for x in 0..size.x { + for y in 0..size.y { + let position = TilePos::new(x, y); + let texture_index = TileTextureIndex(if rng.gen_range(0..1000) > 925 { + rng.gen_range(0..(16 * 8)) + } else { + 0 + }); + tx.send(TileBundle { + position, + tilemap_id: TilemapId(tilemap), + texture_index, + ..Default::default() + }); + } + } + }); + for x in 0..size.x { for y in 0..size.y { - let position = TilePos::new(x, y); - let texture_index = TileTextureIndex(if rng.gen_range(0..1000) > 925 { - rng.gen_range(0..(16 * 8)) - } else { - 0 - }); - let tile = commands - .spawn(( - HideFromInspector, - TileBundle { - position, - tilemap_id: TilemapId(tilemap), - texture_index, - ..Default::default() - }, - )) - .id(); + let tile = commands.spawn().id(); storage.set(&position, tile); } } @@ -133,6 +142,16 @@ pub fn spawn(mut commands: Commands, assets: Res) { // } // } +fn load(mut commands: Commands, mut tasks: Query<&mut MapGen>) { + // to avoid locking up the universe we only want to handle a certain number per iteration (Update) + for mut task in &mut tasks { + 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); + } + } +} + pub fn exit(mut commands: Commands, q: Query>) { for id in q.iter() { commands.entity(id).despawn_recursive();