Skip to content

Commit

Permalink
Examples required components migration, part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
bas-ie committed Nov 2, 2024
1 parent 191ff17 commit 9f4355f
Show file tree
Hide file tree
Showing 10 changed files with 134 additions and 162 deletions.
20 changes: 9 additions & 11 deletions examples/accessing_tiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ struct CurrentColor(u16);
struct LastUpdate(f64);

fn startup(mut commands: Commands, asset_server: Res<AssetServer>) {
commands.spawn(Camera2dBundle::default());
commands.spawn(Camera2d);

let texture_handle: Handle<Image> = asset_server.load("tiles.png");

Expand Down Expand Up @@ -83,16 +83,14 @@ fn startup(mut commands: Commands, asset_server: Res<AssetServer>) {
// Spawns a tilemap.
// Once the tile storage is inserted onto the tilemap entity it can no longer be accessed.
commands.entity(tilemap_entity).insert((
TilemapBundle {
grid_size,
size: map_size,
storage: tile_storage,
map_type,
texture: TilemapTexture::Single(texture_handle),
tile_size,
transform: get_tilemap_center_transform(&map_size, &grid_size, &map_type, 0.0),
..Default::default()
},
Tilemap,
grid_size,
map_size,
tile_storage,
map_type,
TilemapTexture::Single(texture_handle),
tile_size,
get_tilemap_center_transform(&map_size, &grid_size, &map_type, 0.0),
LastUpdate(0.0),
CurrentColor(1),
));
Expand Down
16 changes: 8 additions & 8 deletions examples/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ fn startup(
ArrayTextureLoader,
>,
) {
commands.spawn(Camera2dBundle::default());
commands.spawn(Camera2d);

let texture_handle: Handle<Image> = asset_server.load("tiles.png");

Expand Down Expand Up @@ -49,16 +49,16 @@ fn startup(
let grid_size = tile_size.into();
let map_type = TilemapType::default();

commands.entity(tilemap_entity).insert(TilemapBundle {
commands.entity(tilemap_entity).insert((
Tilemap,
grid_size,
map_type,
size: map_size,
storage: tile_storage,
texture: TilemapTexture::Single(texture_handle),
map_size,
tile_storage,
TilemapTexture::Single(texture_handle),
tile_size,
transform: get_tilemap_center_transform(&map_size, &grid_size, &map_type, 0.0),
..Default::default()
});
get_tilemap_center_transform(&map_size, &grid_size, &map_type, 0.0),
));

// Add atlas to array texture loader so it's preprocessed before we need to use it.
// Only used when the atlas feature is off and we are using array textures.
Expand Down
43 changes: 21 additions & 22 deletions examples/frustum_cull_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl FromWorld for FontHandle {

// Generates the initial tilemap, which is a square grid.
fn spawn_tilemap(mut commands: Commands, tile_handle_square: Res<TileHandleSquare>) {
commands.spawn(Camera2dBundle::default());
commands.spawn(Camera2d);

let map_size = TilemapSize {
// Render chunks are of size 64, so let's create two render chunks
Expand All @@ -98,18 +98,18 @@ fn spawn_tilemap(mut commands: Commands, tile_handle_square: Res<TileHandleSquar
let tile_size = TILE_SIZE_SQUARE;
let grid_size = GRID_SIZE_SQUARE;

commands.entity(tilemap_entity).insert(TilemapBundle {
commands.entity(tilemap_entity).insert((
Tilemap,
grid_size,
size: map_size,
storage: tile_storage,
texture: TilemapTexture::Single(tile_handle_square.clone()),
map_size,
tile_storage,
TilemapTexture::Single(tile_handle_square.clone()),
tile_size,
map_type: TilemapType::Square,
TilemapType::Square,
// The default behaviour is `FrustumCulling(true)`, but we supply this explicitly here
// for the purposes of the example.
frustum_culling: FrustumCulling(true),
..Default::default()
});
FrustumCulling(true),
));
}

#[derive(Component)]
Expand All @@ -122,13 +122,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 @@ -138,12 +131,18 @@ 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 {
justify: JustifyText::Center,
..default()
},
transform,
MapTypeLabel,
));
}
Expand All @@ -160,7 +159,7 @@ fn swap_map_type(
&mut TilemapTileSize,
)>,
keyboard_input: Res<ButtonInput<KeyCode>>,
mut map_type_label_q: Query<&mut Text, With<MapTypeLabel>>,
mut map_type_label_q: Query<&mut Text2d, With<MapTypeLabel>>,
tile_handle_square: Res<TileHandleSquare>,
tile_handle_hex_row: Res<TileHandleHexRow>,
tile_handle_hex_col: Res<TileHandleHexCol>,
Expand Down Expand Up @@ -216,7 +215,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
102 changes: 40 additions & 62 deletions examples/hex_neighbors_radius_chunks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ fn hex_neighbors_radius_from_tile_pos(
}

fn spawn_chunks(mut commands: Commands, tile_handle_hex_row: Res<TileHandleHexRow>) {
commands.spawn(Camera2dBundle::default());
commands.spawn(Camera2d);

let map_size = TilemapSize {
x: CHUNK_MAP_SIDE_LENGTH_X,
Expand Down Expand Up @@ -223,18 +223,16 @@ fn spawn_chunks(mut commands: Commands, tile_handle_hex_row: Res<TileHandleHexRo

commands
.entity(tilemap_entity)
.insert(TilemapBundle {
.insert((
Tilemap,
grid_size,
size: map_size,
storage: tile_storage,
texture: TilemapTexture::Single(tile_handle_hex_row.clone()),
map_size,
tile_storage,
TilemapTexture::Single(tile_handle_hex_row.clone()),
tile_size,
map_type,
transform: Transform::from_translation(chunk_in_world_position(
*chunk_pos, map_type,
)),
..Default::default()
})
Transform::from_translation(chunk_in_world_position(*chunk_pos, map_type)),
))
.insert(chunk_pos);
}
}
Expand All @@ -252,7 +250,7 @@ fn swap_map_type(
)>,
keyboard_input: Res<ButtonInput<KeyCode>>,
tile_label_q: Query<(Entity, &TileLabel, &TilePos), Without<TilemapType>>,
mut transform_q: Query<(&mut Transform, &mut Text), Without<TilemapType>>,
mut transform_q: Query<(&mut Transform, &mut Text2d), Without<TilemapType>>,
tile_handle_hex_row: Res<TileHandleHexRow>,
tile_handle_hex_col: Res<TileHandleHexCol>,
) {
Expand Down Expand Up @@ -308,8 +306,7 @@ fn swap_map_type(
&map_type,
&map_transform,
);
tile_label_text.sections.get_mut(0).unwrap().value =
format!("{}, {}", hex_pos.x, hex_pos.y);
tile_label_text.0 = format!("{}, {}", hex_pos.x, hex_pos.y);
}
}
}
Expand All @@ -327,12 +324,6 @@ fn spawn_tile_labels(
tile_q: Query<&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();
Expand All @@ -342,15 +333,20 @@ fn spawn_tile_labels(
let hex_pos = hex_pos_from_tile_pos(tile_pos, grid_size, map_type, map_transform);

let label_entity = commands
.spawn(Text2dBundle {
text: Text::from_section(
format!("{}, {}", hex_pos.x, hex_pos.y),
text_style.clone(),
)
.with_justify(text_justify),
.spawn((
Text2d::new(format!("{}, {}", hex_pos.x, hex_pos.y)),
TextFont {
font: font_handle.clone(),
font_size: 20.0,
..default()
},
TextColor(Color::BLACK),
TextLayout {
justify: JustifyText::Center,
..default()
},
transform,
..default()
})
))
.id();
commands
.entity(*tile_entity)
Expand Down Expand Up @@ -382,7 +378,7 @@ pub fn update_cursor_pos(
// any transforms on the camera. This is done by projecting the cursor position into
// camera space (world space).
for (cam_t, cam) in camera_q.iter() {
if let Some(pos) = cam.viewport_to_world_2d(cam_t, cursor_moved.position) {
if let Ok(pos) = cam.viewport_to_world_2d(cam_t, cursor_moved.position) {
*cursor_pos = CursorPos(pos);
}
}
Expand All @@ -401,18 +397,13 @@ fn hover_highlight_tile_label(
&Transform,
)>,
highlighted_tiles_q: Query<Entity, With<Hovered>>,
tile_label_q: Query<&TileLabel>,
mut text_q: Query<&mut Text>,
mut tile_label_q: Query<&mut TextColor, With<TileLabel>>,
) {
// 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;
}
commands.entity(highlighted_tile_entity).remove::<Hovered>();
}
if let Ok(mut label_color) = tile_label_q.get_mut(highlighted_tile_entity) {
*label_color = Color::BLACK.into();
commands.entity(highlighted_tile_entity).remove::<Hovered>();
}
}

Expand All @@ -427,13 +418,9 @@ fn hover_highlight_tile_label(
TilePos::from_world_pos(&cursor_pos_in_map_pos, map_size, grid_size, map_type)
{
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();
}
commands.entity(tile_entity).insert(Hovered);
}
if let Ok(mut label_color) = tile_label_q.get_mut(tile_entity) {
*label_color = palettes::tailwind::RED_600.into();
commands.entity(tile_entity).insert(Hovered);
}
}
}
Expand Down Expand Up @@ -467,20 +454,15 @@ fn highlight_neighbor_labels(
highlighted_tiles_q: Query<Entity, With<NeighborHighlight>>,
hovered_tiles_q: Query<(Entity, &TilePos), With<Hovered>>,
tiles_q: Query<&TilePos, Without<Hovered>>,
tile_label_q: Query<&TileLabel>,
mut text_q: Query<&mut Text>,
mut tile_label_q: Query<&mut TextColor, With<TileLabel>>,
radius: Res<HighlightRadius>,
) {
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;
}
commands
.entity(highlighted_tile_entity)
.remove::<NeighborHighlight>();
}
if let Ok(mut label_color) = tile_label_q.get_mut(highlighted_tile_entity) {
*label_color = Color::BLACK.into();
commands
.entity(highlighted_tile_entity)
.remove::<NeighborHighlight>();
}
}

