Stopping for now

This commit is contained in:
Daniel Flanagan 2024-08-02 17:04:16 -05:00
parent d289460ced
commit deabad05ee
2 changed files with 43 additions and 23 deletions

View file

@ -141,6 +141,7 @@ fn load_menu(mut app_state: ResMut<NextState<View>>) {
}
fn load_game(mut app_state: ResMut<NextState<View>>) {
// need to check if the map is fully loaded before switching
app_state.set(View::InGame);
}

View file

@ -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<CommandQueue>);
#[derive(Component)]
struct MapGen(Task<TileBundleBuilder>);
pub fn spawn(mut commands: Commands, assets: Res<AssetServer>) {
// 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,6 +29,11 @@ pub fn spawn(mut commands: Commands, assets: Res<AssetServer>) {
let mut storage = TileStorage::empty(size);
// let mutex_storage = Mutex::new(storage);
let task_pool = AsyncComputeTaskPool::get();
let (tx, rx) = channel::<TileBundle>();
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);
@ -35,17 +42,19 @@ pub fn spawn(mut commands: Commands, assets: Res<AssetServer>) {
} else {
0
});
let tile = commands
.spawn((
HideFromInspector,
TileBundle {
tx.send(TileBundle {
position,
tilemap_id: TilemapId(tilemap),
texture_index,
..Default::default()
},
))
.id();
});
}
}
});
for x in 0..size.x {
for y in 0..size.y {
let tile = commands.spawn().id();
storage.set(&position, tile);
}
}
@ -133,6 +142,16 @@ pub fn spawn(mut commands: Commands, assets: Res<AssetServer>) {
// }
// }
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<Entity, With<Tilemap>>) {
for id in q.iter() {
commands.entity(id).despawn_recursive();