From df5583bb105a27136b5614b12e9e3fc74cf0d8e8 Mon Sep 17 00:00:00 2001 From: StarArawn Date: Sun, 16 May 2021 11:32:01 -0400 Subject: [PATCH 01/23] Working on fixing iso uv's --- src/render/diamondiso-tilemap.vert | 4 ++-- src/render/staggerediso-tilemap.vert | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/render/diamondiso-tilemap.vert b/src/render/diamondiso-tilemap.vert index 875121a6..ddfd634e 100644 --- a/src/render/diamondiso-tilemap.vert +++ b/src/render/diamondiso-tilemap.vert @@ -54,9 +54,9 @@ void main() { int texture_index = int(current_animation_frame); - int columns = int(floor(texture_size.x / tile_size.x)); + int columns = int(texture_size.x) / int(tile_size.x); - float sprite_sheet_x = floor(mod(float(texture_index), float(columns)) * (tile_size.x + spacing.x) - spacing.x); + float sprite_sheet_x = floor(float(texture_index % columns)) * (tile_size.x + spacing.x) - spacing.x; float sprite_sheet_y = floor((texture_index / columns)) * (tile_size.y + spacing.y) - spacing.y; float start_u = sprite_sheet_x / texture_size.x; diff --git a/src/render/staggerediso-tilemap.vert b/src/render/staggerediso-tilemap.vert index 414c32bc..811fdecc 100644 --- a/src/render/staggerediso-tilemap.vert +++ b/src/render/staggerediso-tilemap.vert @@ -57,9 +57,9 @@ void main() { int texture_index = int(current_animation_frame); - int columns = int(floor(texture_size.x / tile_size.x)); + int columns = int(texture_size.x) / int(tile_size.x); - float sprite_sheet_x = floor(mod(float(texture_index), float(columns)) * (tile_size.x + spacing.x) - spacing.x); + float sprite_sheet_x = floor(float(texture_index % columns)) * (tile_size.x + spacing.x) - spacing.x; float sprite_sheet_y = floor((texture_index / columns)) * (tile_size.y + spacing.y) - spacing.y; float start_u = sprite_sheet_x / texture_size.x; From 2a68802c2f050903755921522ab4728bd23005b0 Mon Sep 17 00:00:00 2001 From: StarArawn Date: Wed, 19 May 2021 12:25:20 -0400 Subject: [PATCH 02/23] A new interface better suited for editing tiles. --- examples/accessing_tiles.rs | 106 ++++--- examples/animation.rs | 55 ++-- examples/bench.rs | 31 +-- examples/dynamic_map.rs | 57 ++-- examples/game_of_life.rs | 54 ++-- examples/hex_column.rs | 48 ++-- examples/hex_row.rs | 48 +--- examples/iso_diamond.rs | 57 ++-- examples/iso_staggered.rs | 57 ++-- examples/layers.rs | 57 ++-- examples/map.rs | 34 +-- examples/random_map.rs | 74 ++--- examples/remove_tiles.rs | 54 ++-- examples/sparse_tiles.rs | 42 ++- examples/visibility.rs | 54 ++-- src/chunk.rs | 71 +++-- src/layer.rs | 154 +++++++++++ src/layer_builder.rs | 255 +++++++++++++++++ src/lib.rs | 56 ++-- src/map.rs | 534 ------------------------------------ src/map_query.rs | 255 +++++++++++++++++ src/tile.rs | 43 ++- 22 files changed, 1110 insertions(+), 1086 deletions(-) create mode 100644 src/layer.rs create mode 100644 src/layer_builder.rs delete mode 100644 src/map.rs create mode 100644 src/map_query.rs diff --git a/examples/accessing_tiles.rs b/examples/accessing_tiles.rs index 3039f53b..bd6bf639 100644 --- a/examples/accessing_tiles.rs +++ b/examples/accessing_tiles.rs @@ -9,8 +9,8 @@ struct LastUpdate(f64); fn startup( mut commands: Commands, asset_server: Res, - mut meshes: ResMut>, mut materials: ResMut>, + mut map_query: MapQuery, ) { commands.spawn_bundle(OrthographicCameraBundle { transform: Transform::from_xyz(1024.0, 1024.0, 1000.0 - 0.1), @@ -20,63 +20,81 @@ fn startup( let texture_handle = asset_server.load("tiles.png"); let material_handle = materials.add(ColorMaterial::texture(texture_handle)); - let mut map = Map::new(MapSettings::new( - UVec2::new(4, 4), - UVec2::new(32, 32), - Vec2::new(16.0, 16.0), - Vec2::new(96.0, 256.0), - 0, - )); - let map_entity = commands.spawn().id(); - map.build( + // We can create maps by using the LayerBuilder + // LayerBuilder creates the tile entities and makes sure they are attached to chunks correctly. + // It also provides a way of accessing and viewing tiles during the creation phase. + // Once a LayerBuilder is passed to the map_query.create_layer function it is consumed and + // can no longer be accessed. + // Layer builder accepts a generic bundle that must implement the `TileBundleTrait`. + // This is used internally to access the tile in the bundle to attach the chunk correctly. + + // Layer Entity + let layer_entity = commands.spawn().id(); + + // Create the layer builder + let mut layer_builder = LayerBuilder::::new( &mut commands, - &mut meshes, - material_handle, - map_entity, - true, + layer_entity, + LayerSettings::new( + UVec2::new(4, 4), + UVec2::new(32, 32), + Vec2::new(16.0, 16.0), + Vec2::new(96.0, 256.0), + ) ); - assert!(map.get_tile(IVec2::new(0, 0)).is_some()); - assert!(map.get_tile_neighbors(UVec2::new(2, 2)).len() == 8); + // We can easily fill the entire map by using set_all + layer_builder.set_all(Tile::default().into(), true); + + // You can also fill in a portion of the map + layer_builder.fill(UVec2::new(0, 0), UVec2::new(10, 10), Tile { texture_index: 1, ..Default::default() }.into(), true); + + let neighbors = layer_builder.get_tile_neighbors(UVec2::new(0, 0)); + + // We can access tiles like normal using: + assert!(layer_builder.get_tile(UVec2::new(0, 0)).is_ok()); + assert!(neighbors.len() == 8); + assert!(neighbors.iter().filter(|n| n.1.is_some()).count() == 3); // Only 3 neighbors since negative is outside of map. let mut color = 0; for x in (2..128).step_by(4) { color += 1; for y in (2..128).step_by(4) { - let neighbors = map.get_tile_neighbors(UVec2::new(x, y)); - for (pos, _neighbor) in neighbors.iter() { - let _ = map.add_tile( - &mut commands, + // Grabbing neighbors is easy. + let neighbors: Vec = layer_builder.get_tile_neighbors(UVec2::new(x, y)).iter().map(|(pos, _)| *pos).collect(); + for pos in neighbors.iter() { + // We can set specific tiles like this: + let _ = layer_builder.set_tile( UVec2::new(pos.x as u32, pos.y as u32), Tile { texture_index: color, ..Default::default() - }, + }.into(), true, ); } } } + // Once create_layer is called you can no longer access the tiles in this system. + map_query.create_layer(&mut commands, layer_builder, material_handle); + commands - .entity(map_entity) - .insert_bundle(MapBundle { - map, - ..Default::default() - }) + .entity(layer_entity) .insert(CurrentColor(1)) .insert(LastUpdate(0.0)); } // Should run after the commands from startup have been processed. -// An example of a slow way of editing tiles.. +// An example of how to manipulate tiles. fn update_map( - mut commands: Commands, time: Res