Expand Down Expand Up @@ -508,13 +490,9 @@ fn highlight_neighbor_labels(
if let Ok(tile_pos) = tiles_q.get(*tile_entity) {
let tile_hex_pos = hex_pos_from_tile_pos(tile_pos, grid_size, map_type, map_t);
if neighbors.contains(&tile_hex_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();
}
commands.entity(*tile_entity).insert(NeighborHighlight);
}
if let Ok(mut label_color) = tile_label_q.get_mut(*tile_entity) {
*label_color = palettes::tailwind::BLUE_600.into();
commands.entity(*tile_entity).insert(NeighborHighlight);
}
}
}
Expand Down
16 changes: 8 additions & 8 deletions examples/hexagon_column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ mod helpers;
const QUADRANT_SIDE_LENGTH: u32 = 80;

fn startup(mut commands: Commands, asset_server: Res<AssetServer>) {
commands.spawn(Camera2dBundle::default());
commands.spawn(Camera2d);

let texture_handle: Handle<Image> = asset_server.load("flat_hex_tiles.png");

Expand Down Expand Up @@ -74,16 +74,16 @@ fn startup(mut commands: Commands, asset_server: Res<AssetServer>) {
let grid_size = TilemapGridSize { x: 17.0, y: 15.0 };
let map_type = TilemapType::Hexagon(HexCoordSystem::Column);

commands.entity(tilemap_entity).insert(TilemapBundle {
commands.entity(tilemap_entity).insert((
Tilemap,
grid_size,
size: map_size,
storage: tile_storage,
texture: TilemapTexture::Single(texture_handle),
map_size,
tile_storage,
TilemapTexture::Single(texture_handle),
tile_size,
map_type,
transform: get_tilemap_center_transform(&map_size, &grid_size, &map_type, 0.0),
..Default::default()
});
get_tilemap_center_transform(&map_size, &grid_size, &map_type, 0.0),
));
}

fn swap_mesh_type(
Expand Down
Loading

0 comments on commit 9f4355f

Please sign in to comment.