Wrap TileMap HashMaps in Arc for async pathfinding access
- Add Arc<AHashMap> wrapper around floor_tiles, fixture_tiles, item_tiles - Copy-on-write semantics: Arc::make_mut clones only if other Arcs exist - Add insert_floor, insert_fixture, insert_item, remove_item methods - Add get_floor_mut, get_fixture_mut for visibility updates - Update all mutation sites to use new TileMap methods - Enables cheap Arc::clone for async pathfinding workers - Single-threaded pathfinding: no clone, direct access - Multi-threaded pathfinding: clone Arc, read without locks
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
use ahash::AHashMap;
|
||||
use bevy::prelude::*;
|
||||
use std::sync::Arc;
|
||||
|
||||
/// Packed floor tile data for efficient storage. ~35 bytes vs 76 bytes tuple.
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
@@ -170,12 +171,13 @@ impl FixtureTileData {
|
||||
}
|
||||
}
|
||||
|
||||
/// Tile map with fast AHashMap for pathfinding lookups.
|
||||
#[derive(Resource, Default)]
|
||||
/// Tile map with Arc-wrapped HashMaps for async pathfinding access.
|
||||
/// Uses copy-on-write: Arc::make_mut clones only if other Arcs exist.
|
||||
#[derive(Resource, Clone, Default)]
|
||||
pub struct TileMap {
|
||||
pub floor_tiles: AHashMap<IVec3, FloorTileData>,
|
||||
pub fixture_tiles: AHashMap<IVec3, FixtureTileData>,
|
||||
pub item_tiles: AHashMap<IVec3, Vec<u32>>,
|
||||
pub floor_tiles: Arc<AHashMap<IVec3, FloorTileData>>,
|
||||
pub fixture_tiles: Arc<AHashMap<IVec3, FixtureTileData>>,
|
||||
pub item_tiles: Arc<AHashMap<IVec3, Vec<u32>>>,
|
||||
}
|
||||
|
||||
impl TileMap {
|
||||
@@ -202,4 +204,37 @@ impl TileMap {
|
||||
pub fn has_fixture(&self, pos: &IVec3) -> bool {
|
||||
self.fixture_tiles.contains_key(pos)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn insert_floor(&mut self, pos: IVec3, tile: FloorTileData) {
|
||||
Arc::make_mut(&mut self.floor_tiles).insert(pos, tile);
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn insert_fixture(&mut self, pos: IVec3, tile: FixtureTileData) {
|
||||
Arc::make_mut(&mut self.fixture_tiles).insert(pos, tile);
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn insert_item(&mut self, pos: IVec3, entity_id: u32) {
|
||||
Arc::make_mut(&mut self.item_tiles)
|
||||
.entry(pos)
|
||||
.or_default()
|
||||
.push(entity_id);
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn remove_item(&mut self, pos: &IVec3) -> Option<Vec<u32>> {
|
||||
Arc::make_mut(&mut self.item_tiles).remove(pos)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn get_floor_mut(&mut self, pos: &IVec3) -> Option<&mut FloorTileData> {
|
||||
Arc::make_mut(&mut self.floor_tiles).get_mut(pos)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn get_fixture_mut(&mut self, pos: &IVec3) -> Option<&mut FixtureTileData> {
|
||||
Arc::make_mut(&mut self.fixture_tiles).get_mut(pos)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user