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

Implement MapEntities #496

Merged
merged 1 commit into from
Dec 17, 2023
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
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