Stopping for now
This commit is contained in:
parent
d289460ced
commit
deabad05ee
2 changed files with 43 additions and 23 deletions
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
65
src/map.rs
65
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<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,25 +29,32 @@ 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);
|
||||
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<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();
|
||||
|
|
Loading…
Reference in a new issue