Add reflect and documentation

This commit is contained in:
Conner Petzold 2025-07-16 19:20:23 -04:00
parent c0f8d7a555
commit 3e08728e77
2 changed files with 28 additions and 14 deletions

View File

@ -8,6 +8,7 @@ use bevy_ecs::{
entity::Entity, entity::Entity,
lifecycle::HookContext, lifecycle::HookContext,
query::Changed, query::Changed,
reflect::{ReflectComponent, ReflectResource},
resource::Resource, resource::Resource,
system::{Query, ResMut}, system::{Query, ResMut},
world::DeferredWorld, world::DeferredWorld,
@ -15,6 +16,7 @@ use bevy_ecs::{
use bevy_image::Image; use bevy_image::Image;
use bevy_math::{primitives::Rectangle, UVec2}; use bevy_math::{primitives::Rectangle, UVec2};
use bevy_platform::collections::HashMap; use bevy_platform::collections::HashMap;
use bevy_reflect::{prelude::*, Reflect};
use bevy_render::mesh::{Mesh, Mesh2d}; use bevy_render::mesh::{Mesh, Mesh2d};
use bevy_utils::default; use bevy_utils::default;
use tracing::warn; use tracing::warn;
@ -29,42 +31,53 @@ pub struct TilemapChunkPlugin;
impl Plugin for TilemapChunkPlugin { impl Plugin for TilemapChunkPlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.init_resource::<TilemapChunkMeshCache>() app.register_type::<TilemapChunkMeshCache>()
.register_type::<TilemapChunk>()
.register_type::<TilemapChunkTileData>()
.init_resource::<TilemapChunkMeshCache>()
.add_systems(Update, update_tilemap_chunk_indices); .add_systems(Update, update_tilemap_chunk_indices);
} }
} }
/// A resource storing the meshes for each tilemap chunk size. /// A resource storing the meshes for each tilemap chunk size.
#[derive(Resource, Default, Deref, DerefMut)] #[derive(Resource, Default, Deref, DerefMut, Reflect)]
#[reflect(Resource, Default)]
pub struct TilemapChunkMeshCache(HashMap<UVec2, Handle<Mesh>>); pub struct TilemapChunkMeshCache(HashMap<UVec2, Handle<Mesh>>);
/// A component representing a chunk of a tilemap. /// A component representing a chunk of a tilemap.
/// Each chunk is a rectangular section of tiles that is rendered as a single mesh. /// Each chunk is a rectangular section of tiles that is rendered as a single mesh.
#[derive(Component, Clone, Debug, Default)] #[derive(Component, Clone, Debug, Default, Reflect)]
#[reflect(Component, Clone, Debug, Default)]
#[component(immutable, on_insert = on_insert_tilemap_chunk)] #[component(immutable, on_insert = on_insert_tilemap_chunk)]
pub struct TilemapChunk { pub struct TilemapChunk {
/// The size of the chunk in tiles /// The size of the chunk in tiles.
pub chunk_size: UVec2, pub chunk_size: UVec2,
/// The size to use for each tile, not to be confused with the size of a tile in the tileset image. /// The size to use for each tile, not to be confused with the size of a tile in the tileset image.
/// The size of the tile in the tileset image is determined by the tileset image's dimensions. /// The size of the tile in the tileset image is determined by the tileset image's dimensions.
pub tile_display_size: UVec2, pub tile_display_size: UVec2,
/// Handle to the tileset image containing all tile textures /// Handle to the tileset image containing all tile textures.
pub tileset: Handle<Image>, pub tileset: Handle<Image>,
/// The alpha mode to use for the tilemap chunk /// The alpha mode to use for the tilemap chunk.
pub alpha_mode: AlphaMode2d, pub alpha_mode: AlphaMode2d,
} }
#[derive(Clone, Copy, Debug)] /// Data for a single tile in the tilemap chunk.
#[derive(Clone, Copy, Debug, Reflect)]
#[reflect(Clone, Debug, Default)]
pub struct TileData { pub struct TileData {
/// The index of the tile in the corresponding tileset array texture.
pub tileset_index: u16, pub tileset_index: u16,
/// The color tint of the tile. White leaves the sampled texture color unchanged.
pub color: Color, pub color: Color,
/// The visibility of the tile.
pub visible: bool, pub visible: bool,
} }
impl TileData { impl TileData {
pub fn from_index(index: u16) -> Self { /// Creates a new `TileData` with the given tileset index and default values.
pub fn from_tileset_index(tileset_index: u16) -> Self {
Self { Self {
tileset_index: index, tileset_index,
..default() ..default()
} }
} }
@ -80,9 +93,10 @@ impl Default for TileData {
} }
} }
/// Component storing the indices of tiles within a chunk. /// Component storing the data of tiles within a chunk.
/// Each index corresponds to a specific tile in the tileset. /// Each index corresponds to a specific tile in the tileset. `None` indicates an empty tile.
#[derive(Component, Clone, Debug, Deref, DerefMut)] #[derive(Component, Clone, Debug, Deref, DerefMut, Reflect)]
#[reflect(Component, Clone, Debug)]
pub struct TilemapChunkTileData(pub Vec<Option<TileData>>); pub struct TilemapChunkTileData(pub Vec<Option<TileData>>);
fn on_insert_tilemap_chunk(mut world: DeferredWorld, HookContext { entity, .. }: HookContext) { fn on_insert_tilemap_chunk(mut world: DeferredWorld, HookContext { entity, .. }: HookContext) {

View File

@ -34,7 +34,7 @@ fn setup(mut commands: Commands, assets: Res<AssetServer>) {
if i == 0 { if i == 0 {
None None
} else { } else {
Some(TileData::from_index(i - 1)) Some(TileData::from_tileset_index(i - 1))
} }
}) })
.collect(); .collect();
@ -80,7 +80,7 @@ fn update_tilemap(
if timer.just_finished() { if timer.just_finished() {
for _ in 0..50 { for _ in 0..50 {
let index = rng.gen_range(0..tile_data.len()); let index = rng.gen_range(0..tile_data.len());
tile_data[index] = Some(TileData::from_index(rng.gen_range(0..5))); tile_data[index] = Some(TileData::from_tileset_index(rng.gen_range(0..5)));
} }
} }
} }