Skip to content

Commit 41628ce

Browse files
geieredgarrparrett
andauthored
Update to Bevy 0.10 (#390)
* Use bevy git repository * Update to latest bevy version * Fix clippy warnings * Fix shaders * Fix frustum_cull_test example * Update to bevy commit 64b0f19 * Replace Res with SystemParamItem * Fix examples * Use viewport_to_world_2d * Use apply_system_buffers in startup schedule * Update to bevy commit e8114261 * Update to bevy 0.10 * Fix warning when compiling with atlas feature * Fix missing system and improve examples * Replace `SpawnLabelsSet` with `SpawnTilemapSet` --------- Co-authored-by: Rob Parrett <[email protected]>
1 parent 3171dcb commit 41628ce

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+239
-269
lines changed

Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ atlas = []
1515
render = []
1616

1717
[dependencies]
18-
bevy = { version = "0.9", default-features = false, features = [
18+
bevy = { version = "0.10", default-features = false, features = [
1919
"bevy_core_pipeline",
2020
"bevy_render",
2121
"bevy_asset",
@@ -33,7 +33,7 @@ serde_json = { version = "1.0" }
3333
tiled = { version = "0.10.2", default-features = false }
3434

3535
[dev-dependencies.bevy]
36-
version = "0.9"
36+
version = "0.10"
3737
default-features = false
3838
features = [
3939
"bevy_core_pipeline",
@@ -48,7 +48,7 @@ features = [
4848
]
4949

5050
[target.'cfg(unix)'.dev-dependencies.bevy]
51-
version = "0.9"
51+
version = "0.10"
5252
default-features = false
5353
features = [
5454
"bevy_core_pipeline",

examples/accessing_tiles.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,10 @@ fn main() {
152152
.add_plugins(
153153
DefaultPlugins
154154
.set(WindowPlugin {
155-
window: WindowDescriptor {
155+
primary_window: Some(Window {
156156
title: String::from("Accessing Tiles Example"),
157157
..Default::default()
158-
},
158+
}),
159159
..default()
160160
})
161161
.set(ImagePlugin::default_nearest()),

examples/animation.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,10 @@ fn main() {
131131
.add_plugins(
132132
DefaultPlugins
133133
.set(WindowPlugin {
134-
window: WindowDescriptor {
134+
primary_window: Some(Window {
135135
title: String::from("Animated Map Example"),
136136
..Default::default()
137-
},
137+
}),
138138
..default()
139139
})
140140
.set(ImagePlugin::default_nearest()),

examples/basic.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,24 +90,23 @@ fn swap_texture_or_hide(
9090
}
9191
if keyboard_input.just_pressed(KeyCode::H) {
9292
for (_, mut visibility) in &mut query {
93-
if visibility.is_visible {
94-
visibility.is_visible = false;
95-
} else {
96-
visibility.is_visible = true;
97-
}
93+
*visibility = match *visibility {
94+
Visibility::Inherited | Visibility::Visible => Visibility::Hidden,
95+
Visibility::Hidden => Visibility::Visible,
96+
};
9897
}
9998
}
10099
}
101100

102101
fn main() {
103102
App::new()
104103
.add_plugins(DefaultPlugins.set(WindowPlugin{
105-
window: WindowDescriptor {
104+
primary_window: Some(Window {
106105
title: String::from(
107106
"Basic Example - Press Space to change Texture and H to show/hide tilemap.",
108107
),
109108
..Default::default()
110-
},
109+
}),
111110
..default()
112111
}).set(ImagePlugin::default_nearest()))
113112
.add_plugin(TilemapPlugin)

examples/bench.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ fn main() {
4444
.add_plugins(
4545
DefaultPlugins
4646
.set(WindowPlugin {
47-
window: WindowDescriptor {
47+
primary_window: Some(Window {
4848
title: String::from("Benchmark Example"),
4949
..Default::default()
50-
},
50+
}),
5151
..default()
5252
})
5353
.set(ImagePlugin::default_nearest()),

examples/chunking.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ fn despawn_outofrange_chunks(
9090
let chunk_pos = chunk_transform.translation.xy();
9191
let distance = camera_transform.translation.xy().distance(chunk_pos);
9292
if distance > 320.0 {
93-
let x = (chunk_pos.x as f32 / (CHUNK_SIZE.x as f32 * TILE_SIZE.x)).floor() as i32;
94-
let y = (chunk_pos.y as f32 / (CHUNK_SIZE.y as f32 * TILE_SIZE.y)).floor() as i32;
93+
let x = (chunk_pos.x / (CHUNK_SIZE.x as f32 * TILE_SIZE.x)).floor() as i32;
94+
let y = (chunk_pos.y / (CHUNK_SIZE.y as f32 * TILE_SIZE.y)).floor() as i32;
9595
chunk_manager.spawned_chunks.remove(&IVec2::new(x, y));
9696
commands.entity(entity).despawn_recursive();
9797
}
@@ -109,10 +109,10 @@ fn main() {
109109
.add_plugins(
110110
DefaultPlugins
111111
.set(WindowPlugin {
112-
window: WindowDescriptor {
112+
primary_window: Some(Window {
113113
title: String::from("Basic Chunking Example"),
114114
..Default::default()
115-
},
115+
}),
116116
..default()
117117
})
118118
.set(ImagePlugin::default_nearest()),

examples/colors.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,10 @@ fn main() {
9595
.add_plugins(
9696
DefaultPlugins
9797
.set(WindowPlugin {
98-
window: WindowDescriptor {
98+
primary_window: Some(Window {
9999
title: String::from("Color Example"),
100100
..Default::default()
101-
},
101+
}),
102102
..default()
103103
})
104104
.set(ImagePlugin::default_nearest()),

examples/frustum_cull_test.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -119,15 +119,15 @@ pub struct MapTypeLabel;
119119
fn spawn_map_type_label(
120120
mut commands: Commands,
121121
font_handle: Res<FontHandle>,
122-
windows: Res<Windows>,
122+
windows: Query<&Window>,
123123
map_type_q: Query<&TilemapType>,
124124
) {
125125
let text_style = TextStyle {
126126
font: font_handle.clone(),
127127
font_size: 20.0,
128128
color: Color::BLACK,
129129
};
130-
let text_alignment = TextAlignment::CENTER;
130+
let text_alignment = TextAlignment::Center;
131131

132132
for window in windows.iter() {
133133
for map_type in map_type_q.iter() {
@@ -227,10 +227,10 @@ fn main() {
227227
.add_plugins(
228228
DefaultPlugins
229229
.set(WindowPlugin {
230-
window: WindowDescriptor {
230+
primary_window: Some(Window {
231231
title: String::from("Frustum cull test"),
232232
..Default::default()
233-
},
233+
}),
234234
..default()
235235
})
236236
.set(ImagePlugin::default_nearest())
@@ -248,9 +248,8 @@ fn main() {
248248
.init_resource::<TileHandleHexRow>()
249249
.init_resource::<TileHandleSquare>()
250250
.init_resource::<FontHandle>()
251-
.add_startup_system(spawn_tilemap)
252-
.add_startup_system_to_stage(StartupStage::PostStartup, spawn_map_type_label)
253-
.add_system_to_stage(CoreStage::First, camera_movement)
251+
.add_startup_systems((spawn_tilemap, apply_system_buffers, spawn_map_type_label).chain())
252+
.add_system(camera_movement.in_base_set(CoreSet::First))
254253
.add_system(swap_map_type)
255254
.run();
256255
}

examples/game_of_life.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,10 @@ fn main() {
9898
.add_plugins(
9999
DefaultPlugins
100100
.set(WindowPlugin {
101-
window: WindowDescriptor {
101+
primary_window: Some(Window {
102102
title: String::from("Game of Life Example"),
103103
..Default::default()
104-
},
104+
}),
105105
..default()
106106
})
107107
.set(ImagePlugin::default_nearest()),

examples/helpers/ldtk.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,7 @@ pub fn process_loaded_tile_maps(
110110
log::info!("Map removed!");
111111
// if mesh was modified and removed in the same update, ignore the modification
112112
// events are ordered so future modification events are ok
113-
changed_maps = changed_maps
114-
.into_iter()
115-
.filter(|changed_handle| changed_handle == handle)
116-
.collect();
113+
changed_maps.retain(|changed_handle| changed_handle == handle);
117114
}
118115
}
119116
}
@@ -140,7 +137,7 @@ pub fn process_loaded_tile_maps(
140137
tileset.uid,
141138
(
142139
ldtk_map.tilesets.get(&tileset.uid).unwrap().clone(),
143-
tileset.clone(),
140+
tileset,
144141
),
145142
);
146143
});

examples/helpers/tiled.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -284,13 +284,12 @@ pub fn process_loaded_maps(
284284
let mapped_x = x as i32;
285285
let mapped_y = mapped_y as i32;
286286

287-
let layer_tile =
288-
match layer_data.get_tile(mapped_x as i32, mapped_y as i32) {
289-
Some(t) => t,
290-
None => {
291-
continue;
292-
}
293-
};
287+
let layer_tile = match layer_data.get_tile(mapped_x, mapped_y) {
288+
Some(t) => t,
289+
None => {
290+
continue;
291+
}
292+
};
294293
if tileset_index != layer_tile.tileset_index() {
295294
continue;
296295
}

examples/hex_neighbors.rs

Lines changed: 21 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ fn spawn_tile_labels(
9898
font_size: 20.0,
9999
color: Color::BLACK,
100100
};
101-
let text_alignment = TextAlignment::CENTER;
101+
let text_alignment = TextAlignment::Center;
102102
for (map_transform, map_type, grid_size, tilemap_storage) in tilemap_q.iter() {
103103
for tile_entity in tilemap_storage.iter().flatten() {
104104
let tile_pos = tile_q.get(*tile_entity).unwrap();
@@ -130,15 +130,15 @@ pub struct MapTypeLabel;
130130
fn spawn_map_type_label(
131131
mut commands: Commands,
132132
font_handle: Res<FontHandle>,
133-
windows: Res<Windows>,
133+
windows: Query<&Window>,
134134
map_type_q: Query<&TilemapType>,
135135
) {
136136
let text_style = TextStyle {
137137
font: font_handle.clone(),
138138
font_size: 20.0,
139139
color: Color::BLACK,
140140
};
141-
let text_alignment = TextAlignment::CENTER;
141+
let text_alignment = TextAlignment::Center;
142142

143143
for window in windows.iter() {
144144
for map_type in map_type_q.iter() {
@@ -220,7 +220,7 @@ fn swap_map_type(
220220
_ => unreachable!(),
221221
}
222222

223-
*map_transform = get_tilemap_center_transform(&map_size, &grid_size, &map_type, 0.0);
223+
*map_transform = get_tilemap_center_transform(map_size, &grid_size, &map_type, 0.0);
224224

225225
for (label, tile_pos) in tile_label_q.iter() {
226226
if let Ok(mut tile_label_transform) = transform_q.get_mut(label.0) {
@@ -240,39 +240,19 @@ fn swap_map_type(
240240
#[derive(Component)]
241241
struct Hovered;
242242

243-
// Converts the cursor position into a world position, taking into account any transforms applied
244-
// the camera.
245-
pub fn cursor_pos_in_world(
246-
windows: &Windows,
247-
cursor_pos: Vec2,
248-
cam_t: &Transform,
249-
cam: &Camera,
250-
) -> Vec3 {
251-
let window = windows.primary();
252-
253-
let window_size = Vec2::new(window.width(), window.height());
254-
255-
// Convert screen position [0..resolution] to ndc [-1..1]
256-
// (ndc = normalized device coordinates)
257-
let ndc_to_world = cam_t.compute_matrix() * cam.projection_matrix().inverse();
258-
let ndc = (cursor_pos / window_size) * 2.0 - Vec2::ONE;
259-
ndc_to_world.project_point3(ndc.extend(0.0))
260-
}
261-
262243
#[derive(Resource)]
263-
pub struct CursorPos(Vec3);
244+
pub struct CursorPos(Vec2);
264245
impl Default for CursorPos {
265246
fn default() -> Self {
266247
// Initialize the cursor pos at some far away place. It will get updated
267248
// correctly when the cursor moves.
268-
Self(Vec3::new(-1000.0, -1000.0, 0.0))
249+
Self(Vec2::new(-1000.0, -1000.0))
269250
}
270251
}
271252

272253
// We need to keep the cursor position updated based on any `CursorMoved` events.
273254
pub fn update_cursor_pos(
274-
windows: Res<Windows>,
275-
camera_q: Query<(&Transform, &Camera)>,
255+
camera_q: Query<(&GlobalTransform, &Camera)>,
276256
mut cursor_moved_events: EventReader<CursorMoved>,
277257
mut cursor_pos: ResMut<CursorPos>,
278258
) {
@@ -281,12 +261,9 @@ pub fn update_cursor_pos(
281261
// any transforms on the camera. This is done by projecting the cursor position into
282262
// camera space (world space).
283263
for (cam_t, cam) in camera_q.iter() {
284-
*cursor_pos = CursorPos(cursor_pos_in_world(
285-
&windows,
286-
cursor_moved.position,
287-
cam_t,
288-
cam,
289-
));
264+
if let Some(pos) = cam.viewport_to_world_2d(cam_t, cursor_moved.position) {
265+
*cursor_pos = CursorPos(pos);
266+
}
290267
}
291268
}
292269
}
@@ -320,12 +297,12 @@ fn hover_highlight_tile_label(
320297

321298
for (map_size, grid_size, map_type, tile_storage, map_transform) in tilemap_q.iter() {
322299
// Grab the cursor position from the `Res<CursorPos>`
323-
let cursor_pos: Vec3 = cursor_pos.0;
300+
let cursor_pos: Vec2 = cursor_pos.0;
324301
// We need to make sure that the cursor's world position is correct relative to the map
325302
// due to any map transformation.
326303
let cursor_in_map_pos: Vec2 = {
327-
// Extend the cursor_pos vec3 by 1.0
328-
let cursor_pos = Vec4::from((cursor_pos, 1.0));
304+
// Extend the cursor_pos vec2 by 0.0 and 1.0
305+
let cursor_pos = Vec4::from((cursor_pos, 0.0, 1.0));
329306
let cursor_in_map_pos = map_transform.compute_matrix().inverse() * cursor_pos;
330307
cursor_in_map_pos.xy()
331308
};
@@ -446,17 +423,20 @@ fn highlight_neighbor_label(
446423
}
447424
}
448425

426+
#[derive(SystemSet, Clone, Copy, Hash, PartialEq, Eq, Debug)]
427+
pub struct SpawnTilemapSet;
428+
449429
fn main() {
450430
App::new()
451431
.add_plugins(
452432
DefaultPlugins
453433
.set(WindowPlugin {
454-
window: WindowDescriptor {
434+
primary_window: Some(Window {
455435
title: String::from(
456436
"Hexagon Neighbors - Hover over a tile, and then press 0-5 to mark neighbors",
457437
),
458438
..Default::default()
459-
},
439+
}),
460440
..default()
461441
})
462442
.set(ImagePlugin::default_nearest()),
@@ -466,11 +446,9 @@ fn main() {
466446
.init_resource::<TileHandleHexCol>()
467447
.init_resource::<TileHandleHexRow>()
468448
.init_resource::<FontHandle>()
469-
.add_startup_system(spawn_tilemap)
470-
.add_startup_system_to_stage(StartupStage::PostStartup, spawn_tile_labels)
471-
.add_startup_system_to_stage(StartupStage::PostStartup, spawn_map_type_label)
472-
.add_system_to_stage(CoreStage::First, camera_movement)
473-
.add_system_to_stage(CoreStage::First, update_cursor_pos.after(camera_movement))
449+
.add_startup_systems((spawn_tilemap, apply_system_buffers).chain().in_set(SpawnTilemapSet))
450+
.add_startup_systems((spawn_tile_labels, spawn_map_type_label).after(SpawnTilemapSet))
451+
.add_systems((camera_movement, update_cursor_pos).chain().in_base_set(CoreSet::First))
474452
.add_system(swap_map_type)
475453
.add_system(hover_highlight_tile_label.after(swap_map_type))
476454
.add_system(highlight_neighbor_label.after(hover_highlight_tile_label))

0 commit comments

Comments
 (0)