Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

General fixes to stability. #219

Merged
merged 1 commit into from
Aug 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions examples/chunking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ use bevy_ecs_tilemap::prelude::*;
mod helpers;

const TILE_SIZE: TilemapTileSize = TilemapTileSize { x: 16.0, y: 16.0 };
const CHUNK_SIZE: TilemapSize = TilemapSize { x: 32, y: 32 };
const CHUNK_SIZE: TilemapSize = TilemapSize { x: 4, y: 4 };

fn spawn_chunk(commands: &mut Commands, asset_server: &AssetServer, chunk_pos: IVec2) {
let tilemap_entity = commands.spawn().id();
let mut tile_storage = TileStorage::empty(CHUNK_SIZE);
// Spawn the elements of the tilemap.
for x in 0..32u32 {
for y in 0..32u32 {
for x in 0..CHUNK_SIZE.x {
for y in 0..CHUNK_SIZE.y {
let tile_pos = TilePos { x, y };
let tile_entity = commands
.spawn()
Expand All @@ -20,6 +20,7 @@ fn spawn_chunk(commands: &mut Commands, asset_server: &AssetServer, chunk_pos: I
..Default::default()
})
.id();
commands.entity(tilemap_entity).add_child(tile_entity);
tile_storage.set(&tile_pos, Some(tile_entity));
}
}
Expand Down Expand Up @@ -76,14 +77,14 @@ fn spawn_chunks_around_camera(
fn despawn_outofrange_chunks(
mut commands: Commands,
camera_query: Query<&Transform, With<Camera>>,
chunks_query: Query<(Entity, &Transform), With<TileStorage>>,
chunks_query: Query<(Entity, &Transform, &TileStorage)>,
mut chunk_manager: ResMut<ChunkManager>,
) {
for camera_transform in camera_query.iter() {
for (entity, chunk_transform) in chunks_query.iter() {
for (entity, chunk_transform, tile_storage) in chunks_query.iter() {
let chunk_pos = chunk_transform.translation.xy();
let distance = camera_transform.translation.xy().distance(chunk_pos);
if distance > 2048.0 {
if distance > 320.0 {
let x = (chunk_pos.x as f32 / (CHUNK_SIZE.x as f32 * TILE_SIZE.x)).floor() as i32;
let y = (chunk_pos.y as f32 / (CHUNK_SIZE.y as f32 * TILE_SIZE.y)).floor() as i32;
chunk_manager.spawned_chunks.remove(&IVec2::new(x, y));
Expand Down
3 changes: 2 additions & 1 deletion src/helpers.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use bevy::{
math::Vec2,
prelude::{Commands, Transform},
prelude::{BuildChildren, Commands, Transform},
};

use crate::{
Expand Down Expand Up @@ -89,6 +89,7 @@ pub fn fill_tilemap(
..Default::default()
})
.id();
commands.entity(tilemap_id.0).add_child(tile_entity);
tile_storage.set(&tile_pos, Some(tile_entity));
}
}
Expand Down
9 changes: 9 additions & 0 deletions src/render/chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,15 @@ impl RenderChunk2dStorage {
chunk_storage.get_mut(&position.xyz()).unwrap()
}

pub fn remove_tile_with_entity(&mut self, entity: Entity) {
if let Some((chunk, tile_pos)) = self.get_mut_from_entity(entity) {
chunk.set(&tile_pos.into(), None);
}

self.entity_to_chunk.remove(&entity);
self.entity_to_chunk_tile.remove(&entity);
}

pub fn get_mut_from_entity(&mut self, entity: Entity) -> Option<(&mut RenderChunk2d, UVec2)> {
if !self.entity_to_chunk_tile.contains_key(&entity) {
return None;
Expand Down
4 changes: 1 addition & 3 deletions src/render/prepare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,7 @@ pub fn prepare_removal(
removed_maps: Query<&ExtractedRemovedMap>,
) {
for removed_tile in removed_tiles.iter() {
if let Some((chunk, tile_pos)) = chunk_storage.get_mut_from_entity(removed_tile.entity) {
chunk.set(&tile_pos.into(), None);
}
chunk_storage.remove_tile_with_entity(removed_tile.entity)
}

for removed_map in removed_maps.iter() {
Expand Down