Guest User

Pre Update functions

a guest
Jun 30th, 2025
3
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 3.65 KB | None | 0 0
  1. pub fn set_starting_scene(
  2.     mut commands: Commands,
  3.     starting_game_scene: Res<StartingGameScene>,
  4.     mut current_game_scenes: ResMut<CurrentGameScenes>
  5. ) {
  6.     info!("starting scene set");
  7.  
  8.     current_game_scenes.0.clear();
  9.     current_game_scenes.0.insert(starting_game_scene.0.clone());
  10.  
  11.     commands.remove_resource::<StartingGameScene>();
  12. }
  13.  
  14. pub fn build_world_path_finding_grid(
  15.     mut commands: Commands,
  16.     tiled_worlds: Res<Assets<TiledWorld>>,
  17.     tiled_maps: Res<Assets<TiledMap>>,
  18.     s_world: Single<(Entity, &TiledWorldHandle, &TilemapAnchor), Without<Grid<CardinalNeighborhood>>>,
  19. ) {
  20.     let (entity, world_handle, anchor) = s_world.into_inner();
  21.  
  22.     let Some(tiled_world) = tiled_worlds.get(world_handle.0.id()) else {
  23.         return;
  24.     };
  25.  
  26.     let Some(first_map) = tiled_world.maps.first() else {
  27.         return;
  28.     };
  29.  
  30.     let Some(first_tiled_map) = tiled_maps.get(first_map.1.id()) else {
  31.         return;
  32.     };
  33.  
  34.     let tile_size = Vec2::new(first_tiled_map.map.tile_width as f32, first_tiled_map.map.tile_height as f32);
  35.  
  36.     let grid_size = tiled_world.rect.max / tile_size as Vec2;
  37.  
  38.     let mut chunk_size = 10;
  39.     for count in 0..10 {
  40.         let grid_size_scale = (grid_size.x * grid_size.y) as u32;
  41.         let divider = 20 - count as u32;
  42.  
  43.         if grid_size_scale % divider.pow(2) != 0 || grid_size_scale / divider.pow(2) == 1 {
  44.             continue;
  45.         }
  46.  
  47.         chunk_size = divider;
  48.         break;
  49.     }
  50.  
  51.     let mut grid = Grid::<CardinalNeighborhood>::new(&GridSettings {
  52.         width: grid_size.x as u32,
  53.         height: grid_size.y as u32,
  54.         depth: 1,
  55.         chunk_size: chunk_size,
  56.         chunk_depth: 1,
  57.         chunk_ordinal: false,
  58.         default_cost: 1,
  59.         default_wall: true,
  60.         collision: true,
  61.         avoidance_distance: 4,
  62.     });
  63.  
  64.     for (rect, _) in tiled_world.maps.iter() {
  65.         let min = rect.min / tile_size;
  66.         let max = rect.max / tile_size;
  67.  
  68.         for x in (min.x as u32)..(max.x as u32) {
  69.             for y in (min.y as u32)..(max.y as u32) {
  70.                 grid.set_point(UVec3::new(x, y, 0), Point::new(1, false));
  71.             }
  72.         }
  73.  
  74.     }
  75.  
  76.     grid.build();
  77.  
  78.     let offset = anchor.as_offset(
  79.         &TilemapSize::new(grid_size.x as u32, grid_size.y as u32),
  80.         &TilemapGridSize::new(tile_size.x, tile_size.y),
  81.         &TilemapTileSize::new(tile_size.x, tile_size.y),
  82.         &TilemapType::Square,
  83.     );
  84.  
  85.     commands.entity(entity).insert((
  86.         grid,
  87.         GridTileSize(tile_size),
  88.         children![
  89.             (
  90.                 DebugMap {
  91.                     tile_width: tile_size.x as u32,
  92.                     tile_height: tile_size.y as u32,
  93.                     map_type: DebugMapType::Square,
  94.                     draw_chunks: true,
  95.                     draw_points: true,
  96.                     draw_entrances: false,
  97.                     draw_cached_paths: false,
  98.                 },
  99.                 Transform::from_translation(offset.extend(0.0))
  100.             ),
  101.             (
  102.                 Collider::rectangle(tile_size.x - 2., tile_size.y - 2.),
  103.                 ScanCollider
  104.             )
  105.         ]
  106.     ));
  107. }
  108.  
  109. pub fn handle_colliders_spawned(
  110.     mut commands: Commands,
  111.     e_collider_created: EventReader<TiledColliderCreated>,
  112.     q_tiled_entities: Query<Entity, (Without<TiledCollidersSpawned>, Or<(With<TiledMapMarker>, With<TiledMapLayer>, With<TiledMapObject>)>)>,
  113. ) {
  114.     if e_collider_created.is_empty() {
  115.         return;
  116.     }
  117.  
  118.     info!("spawned colliders");
  119.  
  120.     for entity in q_tiled_entities {
  121.         commands.entity(entity).insert(TiledCollidersSpawned);
  122.     }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment