Skip to content

Commit

Permalink
wip: hex_neighbors compiles
Browse files Browse the repository at this point in the history
  • Loading branch information
rparrett committed Nov 21, 2024
1 parent 206320c commit 9e0e5a7
Showing 1 changed file with 31 additions and 49 deletions.
80 changes: 31 additions & 49 deletions examples/hex_neighbors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,28 +93,24 @@ fn spawn_tile_labels(
tile_q: Query<&mut TilePos>,
font_handle: Res<FontHandle>,
) {
let text_style = TextStyle {
font: font_handle.clone(),
font_size: 20.0,
color: Color::BLACK,
};
let text_justify = JustifyText::Center;
for (map_transform, map_type, grid_size, tilemap_storage) in tilemap_q.iter() {
for tile_entity in tilemap_storage.iter().flatten() {
let tile_pos = tile_q.get(*tile_entity).unwrap();
let tile_center = tile_pos.center_in_world(grid_size, map_type).extend(1.0);
let transform = *map_transform * Transform::from_translation(tile_center);

let label_entity = commands
.spawn(Text2dBundle {
text: Text::from_section(
format!("{}, {}", tile_pos.x, tile_pos.y),
text_style.clone(),
)
.with_justify(text_justify),
.spawn((
Text2d::new(format!("{}, {}", tile_pos.x, tile_pos.y)),
TextFont {
font: font_handle.clone(),
font_size: 20.0,
..default()
},
TextColor(Color::BLACK),
TextLayout::new_with_justify(JustifyText::Center),
transform,
..default()
})
))
.id();
commands
.entity(*tile_entity)
Expand All @@ -133,13 +129,6 @@ fn spawn_map_type_label(
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
Expand All @@ -149,12 +138,15 @@ fn spawn_map_type_label(
..Default::default()
};
commands.spawn((
Text2dBundle {
text: Text::from_section(format!("{map_type:?}"), text_style.clone())
.with_justify(text_justify),
transform,
Text2d::new(format!("{map_type:?}")),
TextFont {
font: font_handle.clone(),
font_size: 20.0,
..default()
},
TextColor(Color::BLACK),
TextLayout::new_with_justify(JustifyText::Center),
transform,
MapTypeLabel,
));
}
Expand All @@ -177,7 +169,7 @@ fn swap_map_type(
(&TileLabel, &TilePos),
(With<TileLabel>, Without<MapTypeLabel>, Without<TilemapType>),
>,
mut map_type_label_q: Query<&mut Text, With<MapTypeLabel>>,
mut map_type_label_q: Query<&mut Text2d, With<MapTypeLabel>>,
mut transform_q: Query<&mut Transform, Without<TilemapType>>,
tile_handle_hex_row: Res<TileHandleHexRow>,
tile_handle_hex_col: Res<TileHandleHexCol>,
Expand Down Expand Up @@ -231,7 +223,7 @@ fn swap_map_type(
}

for mut label_text in map_type_label_q.iter_mut() {
label_text.sections[0].value = format!("{:?}", map_type.as_ref());
label_text.0 = format!("{:?}", map_type.as_ref());
}
}
}
Expand Down Expand Up @@ -281,15 +273,13 @@ fn hover_highlight_tile_label(
)>,
highlighted_tiles_q: Query<Entity, With<Hovered>>,
tile_label_q: Query<&TileLabel>,
mut text_q: Query<&mut Text>,
mut text_q: Query<&mut TextColor>,
) {
// Un-highlight any previously highlighted tile labels.
for highlighted_tile_entity in highlighted_tiles_q.iter() {
if let Ok(label) = tile_label_q.get(highlighted_tile_entity) {
if let Ok(mut tile_text) = text_q.get_mut(label.0) {
for section in tile_text.sections.iter_mut() {
section.style.color = Color::BLACK;
}
if let Ok(mut text_color) = text_q.get_mut(label.0) {
text_color.0 = Color::BLACK;
commands.entity(highlighted_tile_entity).remove::<Hovered>();
}
}
Expand All @@ -313,10 +303,8 @@ fn hover_highlight_tile_label(
// Highlight the relevant tile's label
if let Some(tile_entity) = tile_storage.get(&tile_pos) {
if let Ok(label) = tile_label_q.get(tile_entity) {
if let Ok(mut tile_text) = text_q.get_mut(label.0) {
for section in tile_text.sections.iter_mut() {
section.style.color = palettes::tailwind::RED_600.into();
}
if let Ok(mut text_color) = text_q.get_mut(label.0) {
text_color.0 = palettes::tailwind::RED_600.into();
commands.entity(tile_entity).insert(Hovered);
}
}
Expand All @@ -337,15 +325,13 @@ fn highlight_neighbor_label(
highlighted_tiles_q: Query<Entity, With<NeighborHighlight>>,
hovered_tiles_q: Query<&TilePos, With<Hovered>>,
tile_label_q: Query<&TileLabel>,
mut text_q: Query<&mut Text>,
mut text_q: Query<&mut TextColor>,
) {
// Un-highlight any previously highlighted tile labels.
for highlighted_tile_entity in highlighted_tiles_q.iter() {
if let Ok(label) = tile_label_q.get(highlighted_tile_entity) {
if let Ok(mut tile_text) = text_q.get_mut(label.0) {
for section in tile_text.sections.iter_mut() {
section.style.color = Color::BLACK;
}
if let Ok(mut text_color) = text_q.get_mut(label.0) {
text_color.0 = Color::BLACK;
commands
.entity(highlighted_tile_entity)
.remove::<NeighborHighlight>();
Expand All @@ -369,10 +355,8 @@ fn highlight_neighbor_label(
// `checked_get`.
if let Some(tile_entity) = tile_storage.checked_get(neighbor_pos) {
if let Ok(label) = tile_label_q.get(tile_entity) {
if let Ok(mut tile_text) = text_q.get_mut(label.0) {
for section in tile_text.sections.iter_mut() {
section.style.color = palettes::tailwind::BLUE_600.into();
}
if let Ok(mut text_color) = text_q.get_mut(label.0) {
text_color.0 = palettes::tailwind::BLUE_600.into();
commands.entity(tile_entity).insert(NeighborHighlight);
}
}
Expand Down Expand Up @@ -410,10 +394,8 @@ fn highlight_neighbor_label(
// `checked_get`.
if let Some(tile_entity) = tile_storage.checked_get(&tile_pos) {
if let Ok(label) = tile_label_q.get(tile_entity) {
if let Ok(mut tile_text) = text_q.get_mut(label.0) {
for section in tile_text.sections.iter_mut() {
section.style.color = palettes::tailwind::GREEN_600.into();
}
if let Ok(mut text_color) = text_q.get_mut(label.0) {
text_color.0 = palettes::tailwind::GREEN_600.into();
commands.entity(tile_entity).insert(NeighborHighlight);
}
}
Expand Down

0 comments on commit 9e0e5a7

Please sign in to comment.