2024-08-02 23:16:54 -05:00
use std ::sync ::mpsc ::{ channel , Receiver } ;
2024-08-02 16:32:28 -05:00
use crate ::{ inspector ::HideFromInspector , prelude ::* } ;
2024-08-02 17:04:16 -05:00
use bevy ::{
2024-08-02 21:43:30 -05:00
ecs ::{ system ::SystemState , world ::CommandQueue } ,
2024-08-02 17:04:16 -05:00
tasks ::{ block_on , futures_lite ::future , AsyncComputeTaskPool , Task } ,
} ;
2024-08-02 16:32:28 -05:00
// use bevy::{
// ecs::world::CommandQueue,
// tasks::{AsyncComputeTaskPool, Task},
// };
2024-08-02 16:43:30 -05:00
use bevy_ecs_tilemap ::prelude ::* ;
2024-08-02 13:40:43 -05:00
use rand ::prelude ::* ;
use rand_pcg ::Pcg64 ;
use rand_seeder ::Seeder ;
#[ derive(Component, Debug) ]
pub struct Tilemap ;
2024-08-02 23:16:54 -05:00
#[ derive(Resource, Debug) ]
pub struct TileChan ( Receiver < TileBundle > ) ;
2024-08-02 21:43:30 -05:00
2024-08-02 23:16:54 -05:00
#[ derive(Component, Debug) ]
pub struct TileInit ( Task < ( ) > ) ;
2024-08-02 16:32:28 -05:00
2024-08-02 23:16:54 -05:00
pub fn init ( mut commands : Commands , assets : Res < AssetServer > ) {
2024-08-02 17:04:16 -05:00
// 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?
2024-08-02 13:40:43 -05:00
2024-08-02 16:32:28 -05:00
let mut rng : Pcg64 = Seeder ::from ( " default_seed " ) . make_rng ( ) ;
2024-08-02 16:43:30 -05:00
let size = TilemapSize ::new ( 1024 , 1024 ) ;
2024-08-02 23:16:54 -05:00
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 ( ) ;
2024-08-02 22:53:52 -05:00
2024-08-02 23:16:54 -05:00
let pool = AsyncComputeTaskPool ::get ( ) ;
let task = pool . spawn ( async move {
let ( tx , rx ) = channel ::< TileBundle > ( ) ;
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
2024-08-02 17:04:16 -05:00
} ) ;
2024-08-02 23:16:54 -05:00
tx . send ( TileBundle {
position ,
tilemap_id : TilemapId ( tilemap ) ,
texture_index ,
.. Default ::default ( )
} )
. unwrap ( ) ;
}
2024-08-02 17:04:16 -05:00
}
2024-08-02 23:16:54 -05:00
tx
} ) ;
commands . entity ( tilemap ) . insert ( TileInit ( task ) ) ;
2024-08-02 21:43:30 -05:00
}
2024-08-02 17:04:16 -05:00
2024-08-02 23:16:54 -05:00
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)
2024-08-02 21:43:30 -05:00
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 ) ;
2024-08-02 16:43:30 -05:00
}
}
2024-08-02 21:43:30 -05:00
}
2024-08-02 16:43:30 -05:00
2024-08-02 13:40:43 -05:00
pub fn exit ( mut commands : Commands , q : Query < Entity , With < Tilemap > > ) {
for id in q . iter ( ) {
commands . entity ( id ) . despawn_recursive ( ) ;
}
}