Skip to content

Commit

Permalink
Updated Bevy to version 0.9
Browse files Browse the repository at this point in the history
Signed-off-by: TheDudeFromCI <thedudefromci@gmail.com>
  • Loading branch information
TheDudeFromCI committed Nov 16, 2022
1 parent ee05539 commit efd7b35
Show file tree
Hide file tree
Showing 15 changed files with 109 additions and 68 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ keywords = ["game", "game engine", "sandbox", "graphics"]
categories = ["games", "game-engines"]

[dependencies]
bevy = "0.8.1"
bevy = "0.9.0"
awgen_client = { path = "crates/awgen_client", version = "0.1.0" }
awgen_network = { path = "crates/awgen_network", version = "0.1.0" }
awgen_physics = { path = "crates/awgen_physics", version = "0.1.0" }
Expand Down
4 changes: 2 additions & 2 deletions crates/awgen_client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ keywords = ["game", "game engine", "sandbox", "graphics"]
categories = ["games", "game-engines"]

[dependencies]
bevy = "0.8.1"
bevy-inspector-egui = "0.13.0"
bevy = "0.9.0"
bevy-inspector-egui = "0.14.0"
awgen_physics = { path = "../awgen_physics", version = "0.1.0" }
num = "0.4.0"
14 changes: 10 additions & 4 deletions crates/awgen_client/src/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,20 @@
use awgen_physics::prelude::VelocitySource;
use bevy::input::mouse::MouseMotion;
use bevy::prelude::*;
use bevy::window::CursorGrabMode;
use std::f32::consts::PI;


/// A component marker that allows for an entity to supply a velocity force
/// based off of WASD input controls.
#[derive(Reflect, Component, Default)]
#[derive(Debug, Clone, Reflect, Component, Default)]
#[reflect(Component)]
pub struct WasdController;


