1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
//! ![](https://cloud.midgardhr.dev/index.php/s/Pz3bdsNntyHxGC4/preview)
//!
//! # Bevy Tilemap
//!
//! Bevy Tilemap allows for Bevy native batch-rendered tiles in maps to be
//! constructed with chunk based loading, efficiently.
//!
//! Simple yet refined in its implementation, it is meant to attach to other
//! extensible plugins that can enhance its functionality further. Hand-crafted
//! tilemaps with an attentive focus on performance, and low data usage.
//!
//! ## Features
//! * Perfect for game jams.
//! * Easy to use and stable API with thorough documentation.
//! * Endless or constrained tilemaps.
//! * Batched rendering of many tiles.
//!
//! ## Design
//! This is not intended to be just another Tile Map. It is meant to be a
//! framework and extensible by design, like Bevy. As well as work done to keep
//! it as close to Bevy API as possible while keeping in mind of Rust API best
//! practices. It is not meant to be complicated and created to be simple to use
//! but give enough functionality to advanced users.
//!
//! Less time fiddling, more time building.
//!
//! # Constructing a basic tilemap, setting tiles, and spawning.
//!
//! Bevy Tilemap makes it easy to quickly implement a tilemap if you are in a
//! rush or want to build a conceptual game.
//!
//! ```
//! use bevy_tilemap::prelude::*;
//! use bevy::asset::HandleId;
//! use bevy::prelude::*;
//!
//! // This must be set in Asset<TextureAtlas>.
//! let texture_atlas_handle = Handle::weak(HandleId::random::<TextureAtlas>());
//!
//! let mut tilemap = Tilemap::new(texture_atlas_handle);
//!
//! // Coordinate point with Z order.
//! let point = (16, 16, 0);
//! let tile_index = 0;
//! tilemap.set_tile(point, tile_index);
//!
//! tilemap.spawn_chunk_containing_point(point);
//! ```
//!
//! # Constructing a more advanced tilemap.
//!
//! For most cases, it is preferable to construct a tilemap with explicit
//! parameters. For that you would use a [`Builder`].
//!
//! [`Builder`]: crate::tilemap::Builder
//!
//! ```
//! use bevy_tilemap::prelude::*;
//! use bevy::asset::HandleId;
//! use bevy::prelude::*;
//!
//! // This must be set in Asset<TextureAtlas>.
//! let texture_atlas_handle = Handle::weak(HandleId::random::<TextureAtlas>());
//!
//! let mut tilemap = Tilemap::builder()
//!     .texture_atlas(texture_atlas_handle)
//!     .chunk_dimensions(64, 64)
//!     .tile_dimensions(8, 8)
//!     .dimensions(32, 32)
//!     .add_layer(LayerKind::Dense, 0)
//!     .add_layer(LayerKind::Sparse, 1)
//!     .add_layer(LayerKind::Sparse, 2)
//!     .z_layers(3)
//!     .build()
//!     .unwrap();
//! ```
//!
//! The above example outlines all the current possible builder methods. What is
//! neat is that if more layers are accidentally set than z_layer set, it will
//! use the layer length instead. Much more features are planned including
//! automated systems that will enhance the tilemap further.
//!
//! # Setting tiles
//!
//! There are two methods to set tiles in the tilemap. The first is single tiles
//! at a time which is acceptable for tiny updates such as moving around
//! characters. The second being bulk setting many tiles at once.
//!
//! If you expect to move multiple tiles a frame, **always** use the [`Tiles`]
//! map and set it with [`set_tiles`]. A single event is created with all
//! tiles if set this way.
//!
//! [`Tiles`]: crate::tile::Tiles
//! [`set_tiles`]: crate::tilemap::TileMap::set_tiles
//!
//! ```
//! use bevy_tilemap::prelude::*;
//! use bevy::asset::HandleId;
//! use bevy::prelude::*;
//!
//! // This must be set in Asset<TextureAtlas>.
//! let texture_atlas_handle = Handle::weak(HandleId::random::<TextureAtlas>());
//!
//! let mut tilemap = Tilemap::new(texture_atlas_handle);
//!
//! // Prefer this
//! let mut tiles = Tiles::default();
//! for y in 0..31 {
//!     for x in 0..31 {
//!         tiles.insert((x, y, 0), 0.into());
//!     }
//! }
//!
//! tilemap.set_tiles(tiles);
//!
//! // Over this...
//! for y in 0..31 {
//!     for x in 0..31 {
//!         tilemap.set_tile((x, y, 0), 0);
//!     }
//! }
//! ```
//!
//! # Serde support
//!
//! Optionally serde is supported through the use of features.
//!
//! ```toml
//! [dependencies]
//! bevy_tilemap = { version = "0.2", features = ["serde"] }
//! ```
#![doc(html_root_url = "https://docs.rs/bevy_tilemap/0.2.1")]
#![no_implicit_prelude]
// clippy
#![allow(clippy::too_many_arguments)]
// rustc
#![deny(dead_code, missing_docs, unused_imports)]

