Skip to content

Commit

Permalink
Fixed issue with map and morton coords.
Browse files Browse the repository at this point in the history
  • Loading branch information
StarArawn authored and StarArawn committed May 3, 2021
1 parent 121eb85 commit 330cfa2
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 14 deletions.
1 change: 0 additions & 1 deletion examples/accessing_tiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ fn update_map(
current_color.0 = 1;
}
let mut color = current_color.0;
dbg!(color);
for x in (2..128).step_by(4) {
for y in (2..128).step_by(4) {
let neighbors = map.get_tile_neighbors(
Expand Down
4 changes: 2 additions & 2 deletions src/chunk.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::{task::{Context, Poll}};
use bevy::{prelude::*, render::{pipeline::RenderPipeline, render_graph::base::MainPass}, tasks::{AsyncComputeTaskPool, Task, TaskPool}};
use bevy::{prelude::*, render::{pipeline::RenderPipeline, render_graph::base::MainPass}, tasks::{AsyncComputeTaskPool, Task}};
use futures_util::FutureExt;
use crate::{map_vec2::MapVec2, morton_index, prelude::{SquareChunkMesher, TilemapChunkMesher}, render::pipeline::TILE_MAP_PIPELINE_HANDLE, tile::Tile};

Expand Down Expand Up @@ -147,7 +147,7 @@ impl Chunk {
pub fn to_chunk_pos(&self, position: MapVec2) -> MapVec2 {
MapVec2::new(
position.x - (self.settings.position.x * self.settings.size.x),
position.y - (self.settings.position.y * self.settings.size.x),
position.y - (self.settings.position.y * self.settings.size.y),
)
}
}
Expand Down
42 changes: 31 additions & 11 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,18 @@ impl Map {
// First find chunk tile should live in:
let mut possible_tile_event = None;

if let Some(chunk_data) = self.get_chunk_mut(MapVec2::new(
let chunk_pos = MapVec2::new(
tile_pos.x / self.chunk_size.x,
tile_pos.y / self.chunk_size.y
)) {
if let Some(tile_entity) = chunk_data.1[morton_index(tile_pos)] {
);
let chunk_size = self.chunk_size;
if let Some(chunk_data) = self.get_chunk_mut(chunk_pos) {
let chunk_tile_pos = MapVec2::new(
tile_pos.x - (chunk_pos.x * chunk_size.x),
tile_pos.y - (chunk_pos.y * chunk_size.y),
);

if let Some(tile_entity) = chunk_data.1[morton_index(chunk_tile_pos)] {
// If the tile already exists we need to queue an event.
// We do this because we have good way of changing the actual tile data from here.
// Another possibility is having the user pass in a query of tiles and matching based off of entity,
Expand All @@ -104,7 +111,7 @@ impl Map {
..tile
})
.insert(tile_pos).id();
chunk_data.1[morton_index(tile_pos)] = Some(tile_entity);
chunk_data.1[morton_index(chunk_tile_pos)] = Some(tile_entity);

return Ok(tile_entity);
}
Expand Down Expand Up @@ -180,11 +187,15 @@ impl Map {
tile_pos.x / self.chunk_size.x,
tile_pos.y / self.chunk_size.y,
);
let chunk_tile_pos = MapVec2::new(
tile_pos.x - (chunk_pos.x * self.chunk_size.x),
tile_pos.y - (chunk_pos.y * self.chunk_size.y),
);

let chunk = self.chunks.get(&chunk_pos);
if chunk.is_some() {
let (_, tiles) = chunk.unwrap();
tiles[morton_index(tile_pos)]
tiles[morton_index(chunk_tile_pos)]
} else {
return None;
}
Expand All @@ -197,10 +208,15 @@ impl Map {
/// Note: This list could be slightly out of date.
/// This is slow.
pub fn get_all_tiles(&self) -> Vec<(MapVec2, &Option<Entity>)> {
self.chunks.values().flat_map(|(_, tiles)|
tiles.iter().enumerate().map(|(index, entity)|
(MapVec2::from_morton(index), entity)
).collect::<Vec<(MapVec2, &Option<Entity>)>>()
self.chunks.iter().flat_map(|(chunk_pos, tiles)|
tiles.1.iter().enumerate().map(|(index, entity)| {
let chunk_tile_pos = MapVec2::from_morton(index);
let global_tile_pos = MapVec2::new(
chunk_tile_pos.x + (chunk_pos.x * self.chunk_size.x),
chunk_tile_pos.y + (chunk_pos.y * self.chunk_size.y),
);
(global_tile_pos, entity)
}).collect::<Vec<(MapVec2, &Option<Entity>)>>()
).collect()
}

Expand Down Expand Up @@ -284,7 +300,8 @@ pub(crate) fn update_chunk_hashmap_for_removed_tiles(

// Remove tile from chunk tiles cache.
if let Ok(mut tile_chunk) = chunk_query.get_mut(removed_tile.chunk) {
tile_chunk.tiles[morton_index(*tile_pos)] = None;
let tile_pos = tile_chunk.to_chunk_pos(*tile_pos);
tile_chunk.tiles[morton_index(tile_pos)] = None;
}

let chunk_coords = MapVec2::new(
Expand All @@ -294,7 +311,10 @@ pub(crate) fn update_chunk_hashmap_for_removed_tiles(

// Remove tile from map tiles cache.
if let Some(map_chunk) = map.chunks.get_mut(&chunk_coords) {
map_chunk.1[morton_index(*tile_pos)] = None;
if let Ok(tile_chunk) = chunk_query.get_mut(removed_tile.chunk) {
let tile_pos = tile_chunk.to_chunk_pos(*tile_pos);
map_chunk.1[morton_index(tile_pos)] = None;
}
}

// Remove tile entity
Expand Down

0 comments on commit 330cfa2

Please sign in to comment.