From 0fee74976c9b0e01633c8cb09ede341631eaee51 Mon Sep 17 00:00:00 2001 From: MikolajG Date: Thu, 11 Jul 2024 14:08:59 +0200 Subject: [PATCH] stuff I did while I was away --- .cargo/config | 3 - .vscode/settings.json | 2 +- .vscode/tasks.json | 9 + Cargo.lock | 12 +- Cargo.toml | 3 +- src/config_utils.rs | 36 ++ src/lib.rs | 6 +- src/main.rs | 2 +- src/player_utils/character_controller.rs | 434 +++++++++++------------ src/player_utils/player_look.rs | 4 +- src/weapon_framework.rs | 1 + src/weapon_framework/gun_framework.rs | 25 ++ 12 files changed, 298 insertions(+), 239 deletions(-) delete mode 100644 .cargo/config create mode 100644 src/config_utils.rs create mode 100644 src/weapon_framework/gun_framework.rs diff --git a/.cargo/config b/.cargo/config deleted file mode 100644 index ec26773..0000000 --- a/.cargo/config +++ /dev/null @@ -1,3 +0,0 @@ -[target.x86_64-unknown-linux-gnu] -linker = "clang" -rustflags = ["-Clink-arg=-fuse-ld=mold"] \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json index ab4fd48..89a5e75 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,9 +1,9 @@ { - "rust-analyzer.debug.engine": "vadimcn.vscode-lldb", "cSpell.words": [ "Gamepad", "qeury", "Substep", "xpbd" ], + "rust-analyzer.checkOnSave": false } \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 7856b66..268c937 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -48,6 +48,15 @@ "--run", "steam-run cargo fmt --all" ] + }, + { + "label": "generate docs", + "type": "shell", + "command": "nix-shell", + "args": [ + "--run", + "cargo doc" + ] } ] } \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index 4be259a..1bfcff3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1209,7 +1209,8 @@ dependencies = [ [[package]] name = "bevy_xpbd_3d" version = "0.4.2" -source = "git+https://git.opencodebox.com/MikolajG/bevy_xpbd?branch=reflect-serialize#489e53505abda9649a9ee6e9de6e03a7d5801e28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0425ea7361b9b27c2a382e0663deb42f41147eee60fb2b3d5fa7e42d363ea848" dependencies = [ "bevy", "bevy_math", @@ -1226,9 +1227,9 @@ dependencies = [ [[package]] name = "bevy_xpbd_derive" version = "0.1.0" -source = "git+https://git.opencodebox.com/MikolajG/bevy_xpbd?branch=reflect-serialize#489e53505abda9649a9ee6e9de6e03a7d5801e28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3e1ef1d5e328abe1b76df974245f78e17fd17867583883d5e77444c6a8223a64" dependencies = [ - "proc-macro2", "quote", "syn 2.0.55", ] @@ -3706,9 +3707,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.114" +version = "1.0.117" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c5f09b1bd632ef549eaa9f60a1f8de742bdbc698e6cee2095fc84dde5f549ae0" +checksum = "455182ea6142b14f93f4bc5320a2b31c1f266b66a4a5c858b013302a5d8cbfc3" dependencies = [ "itoa", "ryu", @@ -4185,6 +4186,7 @@ dependencies = [ "embed-resource", "image", "rand", + "serde_json", "webbrowser", "winit", ] diff --git a/Cargo.toml b/Cargo.toml index d6f4a87..0b4a6a1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -68,11 +68,12 @@ bevy_asset_loader = { version = "0.20" } rand = { version = "0.8.3" } webbrowser = { version = "0.8", features = ["hardened"] } bevy_editor_pls = "0.8.1" -bevy_xpbd_3d = {git = "https://git.opencodebox.com/MikolajG/bevy_xpbd", branch = "reflect-serialize"} +bevy_xpbd_3d = "0.4.2" # keep the following in sync with Bevy's dependencies winit = { version = "0.29", default-features = false } image = { version = "0.24", default-features = false } +serde_json = "1.0.117" [build-dependencies] diff --git a/src/config_utils.rs b/src/config_utils.rs new file mode 100644 index 0000000..651da60 --- /dev/null +++ b/src/config_utils.rs @@ -0,0 +1,36 @@ +/* +Trouble in Terror Town: Community Edition +Copyright (C) 2024 Mikolaj Wojciech Gorski + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License 3.0 as published by +the Free Software Foundation. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +See the GNU General Public License 3.0 for more details. + +You should have received a copy of the GNU General Public License 3.0 +along with this program. If not, see . +*/ + +use bevy::prelude::*; +use serde::Deserialize; +use std::fs::*; +use std::time::{Duration, SystemTime}; + +#[derive(Deserialize, Clone, Debug)] +struct CharacterControllerConfig { + movement_acceleration: f32, + air_control_factor: f32, + movement_dampening_factor: f32, + jump_impulse: f32, + gravity: [f32; 3], + max_slope_angle: f32, + axis_speed_limit: f32, + max_acceleration: f32, + friction: f32, +} + + diff --git a/src/lib.rs b/src/lib.rs index c3c85e7..8b902e9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -79,7 +79,7 @@ fn setup( commands, meshes, materials, - 0.5, + 0.7, CharacterControllerBundle::new(Collider::capsule(1.0, 0.5), CharacterController::default()), ); } @@ -105,7 +105,9 @@ fn spawn_map( RigidBody::Static, )); + + is_map_spawned.0 = true; + println!("map spawned!"); - is_map_spawned.0 = true } diff --git a/src/main.rs b/src/main.rs index 265ac68..2d6b10d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -34,7 +34,7 @@ fn main() { .insert_resource(ClearColor(Color::rgb(0.4, 0.4, 0.4))) .add_plugins(DefaultPlugins.set(WindowPlugin { primary_window: Some(Window { - title: "Trouble in Terror Town: Source Crossed".to_string(), // ToDo + title: "Trouble in Terror Town: Community Edition".to_string(), // ToDo // Bind to canvas included in `index.html` canvas: Some("#bevy".to_owned()), // Tells wasm not to override default event handling, like F5 and Ctrl+R diff --git a/src/player_utils/character_controller.rs b/src/player_utils/character_controller.rs index 252e5e2..02dbd0a 100644 --- a/src/player_utils/character_controller.rs +++ b/src/player_utils/character_controller.rs @@ -37,8 +37,8 @@ pub struct CharacterControllerPlugin; impl Plugin for CharacterControllerPlugin { fn build(&self, app: &mut App) { app.add_systems(Update, move_character); - app.add_systems(FixedUpdate, (apply_gravity, update_grounded, dampen_movement,)); - app.add_systems(SubstepSchedule, kinematic_controller_collisions.in_set(SubstepSet::SolveUserConstraints)); + app.add_systems(FixedUpdate, (apply_gravity, update_grounded, dampen_movement)); + app.add_systems(SubstepSchedule, collide_and_slide.in_set(SubstepSet::SolveUserConstraints)); } } @@ -52,10 +52,12 @@ pub struct CharacterController { pub air_control_factor: Scalar, pub movement_dampening_factor: Scalar, pub jump_impulse: Scalar, + /// direction in which gravity pulls the character look at [`new_fall_velocity`] pub gravity: Vector, pub max_slope_angle: Scalar, pub axis_speed_limit: f32, pub max_acceleration: f32, + /// value used for holding current calculated friction, do not change on spawn pub friction: f32, } @@ -65,7 +67,7 @@ impl Default for CharacterController { movement_acceleration: 20.0, air_control_factor: 0.5, movement_dampening_factor: 0.95, - jump_impulse: 4.0, + jump_impulse: 6.0, gravity: Vector::NEG_Y * 9.81 * 2.0, max_slope_angle: (45.0 as Scalar).to_radians(), axis_speed_limit: 6668.5, @@ -141,212 +143,38 @@ fn get_lateral_acceleration( } -// function "borrowed" from UE +/// project a point on a plane fn point_plane_project(point: Vec3, plane_base: Vec3, plane_normal: Vec3) -> Vec3 { - let projection = point - plane_normal * plane_normal.dot(point - plane_base); - projection + point - plane_normal * plane_normal.dot(point - plane_base) } -// function "borrowed" from UE +/// calculating gravity, later you'll have to pass custom terminal velocity fn new_fall_velocity(initial_velocity: LinearVelocity, gravity: Vec3, delta_time: f32, speed_limit: f32) -> Vec3 { let mut result: Vec3 = Vec3 { x: initial_velocity.x, y: initial_velocity.y, z: initial_velocity.z }; let terminal_velocity: f32 = 50.0; - if delta_time > 0.0 - { + // Apply gravity. + result += gravity * delta_time; - // Apply gravity. - result += gravity * delta_time; + // Don't exceed terminal velocity. + let terminal_limit: f32 = f32::abs(terminal_velocity); //FMath::Abs(GetPhysicsVolume()->TerminalVelocity); + if result.length_squared() > (terminal_limit * terminal_limit) + { + let gravity_dir: Vec3 = gravity.normalize(); - - // Don't exceed terminal velocity. - let terminal_limit: f32 = f32::abs(terminal_velocity); //FMath::Abs(GetPhysicsVolume()->TerminalVelocity); - if result.length_squared() > (terminal_limit * terminal_limit) + if result.y > terminal_limit || gravity_dir.y > terminal_limit { - let gravity_dir: Vec3 = gravity.normalize(); - - if result.y > terminal_limit || gravity_dir.y > terminal_limit - { - result = point_plane_project(result, Vec3::ZERO, gravity_dir) + gravity_dir * terminal_limit; - } - } - } + result = point_plane_project(result, Vec3::ZERO, gravity_dir) + gravity_dir * terminal_limit; + } + } result.z = f32::clamp(result.z, -speed_limit, speed_limit); return result; } -fn apply_gravity( - time: Res