@@ -2,12 +2,9 @@ use bevy::prelude::*;
22use bevy_ecs_tilemap:: prelude:: * ;
33
44use crate :: {
5- gameplay:: {
6- sprite_sort:: ZIndexSprite ,
7- world:: {
8- WorldSpawnSystems ,
9- tilemap:: { CHUNK_SIZE , TILE_OFFSET , TILE_SIZE } ,
10- } ,
5+ gameplay:: world:: {
6+ WorldSpawnSystems ,
7+ tilemap:: { CHUNK_SIZE , TILE_OFFSET , TILE_SIZE } ,
118 } ,
129 screens:: Screen ,
1310} ;
@@ -25,73 +22,45 @@ pub fn plugin(app: &mut App) {
2522#[ reflect( Component ) ]
2623pub struct Chunk ;
2724
28- #[ derive( Component , Reflect , Debug ) ]
29- #[ reflect( Component ) ]
30- #[ relationship_target( relationship = LayerOf ) ]
31- pub struct Layers ( Vec < Entity > ) ;
32-
33- #[ derive( Component , Reflect , Debug ) ]
34- #[ reflect( Component ) ]
35- #[ relationship( relationship_target = Layers ) ]
36- pub struct LayerOf ( pub Entity ) ;
37-
3825fn spawn_chunk ( mut commands : Commands , asset_server : Res < AssetServer > ) {
39- let chunk_id = commands
40- . spawn ( (
41- Name :: new ( "Chunk" ) ,
42- Chunk ,
43- Transform :: default ( ) ,
44- Visibility :: default ( ) ,
45- ) )
46- . id ( ) ;
26+ let map_size = TilemapSize :: from ( CHUNK_SIZE ) ;
27+ let tile_size = TilemapTileSize :: from ( TILE_SIZE ) ;
28+ let grid_size = TilemapGridSize :: from ( TILE_OFFSET ) ;
29+ let storage = TileStorage :: empty ( map_size) ;
4730
48- for i in 0 ..5 {
49- let map_size = TilemapSize :: from ( CHUNK_SIZE ) ;
50- let tile_size = TilemapTileSize :: from ( TILE_SIZE ) ;
51- let grid_size = TilemapGridSize :: from ( TILE_OFFSET ) ;
52- let storage = TileStorage :: empty ( map_size) ;
53-
54- commands. spawn ( (
55- Name :: new ( format ! ( "Layer {i}" ) ) ,
56- LayerOf ( chunk_id) ,
57- ChildOf ( chunk_id) ,
58- ZIndexSprite ( i) ,
59- TilemapBundle {
60- transform : Transform :: from_xyz ( 0.0 , grid_size. y / 2.0 * i as f32 , 0.0 ) ,
61- grid_size,
62- size : map_size,
63- storage,
64- texture : TilemapTexture :: Single ( asset_server. load ( "tiles/grass.png" ) ) ,
65- tile_size,
66- map_type : TilemapType :: Isometric ( IsoCoordSystem :: Diamond ) ,
67- anchor : TilemapAnchor :: Center ,
68- render_settings : TilemapRenderSettings {
69- render_chunk_size : UVec2 :: new ( map_size. x , 1 ) ,
70- y_sort : true ,
71- } ,
72- ..default ( )
31+ commands. spawn ( (
32+ Name :: new ( "Chunk" ) ,
33+ Chunk ,
34+ TilemapBundle {
35+ transform : Transform :: from_xyz ( 0.0 , 0.0 , 0.0 ) ,
36+ grid_size,
37+ size : map_size,
38+ storage,
39+ texture : TilemapTexture :: Single ( asset_server. load ( "tiles/grass.png" ) ) ,
40+ tile_size,
41+ map_type : TilemapType :: Isometric ( IsoCoordSystem :: Diamond ) ,
42+ anchor : TilemapAnchor :: Center ,
43+ render_settings : TilemapRenderSettings {
44+ render_chunk_size : UVec2 :: new ( map_size. x , 1 ) ,
45+ y_sort : true ,
7346 } ,
74- ) ) ;
75- }
47+ ..default ( )
48+ } ,
49+ ) ) ;
7650}
7751
7852fn spawn_flat_ground (
7953 mut commands : Commands ,
80- chunk_layers : Single < & Layers , With < Chunk > > ,
81- mut tile_storage_query : Query < & mut TileStorage > ,
54+ chunk_query : Query < ( Entity , & mut TileStorage ) , With < Chunk > > ,
8255) {
83- for ( i, layer) in chunk_layers. iter ( ) . enumerate ( ) . take ( 4 ) {
84- let Ok ( mut tile_storage) = tile_storage_query. get_mut ( layer) else {
85- continue ;
86- } ;
87-
56+ for ( chunk_entity, mut chunk_tile_storage) in chunk_query {
8857 fill_tilemap_rect (
8958 TileTextureIndex ( 0 ) ,
9059 TilePos :: new ( 0 , 0 ) ,
91- TilemapSize :: from ( CHUNK_SIZE / ( i + 1 ) as u32 ) ,
92- TilemapId ( layer ) ,
60+ TilemapSize :: from ( CHUNK_SIZE ) ,
61+ TilemapId ( chunk_entity ) ,
9362 & mut commands,
94- & mut tile_storage ,
63+ & mut chunk_tile_storage ,
9564 ) ;
9665 }
9766}
0 commit comments