Skip to content
This repository has been archived by the owner on May 7, 2022. It is now read-only.

Add sparse and dense layers #16

Merged
merged 54 commits into from
Nov 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
c9620ec
Workable overhaul implementing layers
joshuajbouw Nov 18, 2020
2748175
Deprecate `new_chunk_with_tiles` api
joshuajbouw Nov 18, 2020
2e0cdc7
Rename `z` to `z_layer` to be clear
joshuajbouw Nov 18, 2020
6af084e
Add `set_mesh` `set_tile` api to `Chunk`
joshuajbouw Nov 18, 2020
b6611ee
Change `Handle<Chunk>` storage from `Vec` to `HashMap`
joshuajbouw Nov 18, 2020
011d9e9
Fix len error as no chunk handle has been added
joshuajbouw Nov 18, 2020
dbf9ce8
Update deprecated `new_chunk_with_tiles` method message
joshuajbouw Nov 18, 2020
2465baa
Add more imports to prelude
joshuajbouw Nov 18, 2020
3cc95ee
Add coord to Chunk
joshuajbouw Nov 18, 2020
0ac4ddc
Move tiles into where its needed
joshuajbouw Nov 18, 2020
283ebdd
impl `DeRefMut` for `LayerKindInner`
joshuajbouw Nov 18, 2020
1512b6b
Fix dwarf sprite partial render bug
joshuajbouw Nov 18, 2020
8b20356
Update `random_dungeon` example
joshuajbouw Nov 18, 2020
ae2be41
Change `Deref` and `DerefMut` to `AsRef` and `AsMut`
joshuajbouw Nov 18, 2020
fb2b991
cargo fmt
joshuajbouw Nov 18, 2020
c1cfe4b
Add points
joshuajbouw Nov 19, 2020
4f596d0
Rename `dense_tile_to_renderer_parts` to `dense_tiles_to_attributes`
joshuajbouw Nov 19, 2020
b778da8
Make `dimensions` private and change, move old API to `deprecated`
joshuajbouw Nov 19, 2020
16831ee
Deprecate `tile_setter`
joshuajbouw Nov 19, 2020
78314e2
Deprecate coord
joshuajbouw Nov 19, 2020
8f3d0ed
cargo fmt
joshuajbouw Nov 19, 2020
fce4a1c
Add Tiles
joshuajbouw Nov 19, 2020
24cb2b8
Add 0_2 prelude, deprecate 0_1
joshuajbouw Nov 19, 2020
6bc1be7
Rename `map` module to `tilemap`
joshuajbouw Nov 19, 2020
5ab17ce
Rename `TileMap` to `Tilemap`
joshuajbouw Nov 19, 2020
4ac7925
Add `v0_1` prelude optional feature
joshuajbouw Nov 19, 2020
6a50125
Add derives
joshuajbouw Nov 19, 2020
0d9fc1f
Remove pointless refs from dimensions
joshuajbouw Nov 19, 2020
1a2bc7e
Remove deprecations
joshuajbouw Nov 19, 2020
1f04c04
Clean up, documentation, fixes, dimension rename
joshuajbouw Nov 19, 2020
d7cdefd
Add default for Tilemap
joshuajbouw Nov 19, 2020
c1e2dfc
Remove unneeded fields from Tilemap
joshuajbouw Nov 19, 2020
28a70fb
TileMap -> Tilemap, tile_map -> tilemap
joshuajbouw Nov 19, 2020
cde247a
pub(crate) dimensions
joshuajbouw Nov 19, 2020
d6fd4d2
pub(crate) point
joshuajbouw Nov 19, 2020
8e39a97
tilemap docs
joshuajbouw Nov 19, 2020
0058ca3
cargo fmt
joshuajbouw Nov 19, 2020
0c55bc6
Add attributes
joshuajbouw Nov 19, 2020
52c3ee8
Replace unwraps with expect
joshuajbouw Nov 19, 2020
70a858a
Add common traits
joshuajbouw Nov 19, 2020
f947f2e
Add serde
joshuajbouw Nov 19, 2020
cd9f5a9
Add Error impl
joshuajbouw Nov 19, 2020
e6b5761
Add Errors docs
joshuajbouw Nov 19, 2020
3039baa
Improve borrowing, replace most iter with drain or into_iter
joshuajbouw Nov 19, 2020
5d380cc
Make point public API, impl Into points
joshuajbouw Nov 19, 2020
d65863e
Dimension cleanup
joshuajbouw Nov 20, 2020
02bfbf6
Overhaul point with impl
joshuajbouw Nov 20, 2020
0377bf9
Add `add_layer` to tilemap `Builder`
joshuajbouw Nov 20, 2020
553e25c
Remove commented out code
joshuajbouw Nov 20, 2020
6bcfd25
Add Tile impl Into
joshuajbouw Nov 20, 2020
d69a68c
Add more impl to Point2
joshuajbouw Nov 20, 2020
d1b260d
Add more tilemap docs
joshuajbouw Nov 20, 2020
1327ca7
Add crate level docs
joshuajbouw Nov 20, 2020
660773a
Remove default features
joshuajbouw Nov 20, 2020
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
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ keywords = ["game", "engine", "bevy", "tiles", "tilemap"]
categories = ["game-engines", "graphics", "gui"]
readme = "README.md"
documentation = "https://docs.rs/bevy_tilemap/latest/bevy_tilemap/"
exclude = ["assets/**/*", ".github/**/*"]
exclude = ["assets/**/*", ".github/**/*", "doc/**/*"]

