wip cl physics

This commit is contained in:
griffi-gh 2024-02-13 00:50:30 +01:00
parent d49c858cd8
commit 5f2f27396b
4 changed files with 181 additions and 123 deletions

View file

@ -0,0 +1,52 @@
//TODO client-side physics
//TODO move this to shared
use glam::{Mat4, Vec3};
use kubi_shared::transform::Transform;
use shipyard::{track, AllStoragesView, Component, IntoIter, Unique, UniqueView, View, ViewMut};
use crate::delta_time::DeltaTime;
#[derive(Unique)]
pub struct GlobalClPhysicsConfig {
pub gravity: Vec3,
}
#[derive(Component)]
pub struct ClPhysicsActor {
pub forces: Vec3,
pub velocity: Vec3,
pub terminal_velocity: f32,
//TODO: this should be configurable per block
pub friction_agains_ground: f32,
}
impl Default for ClPhysicsActor {
fn default() -> Self {
Self {
forces: Vec3::ZERO,
velocity: Vec3::ZERO,
terminal_velocity: 40.,
friction_agains_ground: 0.5,
}
}
}
pub fn init_client_physics(
storages: AllStoragesView,
) {
storages.add_unique(GlobalClPhysicsConfig {
gravity: Vec3::new(0., -9.8, 0.),
});
}
pub fn update_client_physics_late(
controllers: View<ClPhysicsActor>,
mut transforms: ViewMut<Transform, track::All>,
dt: UniqueView<DeltaTime>,
phy_conf: UniqueView<GlobalClPhysicsConfig>,
) {
// for (_, mut transform) in (&controllers, &mut transforms).iter() {
// let (scale, rotation, mut translation) = transform.0.to_scale_rotation_translation();
// translation.y -= dt.0.as_secs_f32() * 100.;
// transform.0 = Mat4::from_scale_rotation_translation(scale, rotation, translation);
// }
}

View file

@ -37,6 +37,7 @@ pub(crate) mod loading_screen;
pub(crate) mod connecting_screen; pub(crate) mod connecting_screen;
pub(crate) mod fixed_timestamp; pub(crate) mod fixed_timestamp;
pub(crate) mod filesystem; pub(crate) mod filesystem;
pub(crate) mod client_physics;
use world::{ use world::{
init_game_world, init_game_world,
@ -81,6 +82,7 @@ use loading_screen::update_loading_screen;
use connecting_screen::switch_to_loading_if_connected; use connecting_screen::switch_to_loading_if_connected;
use fixed_timestamp::init_fixed_timestamp_storage; use fixed_timestamp::init_fixed_timestamp_storage;
use filesystem::AssetManager; use filesystem::AssetManager;
use client_physics::{init_client_physics, update_client_physics_late};
/// stuff required to init the renderer and other basic systems /// stuff required to init the renderer and other basic systems
fn pre_startup() -> Workload { fn pre_startup() -> Workload {
@ -104,6 +106,7 @@ fn startup() -> Workload {
init_input, init_input,
insert_control_flow_unique, insert_control_flow_unique,
init_delta_time, init_delta_time,
init_client_physics,
).into_sequential_workload() ).into_sequential_workload()
} }
@ -131,6 +134,7 @@ fn update() -> Workload {
).into_sequential_workload().run_if(is_ingame_or_loading), ).into_sequential_workload().run_if(is_ingame_or_loading),
( (
update_controllers, update_controllers,
update_client_physics_late,
generate_move_events, generate_move_events,
update_raycasts, update_raycasts,
update_block_placement, update_block_placement,

View file

@ -10,10 +10,11 @@ use kubi_shared::{
} }
}; };
use crate::{ use crate::{
transform::Transform,
camera::Camera, camera::Camera,
client_physics::ClPhysicsActor,
fly_controller::FlyController, fly_controller::FlyController,
world::raycast::LookingAtBlock, transform::Transform,
world::raycast::LookingAtBlock
}; };
#[derive(Component)] #[derive(Component)]
@ -23,7 +24,7 @@ pub fn spawn_player (
mut storages: AllStoragesViewMut, mut storages: AllStoragesViewMut,
) { ) {
log::info!("spawning player"); log::info!("spawning player");
storages.add_entity(( storages.add_entity(((
Player, Player,
MainPlayer, MainPlayer,
Entity, Entity,
@ -33,8 +34,10 @@ pub fn spawn_player (
FlyController, FlyController,
LookingAtBlock::default(), LookingAtBlock::default(),
PlayerHolding(Some(Block::Cobblestone)), PlayerHolding(Some(Block::Cobblestone)),
Username("LocalPlayer".into()) Username("LocalPlayer".into()),
)); ),(
ClPhysicsActor::default(),
)));
} }
pub fn spawn_local_player_multiplayer ( pub fn spawn_local_player_multiplayer (
@ -42,8 +45,7 @@ pub fn spawn_local_player_multiplayer (
init: ClientInitData init: ClientInitData
) { ) {
log::info!("spawning local multiplayer player"); log::info!("spawning local multiplayer player");
let entity_id = storages.add_entity(( let entity_id = storages.add_entity(((
(
Player, Player,
Client(init.client_id), Client(init.client_id),
MainPlayer, MainPlayer,
@ -55,9 +57,9 @@ pub fn spawn_local_player_multiplayer (
LookingAtBlock::default(), LookingAtBlock::default(),
PlayerHolding::default(), PlayerHolding::default(),
),( ),(
Username(init.username) Username(init.username),
) ClPhysicsActor::default(),
)); )));
//Add ourself to the client id map //Add ourself to the client id map
let mut client_id_map = storages.borrow::<UniqueViewMut<ClientIdMap>>().unwrap(); let mut client_id_map = storages.borrow::<UniqueViewMut<ClientIdMap>>().unwrap();