Skip to content

Update to Bevy 0.10 #390

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Mar 17, 2023
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ atlas = []
render = []

[dependencies]
bevy = { version = "0.9", default-features = false, features = [
bevy = { version = "0.10", default-features = false, features = [
"bevy_core_pipeline",
"bevy_render",
"bevy_asset",
Expand All @@ -33,7 +33,7 @@ serde_json = { version = "1.0" }
tiled = { version = "0.10.2", default-features = false }

[dev-dependencies.bevy]
version = "0.9"
version = "0.10"
default-features = false
features = [
"bevy_core_pipeline",
Expand All @@ -48,7 +48,7 @@ features = [
]

[target.'cfg(unix)'.dev-dependencies.bevy]
version = "0.9"
version = "0.10"
default-features = false
features = [
"bevy_core_pipeline",
Expand Down
4 changes: 2 additions & 2 deletions examples/accessing_tiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,10 @@ fn main() {
.add_plugins(
DefaultPlugins
.set(WindowPlugin {
window: WindowDescriptor {
primary_window: Some(Window {
title: String::from("Accessing Tiles Example"),
..Default::default()
},
}),
..default()
})
.set(ImagePlugin::default_nearest()),
Expand Down
4 changes: 2 additions & 2 deletions examples/animation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,10 @@ fn main() {
.add_plugins(
DefaultPlugins
.set(WindowPlugin {
window: WindowDescriptor {
primary_window: Some(Window {
title: String::from("Animated Map Example"),
..Default::default()
},
}),
..default()
})
.set(ImagePlugin::default_nearest()),
Expand Down
13 changes: 6 additions & 7 deletions examples/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,24 +90,23 @@ fn swap_texture_or_hide(
}
if keyboard_input.just_pressed(KeyCode::H) {
for (_, mut visibility) in &mut query {
if visibility.is_visible {
visibility.is_visible = false;
} else {
visibility.is_visible = true;
}
*visibility = match *visibility {
Visibility::Inherited | Visibility::Visible => Visibility::Hidden,
Visibility::Hidden => Visibility::Visible,
};
}
}
}

fn main() {
App::new()
.add_plugins(DefaultPlugins.set(WindowPlugin{
window: WindowDescriptor {
primary_window: Some(Window {
title: String::from(
"Basic Example - Press Space to change Texture and H to show/hide tilemap.",
),
..Default::default()
},
}),
..default()
}).set(ImagePlugin::default_nearest()))
.add_plugin(TilemapPlugin)
Expand Down
4 changes: 2 additions & 2 deletions examples/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ fn main() {
.add_plugins(
DefaultPlugins
.set(WindowPlugin {
window: WindowDescriptor {
primary_window: Some(Window {
title: String::from("Benchmark Example"),
..Default::default()
},
}),
..default()
})
.set(ImagePlugin::default_nearest()),
Expand Down
8 changes: 4 additions & 4 deletions examples/chunking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ fn despawn_outofrange_chunks(
let chunk_pos = chunk_transform.translation.xy();
let distance = camera_transform.translation.xy().distance(chunk_pos);
if distance > 320.0 {
let x = (chunk_pos.x as f32 / (CHUNK_SIZE.x as f32 * TILE_SIZE.x)).floor() as i32;
let y = (chunk_pos.y as f32 / (CHUNK_SIZE.y as f32 * TILE_SIZE.y)).floor() as i32;
let x = (chunk_pos.x / (CHUNK_SIZE.x as f32 * TILE_SIZE.x)).floor() as i32;
let y = (chunk_pos.y / (CHUNK_SIZE.y as f32 * TILE_SIZE.y)).floor() as i32;
chunk_manager.spawned_chunks.remove(&IVec2::new(x, y));
commands.entity(entity).despawn_recursive();
}
Expand All @@ -109,10 +109,10 @@ fn main() {
.add_plugins(
DefaultPlugins
.set(WindowPlugin {
window: WindowDescriptor {
primary_window: Some(Window {
title: String::from("Basic Chunking Example"),
..Default::default()
},
}),
..default()
})
.set(ImagePlugin::default_nearest()),
Expand Down
4 changes: 2 additions & 2 deletions examples/colors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,10 @@ fn main() {
.add_plugins(
DefaultPlugins
.set(WindowPlugin {
window: WindowDescriptor {
primary_window: Some(Window {
title: String::from("Color Example"),
..Default::default()
},
}),
..default()
})
.set(ImagePlugin::default_nearest()),
Expand Down
13 changes: 6 additions & 7 deletions examples/frustum_cull_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,15 @@ pub struct MapTypeLabel;
fn spawn_map_type_label(
mut commands: Commands,
font_handle: Res<FontHandle>,
windows: Res<Windows>,
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_alignment = TextAlignment::CENTER;
let text_alignment = TextAlignment::Center;

for window in windows.iter() {
for map_type in map_type_q.iter() {
Expand Down Expand Up @@ -227,10 +227,10 @@ fn main() {
.add_plugins(
DefaultPlugins
.set(WindowPlugin {
window: WindowDescriptor {
primary_window: Some(Window {
title: String::from("Frustum cull test"),
..Default::default()
},
}),
..default()
})
.set(ImagePlugin::default_nearest())
Expand All @@ -248,9 +248,8 @@ fn main() {
.init_resource::<TileHandleHexRow>()
.init_resource::<TileHandleSquare>()
.init_resource::<FontHandle>()
.add_startup_system(spawn_tilemap)
.add_startup_system_to_stage(StartupStage::PostStartup, spawn_map_type_label)
.add_system_to_stage(CoreStage::First, camera_movement)
.add_startup_systems((spawn_tilemap, apply_system_buffers, spawn_map_type_label).chain())
.add_system(camera_movement.in_base_set(CoreSet::First))
.add_system(swap_map_type)
.run();
}
4 changes: 2 additions & 2 deletions examples/game_of_life.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,10 @@ fn main() {
.add_plugins(
DefaultPlugins
.set(WindowPlugin {
window: WindowDescriptor {
primary_window: Some(Window {
title: String::from("Game of Life Example"),
..Default::default()
},
}),
..default()
})
.set(ImagePlugin::default_nearest()),
Expand Down
7 changes: 2 additions & 5 deletions examples/helpers/ldtk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,7 @@ pub fn process_loaded_tile_maps(
log::info!("Map removed!");
// if mesh was modified and removed in the same update, ignore the modification
// events are ordered so future modification events are ok
changed_maps = changed_maps
.into_iter()
.filter(|changed_handle| changed_handle == handle)
.collect();
changed_maps.retain(|changed_handle| changed_handle == handle);
}
}
}
Expand All @@ -140,7 +137,7 @@ pub fn process_loaded_tile_maps(
tileset.uid,
(
ldtk_map.tilesets.get(&tileset.uid).unwrap().clone(),
tileset.clone(),
tileset,
),
);
});
Expand Down
13 changes: 6 additions & 7 deletions examples/helpers/tiled.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,13 +284,12 @@ pub fn process_loaded_maps(
let mapped_x = x as i32;
let mapped_y = mapped_y as i32;

let layer_tile =
match layer_data.get_tile(mapped_x as i32, mapped_y as i32) {
Some(t) => t,
None => {
continue;
}
};
let layer_tile = match layer_data.get_tile(mapped_x, mapped_y) {
Some(t) => t,
None => {
continue;
}
};
if tileset_index != layer_tile.tileset_index() {
continue;
}
Expand Down
64 changes: 21 additions & 43 deletions examples/hex_neighbors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ fn spawn_tile_labels(
font_size: 20.0,
color: Color::BLACK,
};
let text_alignment = TextAlignment::CENTER;
let text_alignment = TextAlignment::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 Down Expand Up @@ -130,15 +130,15 @@ pub struct MapTypeLabel;
fn spawn_map_type_label(
mut commands: Commands,
font_handle: Res<FontHandle>,
windows: Res<Windows>,
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_alignment = TextAlignment::CENTER;
let text_alignment = TextAlignment::Center;

for window in windows.iter() {
for map_type in map_type_q.iter() {
Expand Down Expand Up @@ -220,7 +220,7 @@ fn swap_map_type(
_ => unreachable!(),
}

*map_transform = get_tilemap_center_transform(&map_size, &grid_size, &map_type, 0.0);
*map_transform = get_tilemap_center_transform(map_size, &grid_size, &map_type, 0.0);

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

// Converts the cursor position into a world position, taking into account any transforms applied
// the camera.
pub fn cursor_pos_in_world(
windows: &Windows,
cursor_pos: Vec2,
cam_t: &Transform,
cam: &Camera,
) -> Vec3 {
let window = windows.primary();

let window_size = Vec2::new(window.width(), window.height());

// Convert screen position [0..resolution] to ndc [-1..1]
// (ndc = normalized device coordinates)
let ndc_to_world = cam_t.compute_matrix() * cam.projection_matrix().inverse();
let ndc = (cursor_pos / window_size) * 2.0 - Vec2::ONE;
ndc_to_world.project_point3(ndc.extend(0.0))
}

#[derive(Resource)]
pub struct CursorPos(Vec3);
pub struct CursorPos(Vec2);
impl Default for CursorPos {
fn default() -> Self {
// Initialize the cursor pos at some far away place. It will get updated
// correctly when the cursor moves.
Self(Vec3::new(-1000.0, -1000.0, 0.0))
Self(Vec2::new(-1000.0, -1000.0))
}
}

// We need to keep the cursor position updated based on any `CursorMoved` events.
pub fn update_cursor_pos(
windows: Res<Windows>,
camera_q: Query<(&Transform, &Camera)>,
camera_q: Query<(&GlobalTransform, &Camera)>,
mut cursor_moved_events: EventReader<CursorMoved>,
mut cursor_pos: ResMut<CursorPos>,
) {
Expand All @@ -281,12 +261,9 @@ 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() {
*cursor_pos = CursorPos(cursor_pos_in_world(
&windows,
cursor_moved.position,
cam_t,
cam,
));
if let Some(pos) = cam.viewport_to_world_2d(cam_t, cursor_moved.position) {
*cursor_pos = CursorPos(pos);
}
}
}
}
Expand Down Expand Up @@ -320,12 +297,12 @@ fn hover_highlight_tile_label(

for (map_size, grid_size, map_type, tile_storage, map_transform) in tilemap_q.iter() {
// Grab the cursor position from the `Res<CursorPos>`
let cursor_pos: Vec3 = cursor_pos.0;
let cursor_pos: Vec2 = cursor_pos.0;
// We need to make sure that the cursor's world position is correct relative to the map
// due to any map transformation.
let cursor_in_map_pos: Vec2 = {
// Extend the cursor_pos vec3 by 1.0
let cursor_pos = Vec4::from((cursor_pos, 1.0));
// Extend the cursor_pos vec2 by 0.0 and 1.0
let cursor_pos = Vec4::from((cursor_pos, 0.0, 1.0));
let cursor_in_map_pos = map_transform.compute_matrix().inverse() * cursor_pos;
cursor_in_map_pos.xy()
};
Expand Down Expand Up @@ -446,17 +423,20 @@ fn highlight_neighbor_label(
}
}

#[derive(SystemSet, Clone, Copy, Hash, PartialEq, Eq, Debug)]
pub struct SpawnTilemapSet;

fn main() {
App::new()
.add_plugins(
DefaultPlugins
.set(WindowPlugin {
window: WindowDescriptor {
primary_window: Some(Window {
title: String::from(
"Hexagon Neighbors - Hover over a tile, and then press 0-5 to mark neighbors",
),
..Default::default()
},
}),
..default()
})
.set(ImagePlugin::default_nearest()),
Expand All @@ -466,11 +446,9 @@ fn main() {
.init_resource::<TileHandleHexCol>()
.init_resource::<TileHandleHexRow>()
.init_resource::<FontHandle>()
.add_startup_system(spawn_tilemap)
.add_startup_system_to_stage(StartupStage::PostStartup, spawn_tile_labels)
.add_startup_system_to_stage(StartupStage::PostStartup, spawn_map_type_label)
.add_system_to_stage(CoreStage::First, camera_movement)
.add_system_to_stage(CoreStage::First, update_cursor_pos.after(camera_movement))
.add_startup_systems((spawn_tilemap, apply_system_buffers).chain().in_set(SpawnTilemapSet))
.add_startup_systems((spawn_tile_labels, spawn_map_type_label).after(SpawnTilemapSet))
.add_systems((camera_movement, update_cursor_pos).chain().in_base_set(CoreSet::First))
.add_system(swap_map_type)
.add_system(hover_highlight_tile_label.after(swap_map_type))
.add_system(highlight_neighbor_label.after(hover_highlight_tile_label))
Expand Down
Loading