Skip to content

Commit

Permalink
Fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
jamescarterbell committed Jan 9, 2024
1 parent 8c0f911 commit 892e1f5
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 11 deletions.
8 changes: 7 additions & 1 deletion crates/bevy_tiles/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,13 @@ where
if let Some(chunk_info) = remove_chunk::<N>(world, map, chunk_c) {
chunk_info
} else {
let chunk_id = world.spawn((ChunkCoord::from(chunk_c), InMap(map_id))).id();
let chunk_id = world
.spawn((
ChunkCoord::from(chunk_c),
MapLabel::<L>::default(),
InMap(map_id),
))
.id();
map.chunks.insert(chunk_c.into(), chunk_id);
(chunk_id, Chunk::new(L::CHUNK_SIZE.pow(N as u32)))
}
Expand Down
31 changes: 24 additions & 7 deletions crates/bevy_tiles_render/examples/hello_tile.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::iter::repeat_with;

use bevy::{
app::{App, Startup},
app::{App, Startup, Update},
core_pipeline::core_2d::Camera2dBundle,
diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin},
ecs::system::Commands,
Expand All @@ -18,6 +20,7 @@ use bevy_tiles_render::{
maps::{TileGridSize, TileMapRenderer, TileMapRenderingBundle, TileSize},
TilesRenderPlugin,
};
use rand::Rng;

fn main() {
App::new()
Expand All @@ -28,13 +31,14 @@ fn main() {
))
.add_plugins((TilesPlugin, TilesRenderPlugin))
.add_systems(Startup, spawn)
.add_systems(Update, change_map)
.run();
}

struct GameLayer;

impl TileMapLabel for GameLayer {
const CHUNK_SIZE: usize = 32;
const CHUNK_SIZE: usize = 128;
}

fn spawn(mut commands: Commands) {
Expand All @@ -46,11 +50,6 @@ fn spawn(mut commands: Commands) {
..Default::default()
});

tile_commands.spawn_tile_batch(
CoordIterator::new([-1000, -1000], [1000, 1000]),
move |_| (),
);

commands.spawn(Camera2dBundle {
projection: OrthographicProjection {
scale: 10.0,
Expand All @@ -61,3 +60,21 @@ fn spawn(mut commands: Commands) {
..Default::default()
});
}

fn change_map(mut commands: Commands) {
let mut rng = rand::thread_rng();
let changes = rng.gen_range(500..1000);
let mut tile_commands = commands.tiles::<GameLayer, 2>();

let spawn = rng.gen_bool(0.5);

let tiles = Vec::from_iter(
repeat_with(|| [rng.gen_range(-1000..1000), rng.gen_range(-1000..1000)]).take(changes),
);

if spawn {
tile_commands.spawn_tile_batch(tiles, |_| ())
} else {
tile_commands.despawn_tile_batch(tiles)
}
}
9 changes: 6 additions & 3 deletions crates/bevy_tiles_render/src/extract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,12 @@ pub fn extract_chunks(
commands.insert_or_spawn_batch(extracted_maps);

let chunks_len = chunks.iter().len();
let mut extracted_chunks = ArrayQueue::new(chunks_len);
let mut extracted_saved_chunks = ArrayQueue::new(chunks_len);
let mut chunk_edges = ArrayQueue::new(chunks_len);
if chunks_len == 0 {
return;
}
let extracted_chunks = ArrayQueue::new(chunks_len);
let extracted_saved_chunks = ArrayQueue::new(chunks_len);
let chunk_edges = ArrayQueue::new(chunks_len);

changed_tiles.iter().for_each(|in_chunk| {
saved_chunks.remove(&in_chunk.get());
Expand Down

0 comments on commit 892e1f5

Please sign in to comment.