Skip to content

Commit

Permalink
Upgrade to Bevy v0.13 (#790)
Browse files Browse the repository at this point in the history
Migrated with respect to
https://bevyengine.org/learn/migration-guides/0-12-to-0-13/

Most notable changes are:

* All schedules (setup in `schedule.rs`) has been configured with
`auto_insert_apply_deferred: false` so that commands are applied only in
between individual schedules. This behavior is equal to the behavior
before the upgrade. This is required because the presence of components
of despawned entities is relied upon by e.g. `DespawnEventsPlugin`.

* Light brightness (setup in `map.rs`) was adjusted so it roughly
visually matches the lighting before the Bevy upgrade.

* Trail line transform (in `line.rs`) was updated since the base mesh is
now twice as big.

* Implementation of `DespawnEventsPlugin` and related events and systems
was changed because `Query::get_component::<T>()` is now deprecated. The
code was simplified and the despawn events are limited to a single
component only.
  • Loading branch information
Indy2222 authored Feb 24, 2024
1 parent 279488b commit 519f19c
Show file tree
Hide file tree
Showing 51 changed files with 796 additions and 539 deletions.
804 changes: 513 additions & 291 deletions Cargo.lock

Large diffs are not rendered by default.

13 changes: 7 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ assert_cmd = "2.0.10"
async-compat = "0.2.1"
async-std = "1.11"
async-tar = "0.4.2"
bevy_kira_audio = { version = "0.18", features = ["mp3"] }
bevy_kira_audio = { version = "0.19", features = ["mp3"] }
bincode = "2.0.0-rc.3"
chrono = "0.4.24"
clap = { version = "4.0", features = ["derive"] }
Expand All @@ -131,13 +131,12 @@ fastrand = "1.9.0"
fern = "0.6.2"
flate2 = "1.0.26"
futures = "0.3.28"
futures-lite = "1.13.0"
glam = "0.24"
glam = "0.25"
gltf = "1.0"
itertools = "0.11.0"
iyes_progress = "0.10.0"
iyes_progress = "0.11.0"
log = "0.4.17"
nalgebra = { version = "0.32.3", features = ["convert-glam024"] }
nalgebra = { version = "0.32.4", features = ["convert-glam025"] }
nix = "0.26.2"
ntest = "0.9.0"
parry2d = "0.13.1"
Expand All @@ -164,15 +163,17 @@ url = { version = "2.3.1", features = ["serde"] }
urlencoding = "2.1.2"

[workspace.dependencies.bevy]
version = "0.12"
version = "0.13"
default-features = false
features = [
"animation",
"bevy_animation",
"bevy_asset",
"bevy_gilrs",
"bevy_scene",
"bevy_winit",
"bevy_core_pipeline",
"bevy_debug_stepping",
"bevy_pbr",
"bevy_gltf",
"bevy_render",
Expand Down
2 changes: 1 addition & 1 deletion assets/shaders/bar.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
const BACKGROUND_COLOR = vec4<f32>(0., 0., 0., 0.75);
const FOREGROUND_COLOR = vec4<f32>(0.6, 1., 0.6, 0.75);

@group(1) @binding(0)
@group(2) @binding(0)
var<uniform> value: f32;

struct Vertex {
Expand Down
2 changes: 1 addition & 1 deletion assets/shaders/rally_point.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ struct CustomMaterial {
fade: f32,
};

@group(1) @binding(0)
@group(2) @binding(0)
var<uniform> material: CustomMaterial;

const COLOR: vec4<f32> = vec4<f32>(0.0, 0.5, 0.0, 0.8);
Expand Down
6 changes: 3 additions & 3 deletions assets/shaders/terrain.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ struct Rectangles {
count: u32,
};

@group(1) @binding(100)
@group(2) @binding(100)
var<uniform> uv_scale: f32;
@group(1) @binding(101)
@group(2) @binding(101)
var<uniform> circles: KdTree;
@group(1) @binding(102)
@group(2) @binding(102)
var<uniform> rectangles: Rectangles;
fn mix_colors(base: vec4<f32>, cover: vec4<f32>) -> vec4<f32> {
let alpha = base.a * cover.a;
Expand Down
2 changes: 1 addition & 1 deletion assets/shaders/trail.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

const COLOR = vec4<f32>(1., 0.85, 0.1, 0.7);

@group(1) @binding(0)
@group(2) @binding(0)
var<uniform> start_time: f32;

@fragment
Expand Down
17 changes: 10 additions & 7 deletions crates/camera/src/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,11 @@ impl Plugin for CameraPlugin {
zoom.in_set(InternalCameraSet::Zoom),
update_shadows
.after(InternalCameraSet::Zoom)
.run_if(resource_exists_and_changed::<CameraFocus>()),
.run_if(resource_exists_and_changed::<CameraFocus>),
pivot
.run_if(
resource_exists_and_changed::<DesiredOffNadir>()
.or_else(resource_exists_and_changed::<DesiredAzimuth>()),
resource_exists_and_changed::<DesiredOffNadir>
.or_else(resource_exists_and_changed::<DesiredAzimuth>),
)
.in_set(InternalCameraSet::Pivot),
move_horizontaly
Expand Down Expand Up @@ -370,9 +370,10 @@ fn update_focus(
camera_query: Query<&Transform, With<Camera3d>>,
) {
let camera_transform = camera_query.single();
let forward = camera_transform.forward();
let ray = Ray::new(
camera_transform.translation.into(),
camera_transform.forward().into(),
Vector::new(forward.x, forward.y, forward.z),
);

let intersection = terrain
Expand Down Expand Up @@ -408,7 +409,8 @@ fn update_translation_handler(
mut camera_query: Query<&mut Transform, With<Camera3d>>,
) {
let mut transform = camera_query.single_mut();
transform.translation = focus.point() + f32::from(focus.distance()) * transform.back();
let back = Vec3::from(transform.back());
transform.translation = focus.point() + f32::from(focus.distance()) * back;
}

fn move_horizontaly(
Expand Down Expand Up @@ -475,7 +477,7 @@ fn zoom(
}

let mut transform = camera_query.single_mut();
let delta_vec = f32::from(delta_scalar) * transform.forward();
let delta_vec = f32::from(delta_scalar) * Vec3::from(transform.forward());
transform.translation += delta_vec;
focus.update_distance(delta_scalar);
}
Expand Down Expand Up @@ -508,7 +510,8 @@ fn pivot(
(desired_off_nadir.off_nadir() - Radian::FRAC_PI_2).into(),
0.,
);
transform.translation = focus.point() - f32::from(focus.distance()) * transform.forward();
let forward = Vec3::from(transform.forward());
transform.translation = focus.point() - f32::from(focus.distance()) * forward;
}

fn handle_horizontal_events(
Expand Down
7 changes: 4 additions & 3 deletions crates/camera/src/skybox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ fn setup_camera(
if configured {
return;
}
commands
.entity(entity)
.insert(Skybox(skybox.handle.clone()));
commands.entity(entity).insert(Skybox {
image: skybox.handle.clone(),
brightness: 300.,
});
}
8 changes: 6 additions & 2 deletions crates/combat/src/trail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use bevy::{
reflect::TypePath,
render::{
mesh::{Indices, MeshVertexBufferLayout, PrimitiveTopology},
render_asset::RenderAssetUsages,
render_resource::{
AsBindGroup, RenderPipelineDescriptor, ShaderRef, SpecializedMeshPipelineError,
},
Expand Down Expand Up @@ -256,10 +257,13 @@ fn generate_trail_mesh() -> Mesh {

let indices = Indices::U16(vec![0, 1, 2, 0, 2, 3, 4, 5, 6, 4, 6, 7]);

let mut mesh = Mesh::new(PrimitiveTopology::TriangleList);
let mut mesh = Mesh::new(
PrimitiveTopology::TriangleList,
RenderAssetUsages::RENDER_WORLD,
);
mesh.insert_attribute(Mesh::ATTRIBUTE_POSITION, positions);
mesh.insert_attribute(Mesh::ATTRIBUTE_NORMAL, normals);
mesh.insert_attribute(Mesh::ATTRIBUTE_UV_0, uvs);
mesh.set_indices(Some(indices));
mesh.insert_indices(indices);
mesh
}
1 change: 0 additions & 1 deletion crates/conf/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ anyhow.workspace = true
async-std.workspace = true
bevy.workspace = true
dirs.workspace = true
futures-lite.workspace = true
iyes_progress.workspace = true
paste.workspace = true
serde.workspace = true
Expand Down
3 changes: 1 addition & 2 deletions crates/conf/src/plugin.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use bevy::{
prelude::*,
tasks::{IoTaskPool, Task},
tasks::{futures_lite::future, IoTaskPool, Task},
};
use de_core::fs::conf_dir;
use de_core::state::AppState;
use de_gui::ToastEvent;
use futures_lite::future;
use iyes_progress::prelude::*;
use tracing::error;

Expand Down
36 changes: 17 additions & 19 deletions crates/controller/src/commands/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ pub(super) struct HandlersPlugin;
impl HandlersPlugin {
fn add_place_draft_systems(app: &mut App) {
let key_map = enum_map! {
BuildingType::Base => KeyCode::B,
BuildingType::PowerHub => KeyCode::P,
BuildingType::Base => KeyCode::KeyB,
BuildingType::PowerHub => KeyCode::KeyP,
};

for (building_type, &key) in key_map.iter() {
Expand Down Expand Up @@ -108,11 +108,11 @@ impl Plugin for HandlersPlugin {
.before(GameMenuSet::Toggle)
.before(DraftSet::Discard),
select_all
.run_if(KeyCondition::single(KeyCode::A).with_ctrl().build())
.run_if(KeyCondition::single(KeyCode::KeyA).with_ctrl().build())
.before(SelectionSet::Update),
select_all_visible
.run_if(
KeyCondition::single(KeyCode::A)
KeyCondition::single(KeyCode::KeyA)
.with_ctrl()
.with_shift()
.build(),
Expand Down Expand Up @@ -164,7 +164,9 @@ fn right_click_handler(
.map(|&player| !config.locals().is_playable(*player))
.unwrap_or(false)
}) {
Some(enemy) => attack_events.send(GroupAttackEvent::new(enemy)),
Some(enemy) => {
attack_events.send(GroupAttackEvent::new(enemy));
}
None => {
let Some(target) = pointer.terrain_point().map(|p| p.to_flat()) else {
return;
Expand All @@ -176,7 +178,7 @@ fn right_click_handler(
}

fn double_click_handler(
keys: Res<Input<KeyCode>>,
keys: Res<ButtonInput<KeyCode>>,
pointer: Res<Pointer>,
playable: Query<&ObjectTypeComponent, With<Playable>>,
drafts: Query<(), With<DraftAllowed>>,
Expand Down Expand Up @@ -211,18 +213,14 @@ fn move_camera_arrows_system(
mut move_events: EventWriter<MoveCameraHorizontallyEvent>,
) {
for key_event in key_events.read() {
let Some(key_code) = key_event.key_code else {
continue;
};

let mut direction = Vec2::ZERO;
if key_code == KeyCode::Left {
if key_event.key_code == KeyCode::ArrowLeft {
direction = Vec2::new(-1., 0.);
} else if key_code == KeyCode::Right {
} else if key_event.key_code == KeyCode::ArrowRight {
direction = Vec2::new(1., 0.);
} else if key_code == KeyCode::Down {
} else if key_event.key_code == KeyCode::ArrowDown {
direction = Vec2::new(0., -1.);
} else if key_code == KeyCode::Up {
} else if key_event.key_code == KeyCode::ArrowUp {
direction = Vec2::new(0., 1.);
}

Expand Down Expand Up @@ -287,8 +285,8 @@ fn zoom_camera(

fn pivot_camera(
conf: Res<Configuration>,
buttons: Res<Input<MouseButton>>,
keys: Res<Input<KeyCode>>,
buttons: Res<ButtonInput<MouseButton>>,
keys: Res<ButtonInput<KeyCode>>,
mut mouse_event: EventReader<MouseMotion>,
mut rotate_event: EventWriter<RotateCameraEvent>,
mut tilt_event: EventWriter<TiltCameraEvent>,
Expand All @@ -310,7 +308,7 @@ fn pivot_camera(
fn left_click_handler(
mut select_events: EventWriter<SelectEvent>,
mut draft_events: EventWriter<SpawnDraftsEvent>,
keys: Res<Input<KeyCode>>,
keys: Res<ButtonInput<KeyCode>>,
pointer: Res<Pointer>,
playable: Query<(), With<Playable>>,
drafts: Query<(), With<DraftAllowed>>,
Expand Down Expand Up @@ -385,7 +383,7 @@ fn select_all_visible(mut events: EventWriter<SelectInRectEvent>) {
}

fn update_drags(
keys: Res<Input<KeyCode>>,
keys: Res<ButtonInput<KeyCode>>,
mut drag_events: EventReader<MouseDraggedEvent>,
mut ui_events: EventWriter<UpdateSelectionBoxEvent>,
mut select_events: EventWriter<SelectInRectEvent>,
Expand Down Expand Up @@ -416,6 +414,6 @@ fn update_drags(
}
};

ui_events.send(ui_event)
ui_events.send(ui_event);
}
}
10 changes: 5 additions & 5 deletions crates/controller/src/commands/keyboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ impl KeyCondition {
self
}

pub(super) fn build(self) -> impl Fn(Res<Input<KeyCode>>, EventReader<KeyboardInput>) -> bool {
move |keys: Res<Input<KeyCode>>, mut events: EventReader<KeyboardInput>| {
pub(super) fn build(
self,
) -> impl Fn(Res<ButtonInput<KeyCode>>, EventReader<KeyboardInput>) -> bool {
move |keys: Res<ButtonInput<KeyCode>>, mut events: EventReader<KeyboardInput>| {
let proper_key = events
.read()
.filter(|k| {
k.state == ButtonState::Pressed && k.key_code.map_or(false, |c| c == self.key)
})
.filter(|k| k.state == ButtonState::Pressed && k.key_code == self.key)
.count()
> 0;

Expand Down
2 changes: 1 addition & 1 deletion crates/controller/src/frustum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl<'w, 's> ScreenFrustum<'w, 's> {
half_spaces[i] = HalfSpace::new(norm.extend(-transform.translation.dot(norm)));
}

let forward = transform.forward();
let forward = Vec3::from(transform.forward());
let near_dist = -forward.dot(transform.translation + projection.near * forward);
let far_dist = -forward.dot(transform.translation + projection.far * forward);
half_spaces[4] = HalfSpace::new(forward.extend(near_dist));
Expand Down
2 changes: 1 addition & 1 deletion crates/controller/src/hud/actionbar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl Plugin for ActionBarPlugin {
(
detect_update.in_set(ActionBarSet::DetectUpdate),
update
.run_if(resource_exists_and_changed::<ActiveEntity>())
.run_if(resource_exists_and_changed::<ActiveEntity>)
.after(ActionBarSet::DetectUpdate),
)
.run_if(in_state(GameState::Playing)),
Expand Down
6 changes: 3 additions & 3 deletions crates/controller/src/hud/interaction.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use bevy::{
ecs::{query::ReadOnlyWorldQuery, system::SystemParam},
ecs::{query::QueryFilter, system::SystemParam},
prelude::*,
};

Expand All @@ -15,7 +15,7 @@ pub(crate) struct InteractionBlocker;
#[derive(SystemParam)]
pub(crate) struct HudNodes<'w, 's, F = With<InteractionBlocker>>
where
F: ReadOnlyWorldQuery + Sync + Send + 'static,
F: QueryFilter + Sync + Send + 'static,
{
hud: Query<
'w,
Expand All @@ -31,7 +31,7 @@ where

impl<'w, 's, F> HudNodes<'w, 's, F>
where
F: ReadOnlyWorldQuery + Sync + Send + 'static,
F: QueryFilter + Sync + Send + 'static,
{
/// See [`Self::relative_position`].
pub(crate) fn contains_point(&self, point: Vec2) -> bool {
Expand Down
6 changes: 5 additions & 1 deletion crates/controller/src/hud/minimap/nodes.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use bevy::{
prelude::*,
render::render_resource::{Extent3d, TextureDimension, TextureFormat},
render::{
render_asset::RenderAssetUsages,
render_resource::{Extent3d, TextureDimension, TextureFormat},
},
};
use de_core::{cleanup::DespawnOnGameExit, gamestate::GameState, schedule::PreMovement};
use de_map::size::MapBounds;
Expand Down Expand Up @@ -101,5 +104,6 @@ fn new_image(resolution: UVec2) -> Image {
TextureDimension::D2,
data,
format,
RenderAssetUsages::RENDER_WORLD | RenderAssetUsages::MAIN_WORLD,
)
}
2 changes: 1 addition & 1 deletion crates/controller/src/mouse/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl Plugin for InputPlugin {
(
update_position.in_set(MouseSet::Position),
update_drags
.run_if(resource_exists_and_changed::<MousePosition>())
.run_if(resource_exists_and_changed::<MousePosition>)
.in_set(MouseSet::Drags)
.after(MouseSet::Position),
update_buttons
Expand Down
Loading

0 comments on commit 519f19c

Please sign in to comment.