Skip to content

Commit 7c474d8

Browse files
committed
refactor chunk loading
1 parent 01dc148 commit 7c474d8

File tree

2 files changed

+36
-32
lines changed

2 files changed

+36
-32
lines changed

Diff for: src/level/chunk.rs

+13-4
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ impl Bounds {
3939
pub struct LevelChunk {
4040
pub level: GameLevel,
4141
pub tiles: Vec<Tile>,
42-
pub loading_index: usize,
42+
pub loading_index: i32,
4343
pub entities: HashMap<(i32, i32), SpawnEntityProps>,
4444
pub bounds: Bounds,
4545
pub dirty: Option<Bounds>,
@@ -68,6 +68,8 @@ impl LevelChunk {
6868
let max_y =
6969
((ldtk_level.world_y + ldtk_level.px_hei) / ldtk.coordinate.default_grid_size) as i32;
7070

71+
let height = max_y - min_y;
72+
7173
// タイル読み込み
7274

7375
let int_grid_layer = ldtk_level.get_layer("Tiles").unwrap();
@@ -87,8 +89,6 @@ impl LevelChunk {
8789
});
8890
}
8991

90-
let tiles_length = tiles.len();
91-
9292
// エンティティ読み込み
9393

9494
let entity_layer = ldtk_level.get_layer("Entities").unwrap();
@@ -146,7 +146,7 @@ impl LevelChunk {
146146
min_y,
147147
max_y,
148148
},
149-
loading_index: if lazy_loading { 0 } else { tiles_length },
149+
loading_index: if lazy_loading { 0 } else { height },
150150
dirty: if lazy_loading {
151151
None
152152
} else {
@@ -160,6 +160,15 @@ impl LevelChunk {
160160
};
161161
}
162162

163+
#[allow(dead_code)]
164+
pub fn width(&self) -> i32 {
165+
self.bounds.max_x - self.bounds.min_x
166+
}
167+
168+
pub fn height(&self) -> i32 {
169+
self.bounds.max_y - self.bounds.min_y
170+
}
171+
163172
pub fn get_level_tile(&self, x: i32, y: i32) -> &Tile {
164173
if !self.bounds.contains(x, y) {
165174
static BLANK_TILE: LazyLock<Tile> = LazyLock::new(|| Tile::default());

Diff for: src/page/in_game.rs

+23-28
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ fn update_lazy_tile_sprites(
415415
tiles_query: Query<(Entity, &TileSprite)>,
416416
) {
417417
// チャンクごとに更新すべきインデックス列を保持
418-
let mut tile_spawning_indices: HashMap<String, Vec<(i32, i32)>> = HashMap::new();
418+
let mut tile_spawning_indices: HashMap<String, Vec<i32>> = HashMap::new();
419419

420420
// ハッシュマップにチャンクごとVecを生成
421421
for chunk in &world.chunks {
@@ -428,32 +428,25 @@ fn update_lazy_tile_sprites(
428428
let indiceis_to_spawn = tile_spawning_indices.get_mut(&chunk.level.0).unwrap();
429429

430430
// loading_index の続きを一部更新
431-
for _ in 0..50 {
432-
if chunk.tiles.len() <= chunk.loading_index {
433-
break;
434-
}
435-
436-
let w = chunk.bounds.max_x - chunk.bounds.min_x;
437-
let lx = chunk.loading_index as i32 % w;
438-
let ly = chunk.loading_index as i32 / w;
439-
let x = chunk.bounds.min_x + lx as i32;
440-
let y = chunk.bounds.min_y + ly as i32;
441-
indiceis_to_spawn.push((x, y));
442-
443-
// todo これが重い?
444-
// clear_tiles_by_bounds(
445-
// &mut commands,
446-
// &tiles_query,
447-
// Bounds {
448-
// min_x: x,
449-
// max_x: x + 1,
450-
// min_y: y,
451-
// max_y: y + 1,
452-
// },
453-
// );
454-
455-
chunk.loading_index += 1;
431+
if chunk.height() <= chunk.loading_index {
432+
continue;
456433
}
434+
435+
indiceis_to_spawn.push(chunk.bounds.min_y + chunk.loading_index);
436+
437+
// todo これが重い?
438+
clear_tiles_by_bounds(
439+
&mut commands,
440+
&tiles_query,
441+
Bounds {
442+
min_x: chunk.bounds.min_x,
443+
max_x: chunk.bounds.max_x,
444+
min_y: chunk.bounds.min_y + chunk.loading_index,
445+
max_y: chunk.bounds.min_y + chunk.loading_index + 1,
446+
},
447+
);
448+
449+
chunk.loading_index += 1;
457450
}
458451

459452
// 予約されたインデックスに応じてスプライトを生成
@@ -466,8 +459,10 @@ fn update_lazy_tile_sprites(
466459
.unwrap();
467460

468461
// スプライトを再生成
469-
for (x, y) in indices.iter() {
470-
spawn_world_tile(&mut commands, &registry, &world, &chunk, *x, *y);
462+
for y in indices.iter() {
463+
for x in chunk.bounds.min_x..chunk.bounds.max_x {
464+
spawn_world_tile(&mut commands, &registry, &world, &chunk, x, *y);
465+
}
471466
}
472467
}
473468
}

0 commit comments

Comments
 (0)