Skip to content

Commit

Permalink
Merge pull request #496 from hankjordan/mapentities
Browse files Browse the repository at this point in the history
Implement `MapEntities`
  • Loading branch information
StarArawn authored Dec 17, 2023
2 parents 5db167e + 23d8bb0 commit f1f5dc7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
10 changes: 9 additions & 1 deletion src/map/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use bevy::asset::Assets;
use bevy::ecs::entity::{EntityMapper, MapEntities};
use bevy::ecs::reflect::ReflectMapEntities;
use bevy::prelude::{ReflectComponent, Res, ResMut, Resource};
use bevy::render::render_resource::TextureUsages;
use bevy::{
Expand Down Expand Up @@ -41,9 +43,15 @@ pub struct TilemapRenderSettings {

/// A component which stores a reference to the tilemap entity.
#[derive(Component, Reflect, Clone, Copy, Debug, Hash)]
#[reflect(Component)]
#[reflect(Component, MapEntities)]
pub struct TilemapId(pub Entity);

impl MapEntities for TilemapId {
fn map_entities(&mut self, entity_mapper: &mut EntityMapper) {
self.0 = entity_mapper.get_or_reserve(self.0);
}
}

impl Default for TilemapId {
fn default() -> Self {
Self(Entity::from_raw(0))
Expand Down
18 changes: 16 additions & 2 deletions src/tiles/storage.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
use bevy::prelude::*;
use bevy::{
ecs::{
entity::{EntityMapper, MapEntities},
reflect::ReflectMapEntities,
},
prelude::*,
};

use crate::map::TilemapSize;

Expand All @@ -7,12 +13,20 @@ use super::TilePos;
/// Used to store tile entities for fast look up.
/// Tile entities are stored in a grid. The grid is always filled with None.
#[derive(Component, Reflect, Default, Debug, Clone)]
#[reflect(Component)]
#[reflect(Component, MapEntities)]
pub struct TileStorage {
tiles: Vec<Option<Entity>>,
pub size: TilemapSize,
}

impl MapEntities for TileStorage {
fn map_entities(&mut self, entity_mapper: &mut EntityMapper) {
for entity in self.tiles.iter_mut().flatten() {
*entity = entity_mapper.get_or_reserve(*entity);
}
}
}

impl TileStorage {
/// Creates a new tile storage that is empty.
pub fn empty(size: TilemapSize) -> Self {
Expand Down

0 comments on commit f1f5dc7

Please sign in to comment.