forked from StarArawn/bevy_ecs_tilemap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hexagon_generation.rs
240 lines (213 loc) · 7.92 KB
/
hexagon_generation.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
use bevy::{ecs::system::Resource, prelude::*};
use bevy_ecs_tilemap::prelude::*;
mod helpers;
use helpers::camera::movement as camera_movement;
// Press SPACE to change map type. Hover over a tile to highlight its label (red) and those of its
// neighbors (blue). Press and hold one of keys 0-5 to mark the neighbor in that direction (green).
// You can increase the MAP_SIDE_LENGTH, in order to test larger maps but just make sure that you run
// in release mode (`cargo run --release --example hexagon_generation`) otherwise things might be too
// slow.
const MAP_SIDE_LENGTH: u32 = 8;
const TILE_SIZE_HEX_ROW: TilemapTileSize = TilemapTileSize { x: 50.0, y: 58.0 };
const TILE_SIZE_HEX_COL: TilemapTileSize = TilemapTileSize { x: 58.0, y: 50.0 };
const GRID_SIZE_HEX_ROW: TilemapGridSize = TilemapGridSize { x: 50.0, y: 58.0 };
const GRID_SIZE_HEX_COL: TilemapGridSize = TilemapGridSize { x: 58.0, y: 50.0 };
#[derive(Deref, Resource)]
pub struct TileHandleHexRow(Handle<Image>);
#[derive(Deref, Resource)]
pub struct TileHandleHexCol(Handle<Image>);
#[derive(Deref, Resource)]
pub struct TileHandleSquare(Handle<Image>);
#[derive(Deref, Resource)]
pub struct TileHandleIso(Handle<Image>);
#[derive(Deref, Resource)]
pub struct FontHandle(Handle<Font>);
impl FromWorld for TileHandleHexCol {
fn from_world(world: &mut World) -> Self {
let asset_server = world.resource::<AssetServer>();
Self(asset_server.load("bw-tile-hex-col.png"))
}
}
impl FromWorld for TileHandleHexRow {
fn from_world(world: &mut World) -> Self {
let asset_server = world.resource::<AssetServer>();
Self(asset_server.load("bw-tile-hex-row.png"))
}
}
impl FromWorld for FontHandle {
fn from_world(world: &mut World) -> Self {
let asset_server = world.resource::<AssetServer>();
Self(asset_server.load("fonts/FiraSans-Bold.ttf"))
}
}
// Generates the initial tilemap, which is a square grid.
fn spawn_tilemap(mut commands: Commands, tile_handle_hex_row: Res<TileHandleHexRow>) {
commands.spawn(Camera2dBundle::default());
let map_size = TilemapSize {
x: MAP_SIDE_LENGTH,
y: MAP_SIDE_LENGTH,
};
let mut tile_storage = TileStorage::empty(map_size);
let tilemap_entity = commands.spawn_empty().id();
let tilemap_id = TilemapId(tilemap_entity);
let hex_coord_system = HexCoordSystem::Row;
fill_tilemap_hexagon(
TileTextureIndex(0),
TilePos {
x: MAP_SIDE_LENGTH / 2,
y: MAP_SIDE_LENGTH / 2,
},
MAP_SIDE_LENGTH / 2,
hex_coord_system,
tilemap_id,
&mut commands,
&mut tile_storage,
);
let tile_size = TILE_SIZE_HEX_ROW;
let grid_size = GRID_SIZE_HEX_ROW;
let map_type = TilemapType::Hexagon(hex_coord_system);
commands.entity(tilemap_entity).insert(TilemapBundle {
grid_size,
size: map_size,
storage: tile_storage,
texture: TilemapTexture::Single(tile_handle_hex_row.clone()),
tile_size,
map_type,
transform: get_tilemap_center_transform(&map_size, &grid_size, &map_type, 0.0),
..Default::default()
});
}
#[derive(Component)]
pub struct MapTypeLabel;
// Generates the map type label: e.g. `Square { diagonal_neighbors: false }`
fn spawn_map_type_label(
mut commands: Commands,
font_handle: Res<FontHandle>,
windows: Query<&Window>,
map_type_q: Query<&TilemapType>,
) {
let text_style = TextStyle {
font: font_handle.clone(),
font_size: 20.0,
color: Color::BLACK,
};
let text_justify = JustifyText::Center;
for window in windows.iter() {
for map_type in map_type_q.iter() {
// Place the map type label somewhere in the top left side of the screen
let transform = Transform {
translation: Vec2::new(-0.5 * window.width() / 2.0, 0.8 * window.height() / 2.0)
.extend(1.0),
..Default::default()
};
commands.spawn((
Text2dBundle {
text: Text::from_section(format!("{map_type:?}"), text_style.clone())
.with_justify(text_justify),
transform,
..default()
},
MapTypeLabel,
));
}
}
}
// Swaps the map type, when user presses SPACE
#[allow(clippy::too_many_arguments)]
fn swap_map_type(
mut commands: Commands,
mut tilemap_query: Query<(
Entity,
&mut Transform,
&TilemapSize,
&mut TilemapType,
&mut TilemapGridSize,
&mut TilemapTexture,
&mut TilemapTileSize,
&mut TileStorage,
)>,
keyboard_input: Res<ButtonInput<KeyCode>>,
mut map_type_label_q: Query<&mut Text, With<MapTypeLabel>>,
tile_handle_hex_row: Res<TileHandleHexRow>,
tile_handle_hex_col: Res<TileHandleHexCol>,
) {
if keyboard_input.just_pressed(KeyCode::Space) {
for (
map_id,
mut map_transform,
map_size,
mut map_type,
mut grid_size,
mut map_texture,
mut tile_size,
mut tile_storage,
) in tilemap_query.iter_mut()
{
// Remove all previously spawned tiles.
for possible_entity in tile_storage.iter_mut() {
// see documentation for take to understand how it works:
// https://doc.rust-lang.org/std/option/enum.Option.html#method.take
if let Some(entity) = possible_entity.take() {
commands.entity(entity).despawn_recursive();
}
}
let new_coord_sys = match map_type.as_ref() {
TilemapType::Hexagon(HexCoordSystem::Row) => HexCoordSystem::Column,
TilemapType::Hexagon(HexCoordSystem::Column) => HexCoordSystem::Row,
_ => unreachable!(),
};
*map_type = TilemapType::Hexagon(new_coord_sys);
if new_coord_sys == HexCoordSystem::Column {
*map_texture = TilemapTexture::Single((*tile_handle_hex_col).clone());
*tile_size = TILE_SIZE_HEX_COL;
*grid_size = GRID_SIZE_HEX_COL;
} else if new_coord_sys == HexCoordSystem::Row {
*map_texture = TilemapTexture::Single((*tile_handle_hex_row).clone());
*tile_size = TILE_SIZE_HEX_ROW;
*grid_size = GRID_SIZE_HEX_ROW;
}
*map_transform = get_tilemap_center_transform(map_size, &grid_size, &map_type, 0.0);
// Re-generate tiles in a hexagonal pattern.
fill_tilemap_hexagon(
TileTextureIndex(0),
TilePos {
x: MAP_SIDE_LENGTH / 2,
y: MAP_SIDE_LENGTH / 2,
},
MAP_SIDE_LENGTH / 2,
new_coord_sys,
TilemapId(map_id),
&mut commands,
&mut tile_storage,
);
for mut label_text in map_type_label_q.iter_mut() {
label_text.sections[0].value = format!("{:?}", map_type.as_ref());
}
}
}
}
fn main() {
App::new()
.add_plugins(
DefaultPlugins
.set(WindowPlugin {
primary_window: Some(Window {
title: String::from("Generating a hexagonal hex map"),
..Default::default()
}),
..default()
})
.set(ImagePlugin::default_nearest()),
)
.add_plugins(TilemapPlugin)
.init_resource::<TileHandleHexCol>()
.init_resource::<TileHandleHexRow>()
.init_resource::<FontHandle>()
.add_systems(
Startup,
(spawn_tilemap, apply_deferred, spawn_map_type_label).chain(),
)
.add_systems(Update, camera_movement)
.add_systems(Update, swap_map_type)
.run();
}