/// Chunk traits to implement for a custom chunk and a basic struct for use.
pub mod chunk;
/// Various dimension based traits.
mod dimension;
/// Bundles of components for spawning entities.
pub mod entity;
/// Meshes for use in rendering.
mod mesh;
/// Points used for helping with coordinates.
pub mod point;
pub mod prelude;
/// Files and helpers for rendering.
mod render;
/// Tile traits to implement for a custom tile.
pub mod tile;
/// Map traits to implement for a custom map and a basic struct for use.
pub mod tilemap;

use crate::{chunk::Chunk, lib::*, render::TilemapRenderGraphBuilder, tilemap::Tilemap};

/// The Bevy Tilemap main plugin.
#[derive(Default)]
pub struct ChunkTilesPlugin;

impl Plugin for ChunkTilesPlugin {
    fn build(&self, app: &mut AppBuilder) {
        app.add_asset::<Tilemap>()
            .add_asset::<Chunk>()
            .add_system_to_stage("post_update", crate::tilemap::map_system.system());

        let resources = app.resources_mut();
        let mut render_graph = resources
            .get_mut::<RenderGraph>()
            .expect("`RenderGraph` is missing.");
        render_graph.add_tilemap_graph(resources);
    }
}

/// A custom prelude around all the types we need from `std`, `bevy`, and `serde`.
mod lib {
    // Need to add this here as there is a Rust issue surrounding the fact that
    // bevy also uses `no_implicit_prelude`. Without this, it would complain
    // that I am not using `self`, and will refuse to build.
    // See: https://github.com/rust-lang/rust/issues/72381
    pub use ::bevy;
    use ::bevy::{
        app as bevy_app, asset as bevy_asset, core as bevy_core, ecs as bevy_ecs,
        math as bevy_math, render as bevy_render, sprite as bevy_sprite,
        transform as bevy_transform, type_registry as bevy_type_registry, utils as bevy_utils,
    };

    pub(crate) use self::{
        bevy_app::{AppBuilder, Events, Plugin},
        bevy_asset::{AddAsset, Assets, Handle, HandleId},
        bevy_core::Byteable,
        bevy_ecs::{Bundle, Commands, Entity, IntoQuerySystem, Query, ResMut, Resources},
        bevy_math::{Vec2, Vec3},
        bevy_render::{
            color::Color,
            draw::Draw,
            mesh::{Indices, Mesh},
            pipeline::{
                BlendDescriptor, BlendFactor, BlendOperation, ColorStateDescriptor, ColorWrite,
                CompareFunction, CullMode, DepthStencilStateDescriptor, DynamicBinding, FrontFace,
                PipelineDescriptor, RasterizationStateDescriptor, RenderPipeline, RenderPipelines,
                StencilStateDescriptor, StencilStateFaceDescriptor,
            },
            render_graph::{base::MainPass, RenderGraph, RenderResourcesNode},
            renderer::{RenderResource, RenderResources},
            shader::{Shader, ShaderStage, ShaderStages},
            texture::TextureFormat,
        },
        bevy_sprite::TextureAtlas,
        bevy_transform::{
            components::{GlobalTransform, Transform},
            hierarchy::BuildChildren,
        },
        bevy_type_registry::{TypeUuid, Uuid},
        bevy_utils::HashMap,
    };

    // Need to add this here as there is a Rust issue surrounding the fact that
    // serde also uses `no_implicit_prelude`. Without this, it would complain
    // that I am not using `self`, and will refuse to build.
    // See: https://github.com/rust-lang/rust/issues/72381
    #[cfg(feature = "serde")]
    pub use ::serde;
    #[cfg(feature = "serde")]
    pub(crate) use ::serde::{Deserialize, Serialize};

    pub(crate) use ::std::{
        self,
        boxed::Box,
        clone::Clone,
        cmp::Ord,
        convert::{AsMut, AsRef, From, Into},
        default::Default,
        error::Error,
        fmt::{Debug, Display, Formatter, Result as FmtResult},
        iter::{Extend, IntoIterator, Iterator},
        ops::{Add, AddAssign, Div, DivAssign, FnMut, FnOnce, Mul, MulAssign, Neg, Sub, SubAssign},
        option::Option::{self, *},
        result::Result::{self, *},
        vec::Vec,
    };

    // Macros
    pub(crate) use ::std::{panic, vec, write};
}