forked from StrafesNET/strafe-project
204 lines
8.1 KiB
Rust
204 lines
8.1 KiB
Rust
use crate::physics::{MouseState,PhysicsInputInstruction};
|
|
use strafesnet_common::integer::Time;
|
|
use strafesnet_common::instruction::{TimedInstruction,InstructionConsumer};
|
|
#[derive(Debug)]
|
|
pub enum InputInstruction{
|
|
MoveMouse(glam::IVec2),
|
|
MoveRight(bool),
|
|
MoveUp(bool),
|
|
MoveBack(bool),
|
|
MoveLeft(bool),
|
|
MoveDown(bool),
|
|
MoveForward(bool),
|
|
Jump(bool),
|
|
Zoom(bool),
|
|
Reset,
|
|
PracticeFly,
|
|
}
|
|
pub enum Instruction{
|
|
Passthrough(PassthroughInstruction),
|
|
Interpolate(InputInstruction),
|
|
}
|
|
pub enum PassthroughInstruction{
|
|
Render,
|
|
Resize(winit::dpi::PhysicalSize<u32>,crate::settings::UserSettings),
|
|
GenerateModels(strafesnet_common::map::CompleteMap),
|
|
ClearModels,
|
|
//Graphics(crate::graphics_worker::Instruction),
|
|
}
|
|
pub struct MouseInterpolator{
|
|
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 handle_instruction(&mut self,physics:&mut crate::physics::PhysicsContext,ins:TimedInstruction<InputInstruction>){
|
|
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();
|
|
},
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
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{
|
|
queue:std::collections::VecDeque::new(),
|
|
};
|
|
crate::compat_worker::QNWorker::new(move |ins:TimedInstruction<Instruction>|{
|
|
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();
|
|
},
|
|
PassthroughInstruction::Resize(size,user_settings)=>{
|
|
graphics_worker.send(crate::graphics_worker::Instruction::Resize(size,user_settings)).unwrap();
|
|
},
|
|
PassthroughInstruction::GenerateModels(map)=>{
|
|
physics.generate_models(&map);
|
|
physics.spawn();
|
|
graphics_worker.send(crate::graphics_worker::Instruction::GenerateModels(map)).unwrap();
|
|
},
|
|
PassthroughInstruction::ClearModels=>{
|
|
physics.clear();
|
|
graphics_worker.send(crate::graphics_worker::Instruction::ClearModels).unwrap();
|
|
},
|
|
}
|
|
})
|
|
}
|