use std::sync::mpsc::{channel, Receiver}; use crate::{inspector::HideFromInspector, prelude::*}; use bevy::{ ecs::{system::SystemState, world::CommandQueue}, tasks::{block_on, futures_lite::future, AsyncComputeTaskPool, Task}, }; // use bevy::{ // ecs::world::CommandQueue, // tasks::{AsyncComputeTaskPool, Task}, // }; use bevy_ecs_tilemap::prelude::*; use rand::prelude::*; use rand_pcg::Pcg64; use rand_seeder::Seeder; #[derive(Component, Debug)] pub struct Tilemap; #[derive(Resource, Debug)] pub struct TileChan(Receiver); #[derive(Component, Debug)] pub struct TileInit(Task<()>); 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(); let size = TilemapSize::new(1024, 1024); let tile_size = TilemapTileSize::new(16., 16.); let grid_size = TilemapGridSize::new(16., 16.); let map_type = TilemapType::Square; let texture = TilemapTexture::Single(assets.load("img/Tileset Grass.png")); let storage = TileStorage::empty(size); let tilemap = commands .spawn(( Tilemap, Name::new("Tilemap"), TilemapBundle { grid_size, map_type, size, storage, texture, tile_size, transform: get_tilemap_center_transform(&size, &grid_size, &map_type, f32::MIN), ..Default::default() }, )) .id(); 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); 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() }) .unwrap(); } } tx }); commands.entity(tilemap).insert(TileInit(task)); } 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) 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(); } }