-
Notifications
You must be signed in to change notification settings - Fork 26
/
hex_area.rs
243 lines (230 loc) · 7.4 KB
/
hex_area.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
241
242
243
use bevy::{
color::palettes::css::{GOLD, ORANGE, RED, WHITE},
prelude::*,
render::{mesh::Indices, render_asset::RenderAssetUsages, render_resource::PrimitiveTopology},
utils::HashSet,
window::PrimaryWindow,
};
use hexx::*;
use std::collections::HashMap;
/// World size of the hexagons (outer radius)
const HEX_SIZE: Vec2 = Vec2::splat(15.0);
pub fn main() {
App::new()
.init_resource::<HexArea>()
.add_plugins(DefaultPlugins.set(WindowPlugin {
primary_window: Some(Window {
resolution: (1_920.0, 1_080.0).into(),
..default()
}),
..default()
}))
.add_systems(Startup, (setup_camera, setup_grid))
.add_systems(Update, (handle_input, gizmos).chain())
.run();
}
#[derive(Debug, Default, Resource)]
struct HexArea {
pub area: HashSet<Hex>,
}
#[derive(Debug, Resource)]
struct Map {
flat_layout: HexLayout,
pointy_layout: HexLayout,
flat_entities: HashMap<Hex, Entity>,
pointy_entities: HashMap<Hex, Entity>,
flat_cursor_entity: Entity,
pointy_cursor_entity: Entity,
area_material: Handle<ColorMaterial>,
default_material: Handle<ColorMaterial>,
}
/// 2D camera setup
fn setup_camera(mut commands: Commands) {
commands.spawn(Camera2d);
}
/// Hex grid setup
fn setup_grid(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<ColorMaterial>>,
) {
let flat_layout = HexLayout {
hex_size: HEX_SIZE,
orientation: HexOrientation::Flat,
origin: Vec2::new(-480.0, 0.0),
..default()
};
let pointy_layout = HexLayout {
hex_size: HEX_SIZE,
orientation: HexOrientation::Pointy,
origin: Vec2::new(480.0, 0.0),
..default()
};
// materials
let area_material = materials.add(Color::Srgba(GOLD));
let default_material = materials.add(Color::Srgba(WHITE));
let cursor_material = materials.add(Color::Srgba(RED));
// mesh
let mut spawn_map = |layout: &HexLayout| {
let mesh_handle = meshes.add(hexagonal_plane(layout));
let cursor_mesh = meshes.add(border_plane(layout));
let cursor_entity = commands
.spawn((
Mesh2d(cursor_mesh),
MeshMaterial2d(cursor_material.clone()),
Transform::from_xyz(0.0, 0.0, 10.0),
))
.id();
let entities = Hex::ZERO
.range(15)
.map(|hex| {
let pos = layout.hex_to_world_pos(hex);
let id = commands
.spawn((
Mesh2d(mesh_handle.clone()),
MeshMaterial2d(default_material.clone_weak()),
Transform::from_xyz(pos.x, pos.y, 0.0),
))
.id();
(hex, id)
})
.collect();
(cursor_entity, entities)
};
let (flat_cursor_entity, flat_entities) = spawn_map(&flat_layout);
let (pointy_cursor_entity, pointy_entities) = spawn_map(&pointy_layout);
commands.insert_resource(Map {
flat_layout,
pointy_layout,
flat_entities,
pointy_entities,
flat_cursor_entity,
pointy_cursor_entity,
area_material,
default_material,
});
}
/// Input interaction
fn handle_input(
mut commands: Commands,
windows: Query<&Window, With<PrimaryWindow>>,
cameras: Query<(&Camera, &GlobalTransform)>,
map: Res<Map>,
mut area: ResMut<HexArea>,
mouse: Res<ButtonInput<MouseButton>>,
mut cursors: Query<&mut Transform>,
) {
let window = windows.single();
let (camera, cam_transform) = cameras.single();
let Some(pos) = window
.cursor_position()
.and_then(|p| camera.viewport_to_world_2d(cam_transform, p).ok())
else {
return;
};
let mut to_add = Vec::new();
let mut to_remove = Vec::new();
for (layout, entities, cursor) in [
(
&map.flat_layout,
&map.flat_entities,
&map.flat_cursor_entity,
),
(
&map.pointy_layout,
&map.pointy_entities,
&map.pointy_cursor_entity,
),
] {
let coord = layout.world_pos_to_hex(pos);
if entities.get(&coord).is_none() {
continue;
};
let mut cursor = cursors.get_mut(*cursor).unwrap();
let pos = layout.hex_to_world_pos(coord);
cursor.translation.x = pos.x;
cursor.translation.y = pos.y;
if mouse.pressed(MouseButton::Left) {
to_add.push(coord);
} else if mouse.pressed(MouseButton::Right) {
to_remove.push(coord);
}
}
for coord in to_add {
area.area.insert(coord);
let entity = map.flat_entities.get(&coord).unwrap();
commands
.entity(*entity)
.insert(MeshMaterial2d(map.area_material.clone_weak()));
let entity = map.pointy_entities.get(&coord).unwrap();
commands
.entity(*entity)
.insert(MeshMaterial2d(map.area_material.clone_weak()));
}
for coord in to_remove {
area.area.remove(&coord);
let entity = map.flat_entities.get(&coord).unwrap();
commands
.entity(*entity)
.insert(MeshMaterial2d(map.default_material.clone_weak()));
let entity = map.pointy_entities.get(&coord).unwrap();
commands
.entity(*entity)
.insert(MeshMaterial2d(map.default_material.clone_weak()));
}
}
fn gizmos(mut gizmos: Gizmos, area: Res<HexArea>, map: Res<Map>) {
let mut edges = Vec::new();
for hex in &area.area {
for neighbor in hex
.all_neighbors()
.iter()
.filter(|c| !area.area.contains(*c))
{
edges.push(GridEdge {
origin: *hex,
direction: hex.neighbor_direction(*neighbor).unwrap(),
});
}
}
for layout in [&map.flat_layout, &map.pointy_layout] {
for edge in &edges {
let [a, b] = layout.edge_coordinates(*edge);
gizmos.line_2d(a, b, Color::Srgba(ORANGE));
}
}
}
/// Compute a bevy mesh from the layout
fn hexagonal_plane(hex_layout: &HexLayout) -> Mesh {
let mesh_info = PlaneMeshBuilder::new(hex_layout)
.facing(Vec3::Z)
.center_aligned()
.build();
Mesh::new(
PrimitiveTopology::TriangleList,
RenderAssetUsages::RENDER_WORLD,
)
.with_inserted_attribute(Mesh::ATTRIBUTE_POSITION, mesh_info.vertices)
.with_inserted_attribute(Mesh::ATTRIBUTE_NORMAL, mesh_info.normals)
.with_inserted_attribute(Mesh::ATTRIBUTE_UV_0, mesh_info.uvs)
.with_inserted_indices(Indices::U16(mesh_info.indices))
}
fn border_plane(hex_layout: &HexLayout) -> Mesh {
let mesh_info = PlaneMeshBuilder::new(hex_layout)
.facing(Vec3::Z)
.with_inset_options(InsetOptions {
keep_inner_face: false,
scale: 0.2,
..default()
})
.center_aligned()
.build();
Mesh::new(
PrimitiveTopology::TriangleList,
RenderAssetUsages::RENDER_WORLD,
)
.with_inserted_attribute(Mesh::ATTRIBUTE_POSITION, mesh_info.vertices)
.with_inserted_attribute(Mesh::ATTRIBUTE_NORMAL, mesh_info.normals)
.with_inserted_attribute(Mesh::ATTRIBUTE_UV_0, mesh_info.uvs)
.with_inserted_indices(Indices::U16(mesh_info.indices))
}