forked from bevyengine/bevy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtilemap_chunk_orientation.rs
More file actions
78 lines (70 loc) · 2.71 KB
/
tilemap_chunk_orientation.rs
File metadata and controls
78 lines (70 loc) · 2.71 KB
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
//! Shows a tilemap chunk rendered with a single draw call, including different orientations of tiles (rotated, mirrored) and using different tileset indices, colors, alpha and visibility to show all tile features.
use bevy::{
image::{ImageArrayLayout, ImageLoaderSettings},
prelude::*,
sprite_render::{AlphaMode2d, TileData, TileOrientation, TilemapChunk, TilemapChunkTileData},
};
fn main() {
App::new()
.add_plugins(DefaultPlugins.set(ImagePlugin::default_nearest()))
.insert_resource(ClearColor(Color::srgb(0.5, 0.5, 0.9)))
.add_systems(Startup, setup)
.run();
}
fn setup(mut commands: Commands, assets: Res<AssetServer>) {
let chunk_size = UVec2::splat(8);
let tile_display_size = UVec2::splat(64);
// We'll use each possible orientation, one per column
let orientation = [
TileOrientation::Default,
TileOrientation::Rotate90,
TileOrientation::Rotate180,
TileOrientation::Rotate270,
TileOrientation::MirrorH,
TileOrientation::MirrorHRotate90,
TileOrientation::MirrorHRotate180,
TileOrientation::MirrorHRotate270,
];
// Show different color/alpha on each row
let colors = [
Color::WHITE,
Color::linear_rgb(1.0, 0.0, 0.0),
Color::linear_rgb(0.0, 1.0, 0.0),
Color::linear_rgb(0.0, 0.0, 1.0),
Color::linear_rgba(1.0, 0.0, 0.0, 0.25),
Color::linear_rgba(0.0, 1.0, 0.0, 0.25),
Color::linear_rgba(0.0, 0.0, 1.0, 0.25),
Color::linear_rgba(1.0, 1.0, 1.0, 0.5),
];
let tile_data = (0..chunk_size.element_product())
.map(|i| {
let row = i / 8;
let col = i % 8;
Some(TileData {
// Alternate tiles per row
tileset_index: (row % 2) as u16,
color: colors[row as usize],
// Last (top) row is invisible
visible: row != 7,
orientation: orientation[col as usize],
})
})
.collect();
commands.spawn((
TilemapChunk {
chunk_size,
tile_display_size,
tileset: assets
.load_builder()
.with_settings(|settings: &mut ImageLoaderSettings| {
// The tileset texture is expected to be an array of tile textures, so we tell the
// `ImageLoader` that our texture is composed of 2 stacked tile images.
settings.array_layout = Some(ImageArrayLayout::RowCount { rows: 2 });
})
.load("textures/arrow.png"),
alpha_mode: AlphaMode2d::Blend,
},
TilemapChunkTileData(tile_data),
));
commands.spawn(Camera2d);
}