diff --git a/lib/common/src/physics.rs b/lib/common/src/physics.rs index 74a8845..95dbeac 100644 --- a/lib/common/src/physics.rs +++ b/lib/common/src/physics.rs @@ -4,6 +4,12 @@ use crate::mouse::MouseState; pub enum TimeInner{} pub type Time=crate::integer::Time; +#[derive(Clone,Debug)] +pub enum UnbufferedInstruction{ + MoveMouse(glam::IVec2), + Other(OtherInstruction), +} + #[derive(Clone,Debug)] pub enum Instruction{ Mouse(MouseInstruction), diff --git a/lib/common/src/session.rs b/lib/common/src/session.rs index 97322b1..7f2c01d 100644 --- a/lib/common/src/session.rs +++ b/lib/common/src/session.rs @@ -1,9 +1,3 @@ #[derive(Clone,Copy,Hash,Eq,PartialEq,PartialOrd,Debug)] pub enum TimeInner{} pub type Time=crate::integer::Time; - -#[derive(Clone,Debug)] -pub enum UnbufferedInputInstruction{ - MoveMouse(glam::IVec2), - Other(crate::physics::OtherInstruction), -} diff --git a/strafe-client/src/mouse_interpolator.rs b/strafe-client/src/mouse_interpolator.rs index 0215fa3..ee55452 100644 --- a/strafe-client/src/mouse_interpolator.rs +++ b/strafe-client/src/mouse_interpolator.rs @@ -1,16 +1,17 @@ use strafesnet_common::mouse::MouseState; use strafesnet_common::physics::{ + UnbufferedInstruction as PhysicsUnbufferedInstruction, Instruction as PhysicsInputInstruction, Time as PhysicsTime, TimeInner as PhysicsTimeInner, MouseInstruction, OtherInstruction, }; -use strafesnet_common::session::{Time as SessionTime,TimeInner as SessionTimeInner,UnbufferedInputInstruction}; +use strafesnet_common::session::{Time as SessionTime,TimeInner as SessionTimeInner}; use strafesnet_common::instruction::{InstructionConsumer,InstructionEmitter,TimedInstruction}; type TimedPhysicsInstruction=TimedInstruction; -type PhysicsTimedUnbufferedInstruction=TimedInstruction; +type PhysicsTimedUnbufferedInstruction=TimedInstruction; type DoubleTimedUnbufferedInstruction=TimedInstruction; const MOUSE_TIMEOUT:SessionTime=SessionTime::from_millis(10); @@ -30,7 +31,7 @@ impl BufferState{ fn next_state(&self,ins:DoubleTimedUnbufferedInstruction,physics_timeline:&mut std::collections::VecDeque)->(Self,Option>){ match self{ BufferState::Unbuffered=>{ - if let UnbufferedInputInstruction::MoveMouse(pos)=ins.instruction.instruction{ + if let PhysicsUnbufferedInstruction::MoveMouse(pos)=ins.instruction.instruction{ return (BufferState::Initializing(ins.time,MouseState{pos,time:ins.instruction.time}),None); } }, @@ -49,7 +50,7 @@ impl BufferState{ }); return (BufferState::Unbuffered,None); } - if let UnbufferedInputInstruction::MoveMouse(pos)=ins.instruction.instruction{ + if let PhysicsUnbufferedInstruction::MoveMouse(pos)=ins.instruction.instruction{ let next_mouse_state=MouseState{pos,time:ins.instruction.time}; physics_timeline.push_front(TimedInstruction{ time:mouse_state.time, @@ -79,7 +80,7 @@ impl BufferState{ }); return (BufferState::Unbuffered,None); } - if let UnbufferedInputInstruction::MoveMouse(pos)=ins.instruction.instruction{ + if let PhysicsUnbufferedInstruction::MoveMouse(pos)=ins.instruction.instruction{ let next_mouse_state=MouseState{pos,time:ins.instruction.time}; physics_timeline.push_front(TimedInstruction{ time:ins.instruction.time, @@ -92,8 +93,8 @@ impl BufferState{ }, } let instruction_out=match ins.instruction.instruction{ - UnbufferedInputInstruction::MoveMouse(_)=>None, - UnbufferedInputInstruction::Other(other_instruction)=>Some(TimedInstruction{ + PhysicsUnbufferedInstruction::MoveMouse(_)=>None, + PhysicsUnbufferedInstruction::Other(other_instruction)=>Some(TimedInstruction{ time:ins.instruction.time, instruction:other_instruction, }), @@ -208,9 +209,9 @@ impl MouseInterpolator{ // This should be simulation_timer.time(timeout) // but the timer is not accessible from this scope // and it's just here to say that the mouse isn't moving anyways. - time:ins.instruction.time, + time:mouse_state.time, instruction:PhysicsInputInstruction::Mouse( - MouseInstruction::SetNextMouse(MouseState{pos:mouse_state.pos,time:ins.instruction.time}) + MouseInstruction::SetNextMouse(MouseState{pos:mouse_state.pos,time:mouse_state.time}) ), }) }, diff --git a/strafe-client/src/session.rs b/strafe-client/src/session.rs index 9743415..c530637 100644 --- a/strafe-client/src/session.rs +++ b/strafe-client/src/session.rs @@ -1,14 +1,14 @@ -use strafesnet_common::instruction::{InstructionConsumer, InstructionEmitter, InstructionFeedback, TimedInstruction}; +use strafesnet_common::instruction::{InstructionConsumer,InstructionEmitter,InstructionFeedback,TimedInstruction}; // session represents the non-hardware state of the client. // Ideally it is a deterministic state which is atomically updated by instructions, same as the simulation state. -use strafesnet_common::physics::{Instruction as PhysicsInputInstruction,TimeInner as PhysicsTimeInner,Time as PhysicsTime}; +use strafesnet_common::physics::{UnbufferedInstruction as PhysicsUnbufferedInstruction,Instruction as PhysicsInputInstruction,TimeInner as PhysicsTimeInner,Time as PhysicsTime}; use strafesnet_common::timer::{Scaled,Timer}; -use strafesnet_common::session::{TimeInner as SessionTimeInner,Time as SessionTime,UnbufferedInputInstruction}; +use strafesnet_common::session::{TimeInner as SessionTimeInner,Time as SessionTime}; use crate::mouse_interpolator::{MouseInterpolator,StepInstruction}; pub enum ExternalInstruction{ - ToBuffer(UnbufferedInputInstruction), + Input(PhysicsUnbufferedInstruction), SetPaused(bool), Render, Resize(winit::dpi::PhysicalSize), @@ -89,7 +89,7 @@ impl InstructionConsumer for Session{ fn process_instruction(&mut self,ins:TimedInstruction){ match ins.instruction{ // send it down to MouseInterpolator with two timestamps, SessionTime and PhysicsTime - ExternalInstruction::ToBuffer(instruction)=>self.mouse_interpolator.process_instruction(TimedInstruction{ + ExternalInstruction::Input(instruction)=>self.mouse_interpolator.process_instruction(TimedInstruction{ time:ins.time, instruction:TimedInstruction{ time:self.simulation.timer.time(ins.time), diff --git a/strafe-client/src/setup.rs b/strafe-client/src/setup.rs index af81dd7..3b244c2 100644 --- a/strafe-client/src/setup.rs +++ b/strafe-client/src/setup.rs @@ -1,4 +1,4 @@ -use crate::window::WindowInstruction; +use crate::window::Instruction; use strafesnet_common::instruction::TimedInstruction; use strafesnet_common::integer; use strafesnet_common::session::TimeInner as SessionTimeInner; @@ -224,7 +224,7 @@ pub fn setup_and_start(title:&str){ let path=std::path::PathBuf::from(arg); window_thread.send(TimedInstruction{ time:integer::Time::ZERO, - instruction:WindowInstruction::WindowEvent(winit::event::WindowEvent::DroppedFile(path)), + instruction:Instruction::WindowEvent(winit::event::WindowEvent::DroppedFile(path)), }).unwrap(); }; @@ -235,7 +235,7 @@ pub fn setup_and_start(title:&str){ fn run_event_loop( event_loop:winit::event_loop::EventLoop<()>, - mut window_thread:crate::compat_worker::QNWorker>, + mut window_thread:crate::compat_worker::QNWorker>, root_time:std::time::Instant )->Result<(),winit::error::EventLoopError>{ event_loop.run(move |event,elwt|{ @@ -247,7 +247,7 @@ fn run_event_loop( // }; match event{ winit::event::Event::AboutToWait=>{ - window_thread.send(TimedInstruction{time,instruction:WindowInstruction::RequestRedraw}).unwrap(); + window_thread.send(TimedInstruction{time,instruction:Instruction::RequestRedraw}).unwrap(); } winit::event::Event::WindowEvent { event: @@ -259,7 +259,7 @@ fn run_event_loop( winit::event::WindowEvent::Resized(size),//ignoring scale factor changed for now because mutex bruh window_id:_, } => { - window_thread.send(TimedInstruction{time,instruction:WindowInstruction::Resize(size)}).unwrap(); + window_thread.send(TimedInstruction{time,instruction:Instruction::Resize(size)}).unwrap(); } winit::event::Event::WindowEvent{event,..}=>match event{ winit::event::WindowEvent::KeyboardInput{ @@ -275,17 +275,17 @@ fn run_event_loop( elwt.exit(); } winit::event::WindowEvent::RedrawRequested=>{ - window_thread.send(TimedInstruction{time,instruction:WindowInstruction::Render}).unwrap(); + window_thread.send(TimedInstruction{time,instruction:Instruction::Render}).unwrap(); } _=>{ - window_thread.send(TimedInstruction{time,instruction:WindowInstruction::WindowEvent(event)}).unwrap(); + window_thread.send(TimedInstruction{time,instruction:Instruction::WindowEvent(event)}).unwrap(); } }, winit::event::Event::DeviceEvent{ event, .. } => { - window_thread.send(TimedInstruction{time,instruction:WindowInstruction::DeviceEvent(event)}).unwrap(); + window_thread.send(TimedInstruction{time,instruction:Instruction::DeviceEvent(event)}).unwrap(); }, _=>{} } diff --git a/strafe-client/src/window.rs b/strafe-client/src/window.rs index b1cd42e..df26eef 100644 --- a/strafe-client/src/window.rs +++ b/strafe-client/src/window.rs @@ -1,8 +1,9 @@ -use crate::physics_worker::InputInstruction; use strafesnet_common::instruction::TimedInstruction; use strafesnet_common::session::{Time as SessionTime,TimeInner as SessionTimeInner}; +use strafesnet_common::physics::{OtherInstruction,UnbufferedInstruction}; +use crate::session::ExternalInstruction as SessionExternalInstruction; -pub enum WindowInstruction{ +pub enum Instruction{ Resize(winit::dpi::PhysicalSize), WindowEvent(winit::event::WindowEvent), DeviceEvent(winit::event::DeviceEvent), @@ -16,7 +17,7 @@ struct WindowContext<'a>{ mouse:strafesnet_common::mouse::MouseState,//std::sync::Arc> screen_size:glam::UVec2, window:&'a winit::window::Window, - physics_thread:crate::compat_worker::QNWorker<'a,TimedInstruction>, + physics_thread:crate::compat_worker::QNWorker<'a,TimedInstruction>, } impl WindowContext<'_>{ @@ -27,7 +28,7 @@ impl WindowContext<'_>{ match event{ winit::event::WindowEvent::DroppedFile(path)=>{ match crate::file::load(path.as_path()){ - Ok(map)=>self.physics_thread.send(TimedInstruction{time,instruction:crate::physics_worker::Instruction::ChangeMap(map)}).unwrap(), + Ok(map)=>self.physics_thread.send(TimedInstruction{time,instruction:SessionExternalInstruction::ChangeMap(map)}).unwrap(), Err(e)=>println!("Failed to load map: {e}"), } }, @@ -35,7 +36,7 @@ impl WindowContext<'_>{ //pause unpause self.physics_thread.send(TimedInstruction{ time, - instruction:crate::physics_worker::Instruction::SetPaused(!state), + instruction:SessionExternalInstruction::SetPaused(!state), }).unwrap(); //recalculate pressed keys on focus }, @@ -91,28 +92,28 @@ impl WindowContext<'_>{ (keycode,state)=>{ let s=state.is_pressed(); if let Some(input_instruction)=match keycode{ - winit::keyboard::Key::Named(winit::keyboard::NamedKey::Space)=>Some(InputInstruction::Jump(s)), + winit::keyboard::Key::Named(winit::keyboard::NamedKey::Space)=>Some(OtherInstruction::SetJump(s)), winit::keyboard::Key::Character(key)=>match key.as_str(){ - "W"|"w"=>Some(InputInstruction::MoveForward(s)), - "A"|"a"=>Some(InputInstruction::MoveLeft(s)), - "S"|"s"=>Some(InputInstruction::MoveBack(s)), - "D"|"d"=>Some(InputInstruction::MoveRight(s)), - "E"|"e"=>Some(InputInstruction::MoveUp(s)), - "Q"|"q"=>Some(InputInstruction::MoveDown(s)), - "Z"|"z"=>Some(InputInstruction::Zoom(s)), + "W"|"w"=>Some(OtherInstruction::SetMoveForward(s)), + "A"|"a"=>Some(OtherInstruction::SetMoveLeft(s)), + "S"|"s"=>Some(OtherInstruction::SetMoveBack(s)), + "D"|"d"=>Some(OtherInstruction::SetMoveRight(s)), + "E"|"e"=>Some(OtherInstruction::SetMoveUp(s)), + "Q"|"q"=>Some(OtherInstruction::SetMoveDown(s)), + "Z"|"z"=>Some(OtherInstruction::SetZoom(s)), "R"|"r"=>if s{ //mouse needs to be reset since the position is absolute self.mouse=strafesnet_common::mouse::MouseState::default(); - Some(InputInstruction::ResetAndRestart) + Some(OtherInstruction::ResetAndRestart) }else{None}, - "F"|"f"=>if s{Some(InputInstruction::PracticeFly)}else{None}, + "F"|"f"=>if s{Some(OtherInstruction::PracticeFly)}else{None}, _=>None, }, _=>None, }{ self.physics_thread.send(TimedInstruction{ time, - instruction:crate::physics_worker::Instruction::Input(input_instruction), + instruction:SessionExternalInstruction::Input(UnbufferedInstruction::Other(input_instruction)), }).unwrap(); } }, @@ -140,7 +141,7 @@ impl WindowContext<'_>{ self.mouse.pos+=delta; self.physics_thread.send(TimedInstruction{ time, - instruction:crate::physics_worker::Instruction::Input(InputInstruction::MoveMouse(self.mouse.pos)), + instruction:SessionExternalInstruction::Input(InputInstruction::MoveMouse(self.mouse.pos)), }).unwrap(); }, winit::event::DeviceEvent::MouseWheel { @@ -150,7 +151,7 @@ impl WindowContext<'_>{ if false{//self.physics.style.use_scroll{ self.physics_thread.send(TimedInstruction{ time, - instruction:crate::physics_worker::Instruction::Input(InputInstruction::Jump(true)),//activates the immediate jump path, but the style modifier prevents controls&CONTROL_JUMP bit from being set to auto jump + instruction:SessionExternalInstruction::Input(InputInstruction::Jump(true)),//activates the immediate jump path, but the style modifier prevents controls&CONTROL_JUMP bit from being set to auto jump }).unwrap(); } }, @@ -161,7 +162,7 @@ impl WindowContext<'_>{ pub fn worker<'a>( window:&'a winit::window::Window, setup_context:crate::setup::SetupContext<'a>, -)->crate::compat_worker::QNWorker<'a,TimedInstruction>{ +)->crate::compat_worker::QNWorker<'a,TimedInstruction>{ // WindowContextSetup::new let user_settings=crate::settings::read_user_settings(); @@ -184,30 +185,30 @@ pub fn worker<'a>( }; //WindowContextSetup::into_worker - crate::compat_worker::QNWorker::new(move |ins:TimedInstruction|{ + crate::compat_worker::QNWorker::new(move |ins:TimedInstruction|{ match ins.instruction{ - WindowInstruction::RequestRedraw=>{ + Instruction::RequestRedraw=>{ window_context.window.request_redraw(); } - WindowInstruction::WindowEvent(window_event)=>{ + Instruction::WindowEvent(window_event)=>{ window_context.window_event(ins.time,window_event); }, - WindowInstruction::DeviceEvent(device_event)=>{ + Instruction::DeviceEvent(device_event)=>{ window_context.device_event(ins.time,device_event); }, - WindowInstruction::Resize(size)=>{ + Instruction::Resize(size)=>{ window_context.physics_thread.send( TimedInstruction{ time:ins.time, - instruction:crate::physics_worker::Instruction::Resize(size) + instruction:SessionExternalInstruction::Resize(size) } ).unwrap(); } - WindowInstruction::Render=>{ + Instruction::Render=>{ window_context.physics_thread.send( TimedInstruction{ time:ins.time, - instruction:crate::physics_worker::Instruction::Render + instruction:SessionExternalInstruction::Render } ).unwrap(); } diff --git a/strafe-client/src/worker.rs b/strafe-client/src/worker.rs index ada128a..4373d56 100644 --- a/strafe-client/src/worker.rs +++ b/strafe-client/src/worker.rs @@ -190,7 +190,7 @@ mod test{ for _ in 0..5 { let task = instruction::TimedInstruction{ time:strafesnet_common::physics::Time::ZERO, - instruction:strafesnet_common::physics::Instruction::Idle, + instruction:strafesnet_common::physics::Instruction::Other(strafesnet_common::physics::OtherInstruction::Idle), }; worker.send(task).unwrap(); } @@ -204,7 +204,7 @@ mod test{ // Send a new task let task = instruction::TimedInstruction{ time:integer::Time::ZERO, - instruction:strafesnet_common::physics::Instruction::Idle, + instruction:strafesnet_common::physics::Instruction::Other(strafesnet_common::physics::OtherInstruction::Idle), }; worker.send(task).unwrap();