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

@ -1,36 +1,36 @@
use shipyard::{AllStoragesView, Unique, NonSendSync, UniqueView, UniqueViewMut}; use shipyard::{AllStoragesView, Unique, NonSendSync, UniqueView, UniqueViewMut};
use crate::rendering::Renderer; use crate::rendering::Renderer;
use winit::window::CursorGrabMode; use winit::window::CursorGrabMode;
#[derive(Unique)] #[derive(Unique)]
pub struct CursorLock(pub bool); pub struct CursorLock(pub bool);
pub fn update_cursor_lock_state( pub fn update_cursor_lock_state(
lock: UniqueView<CursorLock>, lock: UniqueView<CursorLock>,
display: NonSendSync<UniqueView<Renderer>> display: NonSendSync<UniqueView<Renderer>>
) { ) {
if cfg!(target_os = "android") { if cfg!(target_os = "android") {
return return
} }
if lock.is_inserted_or_modified() { if lock.is_inserted_or_modified() {
//TODO MIGRATION //TODO MIGRATION
let window = &display.window; let window = &display.window;
window.set_cursor_grab(match lock.0 { window.set_cursor_grab(match lock.0 {
true => CursorGrabMode::Confined, true => CursorGrabMode::Confined,
false => CursorGrabMode::None, false => CursorGrabMode::None,
}).expect("Failed to change cursor grab state"); }).expect("Failed to change cursor grab state");
window.set_cursor_visible(!lock.0); window.set_cursor_visible(!lock.0);
} }
} }
pub fn insert_lock_state( pub fn insert_lock_state(
storages: AllStoragesView storages: AllStoragesView
) { ) {
storages.add_unique(CursorLock(false)) storages.add_unique(CursorLock(false))
} }
pub fn lock_cursor_now( pub fn lock_cursor_now(
mut lock: UniqueViewMut<CursorLock> mut lock: UniqueViewMut<CursorLock>
) { ) {
lock.0 = true lock.0 = true
} }

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

@ -1,87 +1,89 @@
use glam::Mat4; use glam::Mat4;
use shipyard::{Component, AllStoragesViewMut, UniqueViewMut}; use shipyard::{Component, AllStoragesViewMut, UniqueViewMut};
use kubi_shared::{ use kubi_shared::{
entity::{Entity, Health}, entity::{Entity, Health},
player::{Player, PLAYER_HEALTH, PlayerHolding}, player::{Player, PLAYER_HEALTH, PlayerHolding},
block::Block, block::Block,
networking::{ networking::{
client::{Username, Client, ClientIdMap}, client::{Username, Client, ClientIdMap},
messages::ClientInitData messages::ClientInitData
} }
}; };
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)]
pub struct MainPlayer; #[derive(Component)]
pub struct MainPlayer;
pub fn spawn_player (
mut storages: AllStoragesViewMut, pub fn spawn_player (
) { mut storages: AllStoragesViewMut,
log::info!("spawning player"); ) {
storages.add_entity(( log::info!("spawning player");
Player, storages.add_entity(((
MainPlayer, Player,
Entity, MainPlayer,
Health::new(PLAYER_HEALTH), Entity,
Transform::default(), Health::new(PLAYER_HEALTH),
Camera::default(), Transform::default(),
FlyController, Camera::default(),
LookingAtBlock::default(), FlyController,
PlayerHolding(Some(Block::Cobblestone)), LookingAtBlock::default(),
Username("LocalPlayer".into()) PlayerHolding(Some(Block::Cobblestone)),
)); Username("LocalPlayer".into()),
} ),(
ClPhysicsActor::default(),
pub fn spawn_local_player_multiplayer ( )));
storages: &mut AllStoragesViewMut, }
init: ClientInitData
) { pub fn spawn_local_player_multiplayer (
log::info!("spawning local multiplayer player"); storages: &mut AllStoragesViewMut,
let entity_id = storages.add_entity(( init: ClientInitData
( ) {
Player, log::info!("spawning local multiplayer player");
Client(init.client_id), let entity_id = storages.add_entity(((
MainPlayer, Player,
Entity, Client(init.client_id),
init.health, MainPlayer,
Transform(Mat4::from_rotation_translation(init.direction, init.position)), Entity,
Camera::default(), init.health,
FlyController, Transform(Mat4::from_rotation_translation(init.direction, init.position)),
LookingAtBlock::default(), Camera::default(),
PlayerHolding::default(), FlyController,
),( LookingAtBlock::default(),
Username(init.username) PlayerHolding::default(),
) ),(
)); Username(init.username),
ClPhysicsActor::default(),
//Add ourself to the client id map )));
let mut client_id_map = storages.borrow::<UniqueViewMut<ClientIdMap>>().unwrap();
client_id_map.0.insert(init.client_id, entity_id); //Add ourself to the client id map
} let mut client_id_map = storages.borrow::<UniqueViewMut<ClientIdMap>>().unwrap();
client_id_map.0.insert(init.client_id, entity_id);
pub fn spawn_remote_player_multiplayer( }
storages: &mut AllStoragesViewMut,
init: ClientInitData pub fn spawn_remote_player_multiplayer(
) { storages: &mut AllStoragesViewMut,
log::info!("spawning remote multiplayer player"); init: ClientInitData
) {
//Spawn player locally log::info!("spawning remote multiplayer player");
let entity_id = storages.add_entity((
Username(init.username), //Spawn player locally
Client(init.client_id), let entity_id = storages.add_entity((
Player, Username(init.username),
Entity, Client(init.client_id),
init.health, Player,
Transform(Mat4::from_rotation_translation(init.direction, init.position)), Entity,
PlayerHolding::default(), init.health,
)); Transform(Mat4::from_rotation_translation(init.direction, init.position)),
PlayerHolding::default(),
//Add it to the client id map ));
let mut client_id_map = storages.borrow::<UniqueViewMut<ClientIdMap>>().unwrap();
client_id_map.0.insert(init.client_id, entity_id); //Add it to the client id map
} let mut client_id_map = storages.borrow::<UniqueViewMut<ClientIdMap>>().unwrap();
client_id_map.0.insert(init.client_id, entity_id);
}