This commit is contained in:
Quaternions 2025-01-13 22:32:26 -08:00
parent b58ebb2775
commit bd61d03c91
7 changed files with 59 additions and 57 deletions

View File

@ -4,6 +4,12 @@ use crate::mouse::MouseState;
pub enum TimeInner{} pub enum TimeInner{}
pub type Time=crate::integer::Time<TimeInner>; pub type Time=crate::integer::Time<TimeInner>;
#[derive(Clone,Debug)]
pub enum UnbufferedInstruction{
MoveMouse(glam::IVec2),
Other(OtherInstruction),
}
#[derive(Clone,Debug)] #[derive(Clone,Debug)]
pub enum Instruction{ pub enum Instruction{
Mouse(MouseInstruction), Mouse(MouseInstruction),

View File

@ -1,9 +1,3 @@
#[derive(Clone,Copy,Hash,Eq,PartialEq,PartialOrd,Debug)] #[derive(Clone,Copy,Hash,Eq,PartialEq,PartialOrd,Debug)]
pub enum TimeInner{} pub enum TimeInner{}
pub type Time=crate::integer::Time<TimeInner>; pub type Time=crate::integer::Time<TimeInner>;
#[derive(Clone,Debug)]
pub enum UnbufferedInputInstruction{
MoveMouse(glam::IVec2),
Other(crate::physics::OtherInstruction),
}

View File

@ -1,16 +1,17 @@
use strafesnet_common::mouse::MouseState; use strafesnet_common::mouse::MouseState;
use strafesnet_common::physics::{ use strafesnet_common::physics::{
UnbufferedInstruction as PhysicsUnbufferedInstruction,
Instruction as PhysicsInputInstruction, Instruction as PhysicsInputInstruction,
Time as PhysicsTime, Time as PhysicsTime,
TimeInner as PhysicsTimeInner, TimeInner as PhysicsTimeInner,
MouseInstruction, MouseInstruction,
OtherInstruction, 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}; use strafesnet_common::instruction::{InstructionConsumer,InstructionEmitter,TimedInstruction};
type TimedPhysicsInstruction=TimedInstruction<PhysicsInputInstruction,PhysicsTimeInner>; type TimedPhysicsInstruction=TimedInstruction<PhysicsInputInstruction,PhysicsTimeInner>;
type PhysicsTimedUnbufferedInstruction=TimedInstruction<UnbufferedInputInstruction,PhysicsTimeInner>; type PhysicsTimedUnbufferedInstruction=TimedInstruction<PhysicsUnbufferedInstruction,PhysicsTimeInner>;
type DoubleTimedUnbufferedInstruction=TimedInstruction<PhysicsTimedUnbufferedInstruction,SessionTimeInner>; type DoubleTimedUnbufferedInstruction=TimedInstruction<PhysicsTimedUnbufferedInstruction,SessionTimeInner>;
const MOUSE_TIMEOUT:SessionTime=SessionTime::from_millis(10); 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<TimedPhysicsInstruction>)->(Self,Option<TimedInstruction<OtherInstruction,PhysicsTimeInner>>){ fn next_state(&self,ins:DoubleTimedUnbufferedInstruction,physics_timeline:&mut std::collections::VecDeque<TimedPhysicsInstruction>)->(Self,Option<TimedInstruction<OtherInstruction,PhysicsTimeInner>>){
match self{ match self{
BufferState::Unbuffered=>{ 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); return (BufferState::Initializing(ins.time,MouseState{pos,time:ins.instruction.time}),None);
} }
}, },
@ -49,7 +50,7 @@ impl BufferState{
}); });
return (BufferState::Unbuffered,None); 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}; let next_mouse_state=MouseState{pos,time:ins.instruction.time};
physics_timeline.push_front(TimedInstruction{ physics_timeline.push_front(TimedInstruction{
time:mouse_state.time, time:mouse_state.time,
@ -79,7 +80,7 @@ impl BufferState{
}); });
return (BufferState::Unbuffered,None); 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}; let next_mouse_state=MouseState{pos,time:ins.instruction.time};
physics_timeline.push_front(TimedInstruction{ physics_timeline.push_front(TimedInstruction{
time:ins.instruction.time, time:ins.instruction.time,
@ -92,8 +93,8 @@ impl BufferState{
}, },
} }
let instruction_out=match ins.instruction.instruction{ let instruction_out=match ins.instruction.instruction{
UnbufferedInputInstruction::MoveMouse(_)=>None, PhysicsUnbufferedInstruction::MoveMouse(_)=>None,
UnbufferedInputInstruction::Other(other_instruction)=>Some(TimedInstruction{ PhysicsUnbufferedInstruction::Other(other_instruction)=>Some(TimedInstruction{
time:ins.instruction.time, time:ins.instruction.time,
instruction:other_instruction, instruction:other_instruction,
}), }),
@ -208,9 +209,9 @@ impl MouseInterpolator{
// This should be simulation_timer.time(timeout) // This should be simulation_timer.time(timeout)
// but the timer is not accessible from this scope // but the timer is not accessible from this scope
// and it's just here to say that the mouse isn't moving anyways. // 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( 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})
), ),
}) })
}, },

