|
|
|
|
@@ -16,7 +16,10 @@ pub enum InputInstruction{
|
|
|
|
|
PracticeFly,
|
|
|
|
|
}
|
|
|
|
|
pub enum Instruction{
|
|
|
|
|
Input(InputInstruction),
|
|
|
|
|
Passthrough(PassthroughInstruction),
|
|
|
|
|
Interpolate(InputInstruction),
|
|
|
|
|
}
|
|
|
|
|
pub enum PassthroughInstruction{
|
|
|
|
|
Render,
|
|
|
|
|
Resize(winit::dpi::PhysicalSize<u32>,crate::settings::UserSettings),
|
|
|
|
|
GenerateModels(strafesnet_common::map::CompleteMap),
|
|
|
|
|
@@ -24,140 +27,179 @@ pub enum Instruction{
|
|
|
|
|
//Graphics(crate::graphics_worker::Instruction),
|
|
|
|
|
}
|
|
|
|
|
pub struct MouseInterpolator{
|
|
|
|
|
timeline:std::collections::VecDeque<TimedInstruction<PhysicsInputInstruction>>,
|
|
|
|
|
last_mouse_time:Time,
|
|
|
|
|
mouse_blocking:bool,
|
|
|
|
|
queue:std::collections::VecDeque<TimedInstruction<InputInstruction>>,
|
|
|
|
|
}
|
|
|
|
|
fn drain_queue(physics:&mut crate::physics::PhysicsContext,iterable:impl IntoIterator<Item=TimedInstruction<InputInstruction>>){
|
|
|
|
|
for ins in iterable{
|
|
|
|
|
let physics_input=match &ins.instruction{
|
|
|
|
|
InputInstruction::MoveMouse(_)=>panic!("Queue was confirmed to contain no MoveMouse events1"),
|
|
|
|
|
&InputInstruction::MoveForward(s)=>PhysicsInputInstruction::SetMoveForward(s),
|
|
|
|
|
&InputInstruction::MoveLeft(s)=>PhysicsInputInstruction::SetMoveLeft(s),
|
|
|
|
|
&InputInstruction::MoveBack(s)=>PhysicsInputInstruction::SetMoveBack(s),
|
|
|
|
|
&InputInstruction::MoveRight(s)=>PhysicsInputInstruction::SetMoveRight(s),
|
|
|
|
|
&InputInstruction::MoveUp(s)=>PhysicsInputInstruction::SetMoveUp(s),
|
|
|
|
|
&InputInstruction::MoveDown(s)=>PhysicsInputInstruction::SetMoveDown(s),
|
|
|
|
|
&InputInstruction::Jump(s)=>PhysicsInputInstruction::SetJump(s),
|
|
|
|
|
&InputInstruction::Zoom(s)=>PhysicsInputInstruction::SetZoom(s),
|
|
|
|
|
InputInstruction::Reset=>PhysicsInputInstruction::Reset,
|
|
|
|
|
InputInstruction::PracticeFly=>PhysicsInputInstruction::PracticeFly,
|
|
|
|
|
};
|
|
|
|
|
physics.run_input_instruction(TimedInstruction{
|
|
|
|
|
time:ins.time,
|
|
|
|
|
instruction:physics_input,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
impl MouseInterpolator{
|
|
|
|
|
fn push_mouse_instruction(&mut self,physics:&crate::physics::PhysicsContext,ins:&TimedInstruction<Instruction>,m:glam::IVec2){
|
|
|
|
|
if self.mouse_blocking{
|
|
|
|
|
//tell the game state which is living in the past about its future
|
|
|
|
|
self.timeline.push_front(TimedInstruction{
|
|
|
|
|
time:self.last_mouse_time,
|
|
|
|
|
instruction:PhysicsInputInstruction::SetNextMouse(MouseState{time:ins.time,pos:m}),
|
|
|
|
|
});
|
|
|
|
|
}else{
|
|
|
|
|
//mouse has just started moving again after being still for longer than 10ms.
|
|
|
|
|
//replace the entire mouse interpolation state to avoid an intermediate state with identical m0.t m1.t timestamps which will divide by zero
|
|
|
|
|
self.timeline.push_front(TimedInstruction{
|
|
|
|
|
time:self.last_mouse_time,
|
|
|
|
|
instruction:PhysicsInputInstruction::ReplaceMouse(
|
|
|
|
|
MouseState{time:self.last_mouse_time,pos:physics.get_next_mouse().pos},
|
|
|
|
|
MouseState{time:ins.time,pos:m}
|
|
|
|
|
),
|
|
|
|
|
});
|
|
|
|
|
//delay physics execution until we have an interpolation target
|
|
|
|
|
self.mouse_blocking=true;
|
|
|
|
|
}
|
|
|
|
|
self.last_mouse_time=ins.time;
|
|
|
|
|
}
|
|
|
|
|
/// returns the mapped physics input instruction
|
|
|
|
|
/// may or may not mutate internal state XD!
|
|
|
|
|
fn map_instruction(&mut self,physics:&crate::physics::PhysicsContext,ins:&TimedInstruction<Instruction>)->Option<PhysicsInputInstruction>{
|
|
|
|
|
match &ins.instruction{
|
|
|
|
|
Instruction::Input(input_instruction)=>match input_instruction{
|
|
|
|
|
&InputInstruction::MoveMouse(m)=>{
|
|
|
|
|
self.push_mouse_instruction(physics,ins,m);
|
|
|
|
|
None
|
|
|
|
|
fn handle_instruction(&mut self,physics:&mut crate::physics::PhysicsContext,ins:TimedInstruction<InputInstruction>){
|
|
|
|
|
//need to handle the case where mouse polling rate is less than 100hz
|
|
|
|
|
//also the whole thing is probably wrong lol
|
|
|
|
|
let is_inserting_mouse_instruction=matches!(ins.instruction,InputInstruction::MoveMouse(_));
|
|
|
|
|
self.queue.push_back(ins);
|
|
|
|
|
//We just pushed an element.
|
|
|
|
|
//The first element is guaranteed to exist.
|
|
|
|
|
let mut iter=self.queue.iter();
|
|
|
|
|
//find a mouse input
|
|
|
|
|
'outer:loop{
|
|
|
|
|
match iter.next(){
|
|
|
|
|
Some(ins0)=>{
|
|
|
|
|
let physics_input=match &ins0.instruction{
|
|
|
|
|
&InputInstruction::MoveMouse(mut mouse0)=>{
|
|
|
|
|
//mouse instruction found.
|
|
|
|
|
//enter a new loop with different behaviour
|
|
|
|
|
//we have to wait for the next mouse event
|
|
|
|
|
//so there is a before and after interpolation target
|
|
|
|
|
//write down ins0.time to appease the borrow checker
|
|
|
|
|
let mut t0=ins0.time;
|
|
|
|
|
'inner:loop{
|
|
|
|
|
match iter.next(){
|
|
|
|
|
Some(ins1)=>match &ins1.instruction{
|
|
|
|
|
&InputInstruction::MoveMouse(mouse1)=>{
|
|
|
|
|
//we found two mouse events to interpolate between
|
|
|
|
|
let consume_count=self.queue.len()-iter.len()-1;//don't consume the mouse1 instruction
|
|
|
|
|
//fire off a mouse instruction
|
|
|
|
|
physics.run_input_instruction(TimedInstruction{
|
|
|
|
|
time:t0,
|
|
|
|
|
instruction:PhysicsInputInstruction::SetNextMouse(
|
|
|
|
|
MouseState{time:ins1.time,pos:mouse1}
|
|
|
|
|
),
|
|
|
|
|
});
|
|
|
|
|
//update inner loop state
|
|
|
|
|
mouse0=mouse1;
|
|
|
|
|
t0=ins1.time;
|
|
|
|
|
//drain and handle the elements from the front
|
|
|
|
|
std::mem::drop(iter);
|
|
|
|
|
let mut hot_queue=self.queue.drain(0..consume_count);
|
|
|
|
|
hot_queue.next();
|
|
|
|
|
drain_queue(physics,hot_queue);
|
|
|
|
|
iter=self.queue.iter();
|
|
|
|
|
//keep looking for another mouse instruction in the inner loop
|
|
|
|
|
continue 'inner;
|
|
|
|
|
},
|
|
|
|
|
_=>if Time::from_millis(10)<ins1.time-t0{
|
|
|
|
|
//we have passed more than 10ms of instructions and have not seen a mouse event.
|
|
|
|
|
let consume_count=self.queue.len()-iter.len();
|
|
|
|
|
//run an event to extrapolate no movement from
|
|
|
|
|
let last_mouse=physics.get_next_mouse();
|
|
|
|
|
physics.run_input_instruction(TimedInstruction{
|
|
|
|
|
time:last_mouse.time,
|
|
|
|
|
instruction:PhysicsInputInstruction::SetNextMouse(
|
|
|
|
|
MouseState{time:ins1.time,pos:last_mouse.pos}
|
|
|
|
|
),
|
|
|
|
|
});
|
|
|
|
|
//drop the iterator so we can consume the queue up to this point
|
|
|
|
|
std::mem::drop(iter);
|
|
|
|
|
//consume queue up to the scanned point
|
|
|
|
|
let mut hot_queue=self.queue.drain(0..consume_count);
|
|
|
|
|
//the first element is always the last mouse instruction (last_mouse above)
|
|
|
|
|
hot_queue.next();
|
|
|
|
|
drain_queue(physics,hot_queue);
|
|
|
|
|
//make a new iterator starting from the new beginning
|
|
|
|
|
//and continue looping like nothing happened
|
|
|
|
|
iter=self.queue.iter();
|
|
|
|
|
continue 'outer;
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
None=>{
|
|
|
|
|
if is_inserting_mouse_instruction{
|
|
|
|
|
//the mouse started moving again after being still for over 10ms.
|
|
|
|
|
//replace the entire mouse state
|
|
|
|
|
physics.run_input_instruction(TimedInstruction{
|
|
|
|
|
time:physics.get_next_mouse().time,
|
|
|
|
|
instruction:PhysicsInputInstruction::ReplaceMouse(
|
|
|
|
|
physics.get_next_mouse().clone(),
|
|
|
|
|
MouseState{time:t0,pos:mouse0}
|
|
|
|
|
),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
break 'outer;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
&InputInstruction::MoveForward(s)=>PhysicsInputInstruction::SetMoveForward(s),
|
|
|
|
|
&InputInstruction::MoveLeft(s)=>PhysicsInputInstruction::SetMoveLeft(s),
|
|
|
|
|
&InputInstruction::MoveBack(s)=>PhysicsInputInstruction::SetMoveBack(s),
|
|
|
|
|
&InputInstruction::MoveRight(s)=>PhysicsInputInstruction::SetMoveRight(s),
|
|
|
|
|
&InputInstruction::MoveUp(s)=>PhysicsInputInstruction::SetMoveUp(s),
|
|
|
|
|
&InputInstruction::MoveDown(s)=>PhysicsInputInstruction::SetMoveDown(s),
|
|
|
|
|
&InputInstruction::Jump(s)=>PhysicsInputInstruction::SetJump(s),
|
|
|
|
|
&InputInstruction::Zoom(s)=>PhysicsInputInstruction::SetZoom(s),
|
|
|
|
|
InputInstruction::Reset=>PhysicsInputInstruction::Reset,
|
|
|
|
|
InputInstruction::PracticeFly=>PhysicsInputInstruction::PracticeFly,
|
|
|
|
|
};
|
|
|
|
|
//handle each event immediately, we are not waiting for mouse
|
|
|
|
|
physics.run_input_instruction(TimedInstruction{
|
|
|
|
|
time:ins0.time,
|
|
|
|
|
instruction:physics_input,
|
|
|
|
|
});
|
|
|
|
|
//drop it and pop it! consume one element and continue the loop
|
|
|
|
|
std::mem::drop(iter);
|
|
|
|
|
self.queue.pop_front();
|
|
|
|
|
iter=self.queue.iter();
|
|
|
|
|
},
|
|
|
|
|
&InputInstruction::MoveForward(s)=>Some(PhysicsInputInstruction::SetMoveForward(s)),
|
|
|
|
|
&InputInstruction::MoveLeft(s)=>Some(PhysicsInputInstruction::SetMoveLeft(s)),
|
|
|
|
|
&InputInstruction::MoveBack(s)=>Some(PhysicsInputInstruction::SetMoveBack(s)),
|
|
|
|
|
&InputInstruction::MoveRight(s)=>Some(PhysicsInputInstruction::SetMoveRight(s)),
|
|
|
|
|
&InputInstruction::MoveUp(s)=>Some(PhysicsInputInstruction::SetMoveUp(s)),
|
|
|
|
|
&InputInstruction::MoveDown(s)=>Some(PhysicsInputInstruction::SetMoveDown(s)),
|
|
|
|
|
&InputInstruction::Jump(s)=>Some(PhysicsInputInstruction::SetJump(s)),
|
|
|
|
|
&InputInstruction::Zoom(s)=>Some(PhysicsInputInstruction::SetZoom(s)),
|
|
|
|
|
InputInstruction::Reset=>Some(PhysicsInputInstruction::Reset),
|
|
|
|
|
InputInstruction::PracticeFly=>Some(PhysicsInputInstruction::PracticeFly),
|
|
|
|
|
},
|
|
|
|
|
Instruction::GenerateModels(_)=>Some(PhysicsInputInstruction::Idle),
|
|
|
|
|
Instruction::ClearModels=>Some(PhysicsInputInstruction::Idle),
|
|
|
|
|
Instruction::Resize(_,_)=>Some(PhysicsInputInstruction::Idle),
|
|
|
|
|
Instruction::Render=>Some(PhysicsInputInstruction::Idle),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
fn update_mouse_blocking(&mut self,physics:&crate::physics::PhysicsContext,ins:&TimedInstruction<Instruction>)->bool{
|
|
|
|
|
if self.mouse_blocking{
|
|
|
|
|
//assume the mouse has stopped moving after 10ms.
|
|
|
|
|
//shitty mice are 125Hz which is 8ms so this should cover that.
|
|
|
|
|
//setting this to 100us still doesn't print even though it's 10x lower than the polling rate,
|
|
|
|
|
//so mouse events are probably not handled separately from drawing and fire right before it :(
|
|
|
|
|
if Time::from_millis(10)<ins.time-physics.get_next_mouse().time{
|
|
|
|
|
//push an event to extrapolate no movement from
|
|
|
|
|
self.timeline.push_front(TimedInstruction{
|
|
|
|
|
time:self.last_mouse_time,
|
|
|
|
|
instruction:PhysicsInputInstruction::SetNextMouse(MouseState{time:ins.time,pos:physics.get_next_mouse().pos}),
|
|
|
|
|
});
|
|
|
|
|
self.last_mouse_time=ins.time;
|
|
|
|
|
//stop blocking. the mouse is not moving so the physics does not need to live in the past and wait for interpolation targets.
|
|
|
|
|
self.mouse_blocking=false;
|
|
|
|
|
true
|
|
|
|
|
}else{
|
|
|
|
|
false
|
|
|
|
|
None=>{
|
|
|
|
|
//if mouse0 is never found and the loop ends, we can drain the entire queue
|
|
|
|
|
//because we are not waiting for mouse events.
|
|
|
|
|
drain_queue(physics,self.queue.drain(..));
|
|
|
|
|
break 'outer;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}else{
|
|
|
|
|
//keep this up to date so that it can be used as a known-timestamp
|
|
|
|
|
//that the mouse was not moving when the mouse starts moving again
|
|
|
|
|
self.last_mouse_time=ins.time;
|
|
|
|
|
true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/// returns whether or not to empty the instruction queue
|
|
|
|
|
fn handle_physics_input(&mut self,physics:&crate::physics::PhysicsContext,ins:&TimedInstruction<Instruction>,phys_input_option:Option<PhysicsInputInstruction>)->bool{
|
|
|
|
|
if let Some(phys_input)=phys_input_option{
|
|
|
|
|
//non-mouse event
|
|
|
|
|
self.timeline.push_back(TimedInstruction{
|
|
|
|
|
time:ins.time,
|
|
|
|
|
instruction:phys_input,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
//this returns the bool for us
|
|
|
|
|
self.update_mouse_blocking(physics,ins)
|
|
|
|
|
}else{
|
|
|
|
|
//mouse event
|
|
|
|
|
true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
fn empty_queue(&mut self,physics:&mut crate::physics::PhysicsContext){
|
|
|
|
|
while let Some(instruction)=self.timeline.pop_front(){
|
|
|
|
|
physics.run_input_instruction(instruction);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
fn handle_instruction(&mut self,physics:&mut crate::physics::PhysicsContext,ins:&TimedInstruction<Instruction>){
|
|
|
|
|
let physics_input_option=self.map_instruction(physics,ins);
|
|
|
|
|
let should_empty_queue=self.handle_physics_input(physics,ins,physics_input_option);
|
|
|
|
|
if should_empty_queue{
|
|
|
|
|
self.empty_queue(physics);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn new(mut physics:crate::physics::PhysicsContext,mut graphics_worker:crate::compat_worker::INWorker<crate::graphics_worker::Instruction>)->crate::compat_worker::QNWorker<TimedInstruction<Instruction>>{
|
|
|
|
|
let mut interpolator=MouseInterpolator{
|
|
|
|
|
mouse_blocking:true,
|
|
|
|
|
last_mouse_time:physics.get_next_mouse().time,
|
|
|
|
|
timeline:std::collections::VecDeque::new(),
|
|
|
|
|
queue:std::collections::VecDeque::new(),
|
|
|
|
|
};
|
|
|
|
|
crate::compat_worker::QNWorker::new(move |ins:TimedInstruction<Instruction>|{
|
|
|
|
|
interpolator.handle_instruction(&mut physics,&ins);
|
|
|
|
|
match ins.instruction{
|
|
|
|
|
Instruction::Render=>{
|
|
|
|
|
let passthrough_instruction=match ins.instruction{
|
|
|
|
|
Instruction::Passthrough(passthrough_instruction)=>passthrough_instruction,
|
|
|
|
|
Instruction::Interpolate(input_instruction)=>{
|
|
|
|
|
interpolator.handle_instruction(&mut physics,TimedInstruction{
|
|
|
|
|
instruction:input_instruction,
|
|
|
|
|
time:ins.time,
|
|
|
|
|
});
|
|
|
|
|
return;
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
match passthrough_instruction{
|
|
|
|
|
PassthroughInstruction::Render=>{
|
|
|
|
|
graphics_worker.send(crate::graphics_worker::Instruction::Render(physics.output(),ins.time,physics.get_next_mouse().pos)).unwrap();
|
|
|
|
|
},
|
|
|
|
|
Instruction::Resize(size,user_settings)=>{
|
|
|
|
|
PassthroughInstruction::Resize(size,user_settings)=>{
|
|
|
|
|
graphics_worker.send(crate::graphics_worker::Instruction::Resize(size,user_settings)).unwrap();
|
|
|
|
|
},
|
|
|
|
|
Instruction::GenerateModels(map)=>{
|
|
|
|
|
PassthroughInstruction::GenerateModels(map)=>{
|
|
|
|
|
physics.generate_models(&map);
|
|
|
|
|
physics.spawn();
|
|
|
|
|
graphics_worker.send(crate::graphics_worker::Instruction::GenerateModels(map)).unwrap();
|
|
|
|
|
},
|
|
|
|
|
Instruction::ClearModels=>{
|
|
|
|
|
PassthroughInstruction::ClearModels=>{
|
|
|
|
|
physics.clear();
|
|
|
|
|
graphics_worker.send(crate::graphics_worker::Instruction::ClearModels).unwrap();
|
|
|
|
|
},
|
|
|
|
|
_=>(),
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|