[dependencies]
bevy = { version = "0.3", features = ["default"] }
bevy = { version = "0.3" }
serde = { version = "1.0", optional = true, features = ["derive"] }

[dev-dependencies]
Expand Down
Binary file modified assets/textures/dwarf.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
112 changes: 59 additions & 53 deletions examples/random_dungeon.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
use bevy::{asset::LoadState, prelude::*, sprite::TextureAtlasBuilder, window::WindowMode};
use bevy_tilemap::{
dimensions::{Dimensions2, Dimensions3},
map::{TileMap, TileMapComponents},
tile::{Tile, TileSetter},
ChunkTilesPlugin,
use bevy::{
asset::LoadState, ecs::bevy_utils::HashMapExt, prelude::*, sprite::TextureAtlasBuilder,
window::WindowMode,
};
use bevy_tilemap::prelude::*;
use rand::Rng;

#[derive(Default, Clone)]
Expand All @@ -16,6 +14,7 @@ pub struct TileSpriteHandles {
#[derive(Default, Clone)]
pub struct MapState {
map_loaded: bool,
spawned: bool,
}

fn setup(
Expand Down Expand Up @@ -52,103 +51,110 @@ fn load(
let texture_atlas = texture_atlas_builder.finish(&mut textures).unwrap();
let atlas_handle = texture_atlases.add(texture_atlas);

let tile_dimensions = Vec2::new(32., 32.);
let chunk_dimensions = Vec3::new(32., 32., 0.);
let tile_map_dimensions = Vec2::new(1., 1.);
let tile_map = TileMap::new(
tile_map_dimensions,
chunk_dimensions,
tile_dimensions,
atlas_handle,
);

let tile_map_components = TileMapComponents {
tile_map,
let tilemap = Tilemap::builder()
.dimensions(1, 1)
.chunk_dimensions(32, 32)
.tile_dimensions(32, 32)
.z_layers(2)
.texture_atlas(atlas_handle)
.build()
.unwrap();

let tilemap_components = TilemapComponents {
tilemap,
transform: Default::default(),
global_transform: Default::default(),
};

commands.spawn(tile_map_components);
commands.spawn(tilemap_components);

sprite_handles.atlas_loaded = true;
}
}

fn build_random_dungeon(
mut map_state: ResMut<MapState>,
mut textures: ResMut<Assets<Texture>>,
texture_atlases: Res<Assets<TextureAtlas>>,
asset_server: Res<AssetServer>,
mut query: Query<&mut TileMap>,
mut query: Query<&mut Tilemap>,
) {
if map_state.map_loaded {
return;
}

for mut map in query.iter_mut() {
for y in 0..map.dimensions().x() as i32 {
for x in 0..map.dimensions().y() as i32 {
let coord = Vec2::new(x as f32, y as f32);
map.new_chunk(coord).unwrap();
for y in 0..map.height().unwrap() as i32 {
for x in 0..map.height().unwrap() as i32 {
map.new_chunk((x, y)).unwrap();
}
}

let width = map.dimensions().width() * map.chunk_dimensions().width();
let height = map.dimensions().height() * map.chunk_dimensions().height();
let width = (map.width().unwrap() * map.chunk_width()) as i32;
let height = (map.height().unwrap() * map.chunk_height()) as i32;

// Then we need to find out what the handles were to our textures we are going to use.
let mut floor_sprite: Handle<Texture> = asset_server.get_handle("textures/tile_floor.png");
let mut wall_sprite: Handle<Texture> = asset_server.get_handle("textures/tile_wall.png");
floor_sprite.make_strong(&mut textures);
wall_sprite.make_strong(&mut textures);

let floor_sprite: Handle<Texture> = asset_server.get_handle("textures/tile_floor.png");
let wall_sprite: Handle<Texture> = asset_server.get_handle("textures/tile_wall.png");
let texture_atlas = texture_atlases.get(map.texture_atlas()).unwrap();
let floor_idx = texture_atlas.get_texture_index(&floor_sprite).unwrap();
let wall_idx = texture_atlas.get_texture_index(&wall_sprite).unwrap();
let floor_tile = Tile::new(floor_idx);
let wall_tile = Tile::new(wall_idx);

// We must use the new handy `TileSetter` tool which is a wrapped `Vec`
let mut setter = TileSetter::with_capacity((height * width) as usize);
for y in 0..(height as i32) {
for x in 0..(width as i32) {
setter.push(Vec3::new(x as f32, y as f32, 0.), floor_tile);
// We must use the new handy `Tiles` tool which is a wrapped `Vec`
let mut tiles = Tiles::with_capacity((height * width) as usize);
for y in 0..height {
for x in 0..width {
tiles.insert((x, y, 0), floor_tile);
}
}
// Then we push in all wall tiles on the X axis.
for x in 0..(width as i32) {
setter.push(Vec3::new(x as f32, 0., 0.), wall_tile);
setter.push(Vec3::new(x as f32, height - 1., 0.), wall_tile);
for x in 0..width {
tiles.insert((x, 0, 0), wall_tile);
tiles.insert((x, height - 1, 0), wall_tile);
}
// Then the wall tiles on the Y axis.
for y in 0..(height as i32) {
setter.push(Vec3::new(0., y as f32, 0.), wall_tile);
setter.push(Vec3::new(width - 1., y as f32, 0.), wall_tile);
for y in 0..height {
tiles.insert((0, y, 0), wall_tile);
tiles.insert((width - 1, y, 0), wall_tile);
}
// Lets just generate some random walls to sparsely place around the dungeon!
let range = (width * height) as usize / 5;
let mut rng = rand::thread_rng();
for _ in 0..range {
let x = rng.gen_range(1, width as i32);
let y = rng.gen_range(1, height as i32);
let coord = Vec3::new(x as f32, y as f32, 0.);
if coord != Vec3::new(width as f32 / 2., height as f32 / 2., 0.) {
setter.push(Vec3::new(x as f32, y as f32, 0.), wall_tile);
let coord = (x, y, 0i32);
if coord != (width / 2, height / 2, 0) {
tiles.insert((x, y, 0), wall_tile);
}
}

// Lets do the same as the above, but lets add in a dwarf friend!
let mut dwarf_sprite: Handle<Texture> = asset_server.get_handle("textures/dwarf.png");
dwarf_sprite.make_strong(&mut textures);

// The above should give us a neat little randomized dungeon! However,
// we are missing a hero! First, we need to add a layer. We must make
// this layer `Sparse` else we will lose efficiency with our data!
//
// You might've noticed that we didn't create a layer for z_layer 0 but
// yet it still works and exists. By default if a layer doesn't exist
// and tiles need to be written there then a Dense layer is created
// automatically.
map.add_layer_with_kind(LayerKind::Sparse, 1).unwrap();

// Now lets add in a dwarf friend!
let dwarf_sprite: Handle<Texture> = asset_server.get_handle("textures/dwarf.png");
let dwarf_idx = texture_atlas.get_texture_index(&dwarf_sprite).unwrap();
let dwarf_tile = Tile::new(dwarf_idx);
setter.push(
Vec3::new(width as f32 / 2., height as f32 / 2., 0.),
tiles.insert(
(width / 2, height / 2, 1), // Do note that we are pushing him onto z_layer 1 now!
dwarf_tile,
);

map.set_tiles(setter).unwrap();
// Now we pass all the tiles to our map.
map.set_tiles(&mut tiles).unwrap();

// Finally we spawn the chunk! In actual use this should be done in a
// spawn system.
map.spawn_chunk((0, 0)).unwrap();

map_state.map_loaded = true;
}
Expand Down
Loading