-
Notifications
You must be signed in to change notification settings - Fork 198
/
texture_vec.rs
111 lines (98 loc) · 3.48 KB
/
texture_vec.rs
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
mod helpers;
#[cfg(not(feature = "atlas"))]
mod no_atlas {
use super::helpers;
use bevy::prelude::*;
use bevy_ecs_tilemap::helpers::hex_grid::axial::AxialPos;
use bevy_ecs_tilemap::prelude::*;
use rand::prelude::SliceRandom;
use rand::thread_rng;
const MAP_RADIUS: u32 = 10;
const MAP_DIAMETER: u32 = 2 * MAP_RADIUS + 1;
const MAP_CENTER: TilePos = TilePos {
x: MAP_RADIUS,
y: MAP_RADIUS,
};
const TILE_SIZE: TilemapTileSize = TilemapTileSize { x: 48.0, y: 54.0 };
const COORD_SYS: HexCoordSystem = HexCoordSystem::Row;
fn startup(mut commands: Commands, asset_server: Res<AssetServer>) {
commands.spawn(Camera2dBundle::default());
let image_handles = vec![
asset_server.load("hex-tile-0.png"),
asset_server.load("hex-tile-1.png"),
asset_server.load("hex-tile-2.png"),
];
let texture_vec = TilemapTexture::Vector(image_handles);
let map_size = TilemapSize {
x: MAP_DIAMETER,
y: MAP_DIAMETER,
};
let mut tile_storage = TileStorage::empty(map_size);
let tilemap_entity = commands.spawn_empty().id();
let tilemap_id = TilemapId(tilemap_entity);
let tile_positions = generate_hexagon(
AxialPos::from_tile_pos_given_coord_system(&MAP_CENTER, COORD_SYS),
MAP_RADIUS,
)
.into_iter()
.map(|axial_pos| axial_pos.as_tile_pos_given_coord_system(COORD_SYS));
let mut rng = thread_rng();
let weighted_tile_choices = [
(TileTextureIndex(0), 0.8),
(TileTextureIndex(1), 0.1),
(TileTextureIndex(2), 0.1),
];
for position in tile_positions {
let texture = weighted_tile_choices
.choose_weighted(&mut rng, |choice| choice.1)
.unwrap()
.0;
let tile_entity = commands
.spawn(TileBundle {
position,
tilemap_id,
texture_index: texture,
..Default::default()
})
.id();
tile_storage.set(&position, tile_entity);
}
let tile_size = TILE_SIZE;
let grid_size = TILE_SIZE.into();
let map_type = TilemapType::Hexagon(COORD_SYS);
commands.entity(tilemap_entity).insert(TilemapBundle {
grid_size,
map_type,
tile_size,
size: map_size,
storage: tile_storage,
texture: texture_vec,
transform: get_tilemap_center_transform(&map_size, &grid_size, &map_type, 0.0),
..Default::default()
});
}
pub fn main() {
App::new()
.add_plugins(
DefaultPlugins
.set(WindowPlugin {
primary_window: Some(Window {
title: String::from("Using TilemapTexture::Vector"),
..Default::default()
}),
..default()
})
.set(ImagePlugin::default_nearest()),
)
.add_plugins(TilemapPlugin)
.add_systems(Startup, startup)
.add_systems(Update, helpers::camera::movement)
.run();
}
}
fn main() {
#[cfg(feature = "atlas")]
panic!("Atlas feature does not support texture vectors!");
#[cfg(not(feature = "atlas"))]
no_atlas::main();
}