Skip to content

Commit

Permalink
Merge pull request #356 from rparrett/examples-cleanup
Browse files Browse the repository at this point in the history
Examples cleanup
  • Loading branch information
bzm3r authored Dec 18, 2022
2 parents f909d8c + b4a2eb1 commit 56e48a2
Show file tree
Hide file tree
Showing 30 changed files with 483 additions and 484 deletions.
6 changes: 1 addition & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,4 @@ features = [
"x11",
"bevy_text",
"bevy_sprite",
]

[[example]]
name = "ldtk"
path = "examples/ldtk/ldtk_usage.rs"
]
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ fn update_damage(
- [`iso_diamond`](examples/iso_diamond.rs) - An isometric meshed map using diamond ordering.
- [`iso_staggered`](examples/iso_staggered.rs) - An isometric meshed map using staggered ordering.
- [`layers`](examples/layers.rs) - An example of how you can use multiple map entities/components for “layers”.
- [`ldtk`](examples/ldtk/ldtk.rs) - An example of loading and rendering of a LDTK map. Use: `cargo run --example ldtk`. We recommend checking out: [`bevy_ecs_ldtk`](https://crates.io/crates/bevy_ecs_ldtk).
- [`ldtk`](examples/ldtk.rs) - An example of loading and rendering of a LDTK map. Use: `cargo run --example ldtk`. We recommend checking out: [`bevy_ecs_ldtk`](https://crates.io/crates/bevy_ecs_ldtk).
- [`random_map`](examples/random_map.rs) - A bench of editing all of the tiles every 100 ms.
- [`remove_tiles`](examples/remove_tiles.rs) - An example showing how you can remove tiles by using map_query
- [`tiled_rotated`](examples/tiled_rotated.rs) - An example of loading and rendering of a tiled map editor map with flipping and rotation. Use: `cargo run --example tiled_rotated`
Expand Down
14 changes: 6 additions & 8 deletions examples/accessing_tiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,8 @@ fn startup(mut commands: Commands, asset_server: Res<AssetServer>) {

// Spawns a tilemap.
// Once the tile storage is inserted onto the tilemap entity it can no longer be accessed.
commands
.entity(tilemap_entity)
.insert(TilemapBundle {
commands.entity(tilemap_entity).insert((
TilemapBundle {
grid_size,
size: map_size,
storage: tile_storage,
Expand All @@ -93,9 +92,10 @@ fn startup(mut commands: Commands, asset_server: Res<AssetServer>) {
tile_size,
transform: get_tilemap_center_transform(&map_size, &grid_size, &map_type, 0.0),
..Default::default()
})
.insert(LastUpdate(0.0))
.insert(CurrentColor(1));
},
LastUpdate(0.0),
CurrentColor(1),
));
}

// A system that manipulates tile colors.
Expand Down Expand Up @@ -153,8 +153,6 @@ fn main() {
DefaultPlugins
.set(WindowPlugin {
window: WindowDescriptor {
width: 1270.0,
height: 720.0,
title: String::from("Accessing Tiles Example"),
..Default::default()
},
Expand Down
47 changes: 23 additions & 24 deletions examples/animation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,54 +69,55 @@ fn create_animated_flowers(mut commands: Commands, asset_server: Res<AssetServer
let texture_handle: Handle<Image> = asset_server.load(FLOWERS);

let TilemapMetadata {
size,
size: map_size,
grid_size,
tile_size,
} = FLOWERS_METADATA;

let mut tile_storage = TileStorage::empty(size);
let mut tile_storage = TileStorage::empty(map_size);

let tilemap_entity = commands.spawn_empty().id();

// Choose 10 random tiles to contain flowers.
let mut rng = thread_rng();
let mut indices: Vec<(u32, u32)> = Vec::with_capacity((size.x * size.y) as usize);
for x in 0..size.x {
for y in 0..size.y {
let mut indices: Vec<(u32, u32)> = Vec::with_capacity((map_size.x * map_size.y) as usize);
for x in 0..map_size.x {
for y in 0..map_size.y {
indices.push((x, y));
}
}
for (x, y) in indices.into_iter().choose_multiple(&mut rng, 10) {
let tile_pos = TilePos { x, y };
let tile_entity = commands
.spawn_empty()
.insert(TileBundle {
position: tile_pos,
tilemap_id: TilemapId(tilemap_entity),
texture_index: TileTextureIndex(0),
..Default::default()
})
.spawn((
TileBundle {
position: tile_pos,
tilemap_id: TilemapId(tilemap_entity),
texture_index: TileTextureIndex(0),
..Default::default()
},
// To enable animation, we must insert the `AnimatedTile` component on
// each tile that is to be animated.
AnimatedTile {
start: 0,
end: 13,
speed: 0.95,
},
))
.id();
tile_storage.set(&tile_pos, tile_entity);

// To enable animation, we must insert the `AnimatedTile` component on
// each tile that is to be animated.
commands.entity(tile_entity).insert(AnimatedTile {
start: 0,
end: 13,
speed: 0.95,
});
tile_storage.set(&tile_pos, tile_entity);
}
let map_type = TilemapType::Square;

commands.entity(tilemap_entity).insert(TilemapBundle {
size,
size: map_size,
grid_size,
map_type,
tile_size,
storage: tile_storage,
texture: TilemapTexture::Single(texture_handle),
transform: get_tilemap_center_transform(&size, &grid_size, &map_type, 1.0),
transform: get_tilemap_center_transform(&map_size, &grid_size, &map_type, 1.0),
..Default::default()
});
}
Expand All @@ -131,8 +132,6 @@ fn main() {
DefaultPlugins
.set(WindowPlugin {
window: WindowDescriptor {
width: 1270.0,
height: 720.0,
title: String::from("Animated Map Example"),
..Default::default()
},
Expand Down
14 changes: 6 additions & 8 deletions examples/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ fn startup(

let texture_handle: Handle<Image> = asset_server.load("tiles.png");

let tilemap_size = TilemapSize { x: 32, y: 32 };
let map_size = TilemapSize { x: 32, y: 32 };

// Create a tilemap entity a little early.
// We want this entity early because we need to tell each tile which tilemap entity
Expand All @@ -27,12 +27,12 @@ fn startup(
// This component is a grid of tile entities and is used to help keep track of individual
// tiles in the world. If you have multiple layers of tiles you would have a tilemap entity
// per layer, each with their own `TileStorage` component.
let mut tile_storage = TileStorage::empty(tilemap_size);
let mut tile_storage = TileStorage::empty(map_size);

// Spawn the elements of the tilemap.
// Alternatively, you can use helpers::filling::fill_tilemap.
for x in 0..tilemap_size.x {
for y in 0..tilemap_size.y {
for x in 0..map_size.x {
for y in 0..map_size.y {
let tile_pos = TilePos { x, y };
let tile_entity = commands
.spawn(TileBundle {
Expand All @@ -52,11 +52,11 @@ fn startup(
commands.entity(tilemap_entity).insert(TilemapBundle {
grid_size,
map_type,
size: tilemap_size,
size: map_size,
storage: tile_storage,
texture: TilemapTexture::Single(texture_handle),
tile_size,
transform: get_tilemap_center_transform(&tilemap_size, &grid_size, &map_type, 0.0),
transform: get_tilemap_center_transform(&map_size, &grid_size, &map_type, 0.0),
..Default::default()
});

Expand Down Expand Up @@ -103,8 +103,6 @@ fn main() {
App::new()
.add_plugins(DefaultPlugins.set(WindowPlugin{
window: WindowDescriptor {
width: 1270.0,
height: 720.0,
title: String::from(
"Basic Example - Press Space to change Texture and H to show/hide tilemap.",
),
Expand Down
12 changes: 5 additions & 7 deletions examples/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ fn startup(mut commands: Commands, asset_server: Res<AssetServer>) {

let texture_handle: Handle<Image> = asset_server.load("tiles.png");

let tilemap_size = TilemapSize { x: 1280, y: 1280 };
let mut tile_storage = TileStorage::empty(tilemap_size);
let map_size = TilemapSize { x: 1280, y: 1280 };
let mut tile_storage = TileStorage::empty(map_size);
let tilemap_entity = commands.spawn_empty().id();

fill_tilemap(
TileTextureIndex(0),
tilemap_size,
map_size,
TilemapId(tilemap_entity),
&mut commands,
&mut tile_storage,
Expand All @@ -30,11 +30,11 @@ fn startup(mut commands: Commands, asset_server: Res<AssetServer>) {
commands.entity(tilemap_entity).insert(TilemapBundle {
grid_size,
map_type,
size: tilemap_size,
size: map_size,
storage: tile_storage,
texture: TilemapTexture::Single(texture_handle),
tile_size,
transform: get_tilemap_center_transform(&tilemap_size, &grid_size, &map_type, 0.0),
transform: get_tilemap_center_transform(&map_size, &grid_size, &map_type, 0.0),
..Default::default()
});
}
Expand All @@ -45,8 +45,6 @@ fn main() {
DefaultPlugins
.set(WindowPlugin {
window: WindowDescriptor {
width: 1270.0,
height: 720.0,
title: String::from("Benchmark Example"),
..Default::default()
},
Expand Down
2 changes: 0 additions & 2 deletions examples/chunking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,6 @@ fn main() {
DefaultPlugins
.set(WindowPlugin {
window: WindowDescriptor {
width: 1270.0,
height: 720.0,
title: String::from("Basic Chunking Example"),
..Default::default()
},
Expand Down
10 changes: 5 additions & 5 deletions examples/colors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ fn startup(mut commands: Commands, asset_server: Res<AssetServer>) {
let texture_handle: Handle<Image> = asset_server.load("tiles.png");

// In total, there will be `(QUADRANT_SIDE_LENGTH * 2) * (QUADRANT_SIDE_LENGTH * 2)` tiles.
let total_size = TilemapSize {
let map_size = TilemapSize {
x: QUADRANT_SIDE_LENGTH * 2,
y: QUADRANT_SIDE_LENGTH * 2,
};
Expand All @@ -21,7 +21,7 @@ fn startup(mut commands: Commands, asset_server: Res<AssetServer>) {
y: QUADRANT_SIDE_LENGTH,
};

let mut tile_storage = TileStorage::empty(total_size);
let mut tile_storage = TileStorage::empty(map_size);
let tilemap_entity = commands.spawn_empty().id();
let tilemap_id = TilemapId(tilemap_entity);

Expand Down Expand Up @@ -76,14 +76,16 @@ fn startup(mut commands: Commands, asset_server: Res<AssetServer>) {

let tile_size = TilemapTileSize { x: 16.0, y: 16.0 };
let grid_size = tile_size.into();
let map_type = TilemapType::default();

commands.entity(tilemap_entity).insert(TilemapBundle {
grid_size,
size: total_size,
size: map_size,
storage: tile_storage,
texture: TilemapTexture::Single(texture_handle),
tile_size,
map_type: TilemapType::Square,
transform: get_tilemap_center_transform(&map_size, &grid_size, &map_type, 0.0),
..Default::default()
});
}
Expand All @@ -94,8 +96,6 @@ fn main() {
DefaultPlugins
.set(WindowPlugin {
window: WindowDescriptor {
width: 1270.0,
height: 720.0,
title: String::from("Color Example"),
..Default::default()
},
Expand Down
Loading

0 comments on commit 56e48a2

Please sign in to comment.