View File

@ -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. // 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. // 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::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}; use crate::mouse_interpolator::{MouseInterpolator,StepInstruction};
pub enum ExternalInstruction{ pub enum ExternalInstruction{
ToBuffer(UnbufferedInputInstruction), Input(PhysicsUnbufferedInstruction),
SetPaused(bool), SetPaused(bool),
Render, Render,
Resize(winit::dpi::PhysicalSize<u32>), Resize(winit::dpi::PhysicalSize<u32>),
@ -89,7 +89,7 @@ impl InstructionConsumer<ExternalInstruction> for Session{
fn process_instruction(&mut self,ins:TimedInstruction<ExternalInstruction,Self::TimeInner>){ fn process_instruction(&mut self,ins:TimedInstruction<ExternalInstruction,Self::TimeInner>){
match ins.instruction{ match ins.instruction{
// send it down to MouseInterpolator with two timestamps, SessionTime and PhysicsTime // 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, time:ins.time,
instruction:TimedInstruction{ instruction:TimedInstruction{
time:self.simulation.timer.time(ins.time), time:self.simulation.timer.time(ins.time),

View File

@ -1,4 +1,4 @@
use crate::window::WindowInstruction; use crate::window::Instruction;
use strafesnet_common::instruction::TimedInstruction; use strafesnet_common::instruction::TimedInstruction;
use strafesnet_common::integer; use strafesnet_common::integer;
use strafesnet_common::session::TimeInner as SessionTimeInner; 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); let path=std::path::PathBuf::from(arg);
window_thread.send(TimedInstruction{ window_thread.send(TimedInstruction{
time:integer::Time::ZERO, time:integer::Time::ZERO,
instruction:WindowInstruction::WindowEvent(winit::event::WindowEvent::DroppedFile(path)), instruction:Instruction::WindowEvent(winit::event::WindowEvent::DroppedFile(path)),
}).unwrap(); }).unwrap();
}; };
@ -235,7 +235,7 @@ pub fn setup_and_start(title:&str){
fn run_event_loop( fn run_event_loop(
event_loop:winit::event_loop::EventLoop<()>, event_loop:winit::event_loop::EventLoop<()>,
mut window_thread:crate::compat_worker::QNWorker<TimedInstruction<WindowInstruction,SessionTimeInner>>, mut window_thread:crate::compat_worker::QNWorker<TimedInstruction<Instruction,SessionTimeInner>>,
root_time:std::time::Instant root_time:std::time::Instant
)->Result<(),winit::error::EventLoopError>{ )->Result<(),winit::error::EventLoopError>{
event_loop.run(move |event,elwt|{ event_loop.run(move |event,elwt|{
@ -247,7 +247,7 @@ fn run_event_loop(
// }; // };
match event{ match event{
winit::event::Event::AboutToWait=>{ 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 { winit::event::Event::WindowEvent {
event: event:
@ -259,7 +259,7 @@ fn run_event_loop(
winit::event::WindowEvent::Resized(size),//ignoring scale factor changed for now because mutex bruh winit::event::WindowEvent::Resized(size),//ignoring scale factor changed for now because mutex bruh
window_id:_, 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::Event::WindowEvent{event,..}=>match event{
winit::event::WindowEvent::KeyboardInput{ winit::event::WindowEvent::KeyboardInput{
@ -275,17 +275,17 @@ fn run_event_loop(
elwt.exit(); elwt.exit();
} }
winit::event::WindowEvent::RedrawRequested=>{ 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{ winit::event::Event::DeviceEvent{
event, event,
.. ..
} => { } => {
window_thread.send(TimedInstruction{time,instruction:WindowInstruction::DeviceEvent(event)}).unwrap(); window_thread.send(TimedInstruction{time,instruction:Instruction::DeviceEvent(event)}).unwrap();
}, },
_=>{} _=>{}
} }

View File

@ -1,8 +1,9 @@
use crate::physics_worker::InputInstruction;
use strafesnet_common::instruction::TimedInstruction; use strafesnet_common::instruction::TimedInstruction;
use strafesnet_common::session::{Time as SessionTime,TimeInner as SessionTimeInner}; 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<u32>), Resize(winit::dpi::PhysicalSize<u32>),
WindowEvent(winit::event::WindowEvent), WindowEvent(winit::event::WindowEvent),
DeviceEvent(winit::event::DeviceEvent), DeviceEvent(winit::event::DeviceEvent),
@ -16,7 +17,7 @@ struct WindowContext<'a>{
mouse:strafesnet_common::mouse::MouseState<SessionTimeInner>,//std::sync::Arc<std::sync::Mutex<>> mouse:strafesnet_common::mouse::MouseState<SessionTimeInner>,//std::sync::Arc<std::sync::Mutex<>>
screen_size:glam::UVec2, screen_size:glam::UVec2,
window:&'a winit::window::Window, window:&'a winit::window::Window,
physics_thread:crate::compat_worker::QNWorker<'a,TimedInstruction<crate::physics_worker::Instruction,SessionTimeInner>>, physics_thread:crate::compat_worker::QNWorker<'a,TimedInstruction<SessionExternalInstruction,SessionTimeInner>>,
} }
impl WindowContext<'_>{ impl WindowContext<'_>{
@ -27,7 +28,7 @@ impl WindowContext<'_>{
match event{ match event{
winit::event::WindowEvent::DroppedFile(path)=>{ winit::event::WindowEvent::DroppedFile(path)=>{
match crate::file::load(path.as_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}"), Err(e)=>println!("Failed to load map: {e}"),
} }
}, },
@ -35,7 +36,7 @@ impl WindowContext<'_>{
//pause unpause //pause unpause
self.physics_thread.send(TimedInstruction{ self.physics_thread.send(TimedInstruction{
time, time,
instruction:crate::physics_worker::Instruction::SetPaused(!state), instruction:SessionExternalInstruction::SetPaused(!state),
}).unwrap(); }).unwrap();
//recalculate pressed keys on focus //recalculate pressed keys on focus
}, },
@ -91,28 +92,28 @@ impl WindowContext<'_>{
(keycode,state)=>{ (keycode,state)=>{
let s=state.is_pressed(); let s=state.is_pressed();
if let Some(input_instruction)=match keycode{ 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(){ winit::keyboard::Key::Character(key)=>match key.as_str(){
"W"|"w"=>Some(InputInstruction::MoveForward(s)), "W"|"w"=>Some(OtherInstruction::SetMoveForward(s)),
"A"|"a"=>Some(InputInstruction::MoveLeft(s)), "A"|"a"=>Some(OtherInstruction::SetMoveLeft(s)),
"S"|"s"=>Some(InputInstruction::MoveBack(s)), "S"|"s"=>Some(OtherInstruction::SetMoveBack(s)),
"D"|"d"=>Some(InputInstruction::MoveRight(s)), "D"|"d"=>Some(OtherInstruction::SetMoveRight(s)),
"E"|"e"=>Some(InputInstruction::MoveUp(s)), "E"|"e"=>Some(OtherInstruction::SetMoveUp(s)),
"Q"|"q"=>Some(InputInstruction::MoveDown(s)), "Q"|"q"=>Some(OtherInstruction::SetMoveDown(s)),
"Z"|"z"=>Some(InputInstruction::Zoom(s)), "Z"|"z"=>Some(OtherInstruction::SetZoom(s)),
"R"|"r"=>if s{ "R"|"r"=>if s{
//mouse needs to be reset since the position is absolute //mouse needs to be reset since the position is absolute
self.mouse=strafesnet_common::mouse::MouseState::default(); self.mouse=strafesnet_common::mouse::MouseState::default();
Some(InputInstruction::ResetAndRestart) Some(OtherInstruction::ResetAndRestart)
}else{None}, }else{None},
"F"|"f"=>if s{Some(InputInstruction::PracticeFly)}else{None}, "F"|"f"=>if s{Some(OtherInstruction::PracticeFly)}else{None},
_=>None, _=>None,
}, },
_=>None, _=>None,
}{ }{
self.physics_thread.send(TimedInstruction{ self.physics_thread.send(TimedInstruction{
time, time,
instruction:crate::physics_worker::Instruction::Input(input_instruction), instruction:SessionExternalInstruction::Input(UnbufferedInstruction::Other(input_instruction)),
}).unwrap(); }).unwrap();
} }
}, },
@ -140,7 +141,7 @@ impl WindowContext<'_>{
self.mouse.pos+=delta; self.mouse.pos+=delta;
self.physics_thread.send(TimedInstruction{ self.physics_thread.send(TimedInstruction{
time, time,
instruction:crate::physics_worker::Instruction::Input(InputInstruction::MoveMouse(self.mouse.pos)), instruction:SessionExternalInstruction::Input(InputInstruction::MoveMouse(self.mouse.pos)),
}).unwrap(); }).unwrap();
}, },
winit::event::DeviceEvent::MouseWheel { winit::event::DeviceEvent::MouseWheel {
@ -150,7 +151,7 @@ impl WindowContext<'_>{
if false{//self.physics.style.use_scroll{ if false{//self.physics.style.use_scroll{
self.physics_thread.send(TimedInstruction{ self.physics_thread.send(TimedInstruction{
time, 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(); }).unwrap();
} }
}, },
@ -161,7 +162,7 @@ impl WindowContext<'_>{
pub fn worker<'a>( pub fn worker<'a>(
window:&'a winit::window::Window, window:&'a winit::window::Window,
setup_context:crate::setup::SetupContext<'a>, setup_context:crate::setup::SetupContext<'a>,
)->crate::compat_worker::QNWorker<'a,TimedInstruction<WindowInstruction,SessionTimeInner>>{ )->crate::compat_worker::QNWorker<'a,TimedInstruction<Instruction,SessionTimeInner>>{
// WindowContextSetup::new // WindowContextSetup::new
let user_settings=crate::settings::read_user_settings(); let user_settings=crate::settings::read_user_settings();
@ -184,30 +185,30 @@ pub fn worker<'a>(
}; };
//WindowContextSetup::into_worker //WindowContextSetup::into_worker
crate::compat_worker::QNWorker::new(move |ins:TimedInstruction<WindowInstruction,SessionTimeInner>|{ crate::compat_worker::QNWorker::new(move |ins:TimedInstruction<Instruction,SessionTimeInner>|{
match ins.instruction{ match ins.instruction{
WindowInstruction::RequestRedraw=>{ Instruction::RequestRedraw=>{
window_context.window.request_redraw(); window_context.window.request_redraw();
} }
WindowInstruction::WindowEvent(window_event)=>{ Instruction::WindowEvent(window_event)=>{
window_context.window_event(ins.time,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); window_context.device_event(ins.time,device_event);
}, },
WindowInstruction::Resize(size)=>{ Instruction::Resize(size)=>{
window_context.physics_thread.send( window_context.physics_thread.send(
TimedInstruction{ TimedInstruction{
time:ins.time, time:ins.time,
instruction:crate::physics_worker::Instruction::Resize(size) instruction:SessionExternalInstruction::Resize(size)
} }
).unwrap(); ).unwrap();
} }
WindowInstruction::Render=>{ Instruction::Render=>{
window_context.physics_thread.send( window_context.physics_thread.send(
TimedInstruction{ TimedInstruction{
time:ins.time, time:ins.time,
instruction:crate::physics_worker::Instruction::Render instruction:SessionExternalInstruction::Render
} }
).unwrap(); ).unwrap();
} }

View File

@ -190,7 +190,7 @@ mod test{
for _ in 0..5 { for _ in 0..5 {
let task = instruction::TimedInstruction{ let task = instruction::TimedInstruction{
time:strafesnet_common::physics::Time::ZERO, 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(); worker.send(task).unwrap();
} }
@ -204,7 +204,7 @@ mod test{
// Send a new task // Send a new task
let task = instruction::TimedInstruction{ let task = instruction::TimedInstruction{
time:integer::Time::ZERO, 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(); worker.send(task).unwrap();