/// A component that reads a continuous euler rotation based off of mouse
/// movement inputs.
#[derive(Reflect, Component)]
#[derive(Debug, Clone, Reflect, Component)]
#[reflect(Component)]
pub struct MouseController {
/// The mouse sensitivity of this camera controller.
Expand Down Expand Up @@ -49,7 +50,7 @@ impl Default for MouseController {

/// A marker that indicates that the output of a mouse controller rotation
/// should be applied to a camera's transform.
#[derive(Reflect, Component, Default)]
#[derive(Debug, Clone, Reflect, Component, Default)]
#[reflect(Component)]
pub struct CameraController {
/// The camera entity to apply the rotation transform to.
Expand Down Expand Up @@ -128,7 +129,12 @@ pub fn toggle_cursor(
if input.just_pressed(KeyCode::F11) {
camera.locked = !camera.locked;

window.set_cursor_lock_mode(camera.locked);
let grab_mode = match camera.locked {
true => CursorGrabMode::Confined,
false => CursorGrabMode::None,
};

window.set_cursor_grab_mode(grab_mode);
window.set_cursor_visibility(!camera.locked);
}
}
Expand Down
6 changes: 3 additions & 3 deletions crates/awgen_client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ impl Plugin for ClientPlugin {
app.register_type::<WasdController>()
.register_type::<MouseController>()
.register_type::<CameraController>()
.add_system(wasd_velocity_input.in_ambiguity_set("player_controls"))
.add_system(mouse_rotation_input.in_ambiguity_set("player_controls"))
.add_system(toggle_cursor.in_ambiguity_set("player_controls"))
.add_system(wasd_velocity_input)
.add_system(mouse_rotation_input.ambiguous_with(wasd_velocity_input))
.add_system(toggle_cursor.ambiguous_with(mouse_rotation_input))
.add_system(apply_camera_transform.after(mouse_rotation_input));
}
}
5 changes: 2 additions & 3 deletions crates/awgen_network/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,5 @@ keywords = ["game", "game engine", "sandbox", "graphics"]
categories = ["games", "game-engines"]

[dependencies]
bevy = "0.8.1"
bevy_renet = "0.0.5"
iyes_loopless = "0.8.0"
bevy = "0.9.0"
bevy_renet = { git = "https://github.com/Alainx277/renet.git", branch = "bevy-0.9" }
9 changes: 6 additions & 3 deletions crates/awgen_network/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ impl Plugin for NetworkPlugin {
port,
max_clients,
} => {
app.add_plugin(RenetServerPlugin)
app.add_plugin(RenetServerPlugin::default())
.insert_resource(build_server(*port, *max_clients))
.register_type::<ClientSocket>()
.add_event::<ClientConnectedEvent>()
Expand All @@ -107,7 +107,10 @@ impl Plugin for NetworkPlugin {
NetworkSide::Client {
ip,
port,
} => app.add_plugin(RenetClientPlugin).insert_resource(build_client(ip, *port)),
} => {
app.add_plugin(RenetClientPlugin::default())
.insert_resource(build_client(ip, *port))
},
};
}
}
Expand Down Expand Up @@ -138,5 +141,5 @@ fn build_client(ip: &str, port: u16) -> RenetClient {
server_addr,
user_data: None,
};
RenetClient::new(time, socket, client_id, connection_config, auth).unwrap()
RenetClient::new(time, socket, connection_config, auth).unwrap()
}
2 changes: 1 addition & 1 deletion crates/awgen_network/src/server_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ pub fn server_socket_event(
for event in events.iter() {
match event {
ServerEvent::ClientConnected(id, _) => {
let entity = commands.spawn().insert(ClientSocket::new(*id)).id();
let entity = commands.spawn(ClientSocket::new(*id)).id();
ev_connected.send(ClientConnectedEvent(entity));
},
ServerEvent::ClientDisconnected(id) => {
Expand Down
3 changes: 1 addition & 2 deletions crates/awgen_physics/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,5 @@ keywords = ["game", "game engine", "sandbox", "graphics"]
categories = ["games", "game-engines"]

[dependencies]
bevy = "0.8.1"
iyes_loopless = "0.8.0"
bevy = "0.9.0"
num = "0.4.0"
37 changes: 19 additions & 18 deletions crates/awgen_physics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ pub mod prelude {
}

use bevy::prelude::*;
use iyes_loopless::prelude::*;
use bevy::time::FixedTimestep;
use prelude::*;
use std::time::Duration;


/// The implementation of the Awgen physics plugin. Handles collision, physics
Expand All @@ -42,33 +41,36 @@ impl PhysicsPlugin {

impl Plugin for PhysicsPlugin {
fn build(&self, app: &mut App) {
let frame_nanos = ((1.0f64 / self.tickrate as f64) * 1_000_000_000.0f64) as u64;
let timestep = 1.0 / self.tickrate as f64;

app.register_type::<Position>()
.register_type::<PreviousPosition>()
.register_type::<VelocitySource>()
.register_type::<Movable>()
.insert_resource(PhysicsTickrate::new(self.tickrate))
.insert_resource(PhysicsFrame::default())
.add_fixed_timestep_before_stage(
.add_stage_before(
CoreStage::Update,
Duration::from_nanos(frame_nanos),
"pre_tick",
SystemStage::parallel()
.with_run_criteria(FixedTimestep::step(timestep))
.with_system(push_position_stack)
.with_system(prepare_physics_render_frame),
)
.add_fixed_timestep_system("pre_tick", 0, push_position_stack)
.add_fixed_timestep_system("pre_tick", 0, prepare_physics_render_frame)
.add_fixed_timestep(Duration::from_nanos(frame_nanos), "tick")
.add_fixed_timestep_after_stage(
CoreStage::Update,
Duration::from_nanos(frame_nanos),
.add_stage_after(
"pre_tick",
"tick",
SystemStage::parallel().with_run_criteria(FixedTimestep::step(timestep)),
)
.add_stage_after(
"tick",
"post_tick",
SystemStage::parallel()
.with_run_criteria(FixedTimestep::step(timestep))
.with_system(apply_velocity),
)
.add_fixed_timestep_system("post_tick", 0, apply_velocity)
.add_system_to_stage(CoreStage::PostUpdate, update_physics_render_frame)
.add_system_to_stage(
CoreStage::PostUpdate,
update_render_position.after(update_physics_render_frame),
);
.add_system(update_physics_render_frame)
.add_system(update_render_position.after(update_physics_render_frame));
}
}

Expand All @@ -94,7 +96,6 @@ pub struct RigidBodyBundle {
#[derive(Bundle, Default)]
pub struct InterpolatedRigidBodyBundle {
/// The current position of the rigid body.
#[bundle]
rigid_body: RigidBodyBundle,

/// The position of the rigid body on the previous frame.
Expand Down
6 changes: 3 additions & 3 deletions crates/awgen_physics/src/position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use bevy::prelude::*;


/// The absolute position of an entity on a physics frame.
#[derive(Reflect, Component)]
#[reflect(Component)]
#[derive(Debug, Clone, Reflect, Component)]
// #[reflect(Component)]
pub struct Position {
/// The translation value of the entity within the world, measured in
/// meters.
Expand All @@ -33,7 +33,7 @@ impl Default for Position {


/// The value of an entity's position on the previous physics frame.
#[derive(Reflect, Component)]
#[derive(Debug, Clone, Reflect, Component)]
#[reflect(Component)]
pub struct PreviousPosition {
/// The translation value of the entity within the world, measured in
Expand Down
24 changes: 15 additions & 9 deletions crates/awgen_physics/src/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ use bevy::prelude::*;


/// The number of physics frames that are calculated per second.
#[derive(Debug, Clone)]
#[derive(Debug, Clone, Resource)]
pub struct PhysicsTickrate {
/// The number of frames per second.
rate: f32,

/// The delta time between physics frames, measured in seconds.
delta: f64,
delta: f32,
}

impl PhysicsTickrate {
Expand All @@ -21,7 +21,7 @@ impl PhysicsTickrate {
pub fn new(rate: f32) -> Self {
Self {
rate,
delta: 1.0 / rate as f64,
delta: 1.0 / rate,
}
}

Expand All @@ -33,18 +33,24 @@ impl PhysicsTickrate {


/// Gets the delta time, in seconds, between physics frames.
pub fn delta(&self) -> f64 {
pub fn delta(&self) -> f32 {
self.delta
}
}

impl Default for PhysicsTickrate {
fn default() -> Self {
PhysicsTickrate::new(20.0)
}
}


/// A time keeping unit that measures the physics frame time delta for physics
/// rendering interpolation.
#[derive(Debug, Clone, Default)]
#[derive(Debug, Clone, Default, Resource)]
pub struct PhysicsFrame {
/// The total system time, in seconds, of the last real physics frame.
last_frame: f64,
last_frame: f32,

/// The delta percentage between the last physics frame and the next physics
/// frame.
Expand All @@ -58,7 +64,7 @@ pub struct PhysicsFrame {
impl PhysicsFrame {
/// Gets the total time, in seconds, of the last physics frame since the
/// runtime was started.
pub fn last_frame(&self) -> f64 {
pub fn last_frame(&self) -> f32 {
self.last_frame
}

Expand Down Expand Up @@ -87,7 +93,7 @@ pub fn update_physics_render_frame(
tickrate: Res<PhysicsTickrate>,
mut physics: ResMut<PhysicsFrame>,
) {
let cur_frame = time.seconds_since_startup();
let cur_frame = time.elapsed_seconds();
let progress = (cur_frame - physics.last_frame) / tickrate.delta();
physics.delta = num::clamp(progress as f32, 0.0, 1.0);
}
Expand All @@ -96,6 +102,6 @@ pub fn update_physics_render_frame(
/// Called at the beginning of a physics frame to prepare the timing for
/// calculating physics render frames.
pub fn prepare_physics_render_frame(time: Res<Time>, mut frame: ResMut<PhysicsFrame>) {
frame.last_frame = time.seconds_since_startup();
frame.last_frame = time.elapsed_seconds();
frame.frame_num += 1;
}
3 changes: 1 addition & 2 deletions crates/awgen_server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,4 @@ keywords = ["game", "game engine", "sandbox", "graphics"]
categories = ["games", "game-engines"]

[dependencies]
bevy = "0.8.1"
iyes_loopless = "0.8.0"
bevy = "0.9.0"
39 changes: 34 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use awgen_client::ClientPlugin;
use awgen_network::NetworkPlugin;
use awgen_physics::PhysicsPlugin;
use awgen_server::ServerPlugin;
use bevy::log::{Level, LogPlugin};
use bevy::prelude::*;
use clap::{Parser, Subcommand};
use std::panic;
Expand Down Expand Up @@ -117,11 +118,18 @@ fn launch_client(ip: String, port: u16, debug: bool) {

App::new()
.insert_resource(ClearColor(CLEAR_COLOR))
.insert_resource(WindowDescriptor {
title: window_title,
..default()
})
.add_plugins(DefaultPlugins)
.add_plugins(
DefaultPlugins
.set(WindowPlugin {
window: WindowDescriptor {
title: window_title,
..default()
},
..default()
})
.set(log_plugin(debug))
.set(ImagePlugin::default_nearest()),
)
.add_plugin(PhysicsPlugin::new(TICKRATE))
.add_plugin(NetworkPlugin::new_client(ip, port))
.add_plugin(client)
Expand All @@ -140,8 +148,29 @@ fn launch_server(port: u16, debug: bool) {

App::new()
.add_plugins(MinimalPlugins)
.add_plugin(log_plugin(debug))
.add_plugin(PhysicsPlugin::new(TICKRATE))
.add_plugin(NetworkPlugin::new_server(port, MAX_CLIENTS))
.add_plugin(server)
.run();
}


/// Configures the logging plugin based on whether the application is launched
/// in debug mode or not.
fn log_plugin(debug: bool) -> LogPlugin {
match debug {
true => {
LogPlugin {
level: Level::DEBUG,
filter: "info,wgpu=error,awgen=debug".to_string(),
}
},
false => {
LogPlugin {
level: Level::INFO,
filter: "info,wgpu=error".to_string(),
}
},
}
}
6 changes: 3 additions & 3 deletions src/prefabs/basic_scene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ pub fn spawn_basic_scene(
mut materials: ResMut<Assets<StandardMaterial>>,
) {
// plane
commands.spawn_bundle(PbrBundle {
commands.spawn(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Plane {
size: 5.0,
})),
material: materials.add(Color::rgb(0.3, 0.5, 0.3).into()),
..default()
});
// cube
commands.spawn_bundle(PbrBundle {
commands.spawn(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Cube {
size: 1.0,
})),
Expand All @@ -28,7 +28,7 @@ pub fn spawn_basic_scene(
..default()
});
// light
commands.spawn_bundle(PointLightBundle {
commands.spawn(PointLightBundle {
point_light: PointLight {
intensity: 1500.0,
shadows_enabled: true,
Expand Down
Loading

0 comments on commit efd7b35

Please sign in to comment.