diff --git a/src/physics_worker.rs b/src/physics_worker.rs index 7c294ac..60c9866 100644 --- a/src/physics_worker.rs +++ b/src/physics_worker.rs @@ -31,31 +31,71 @@ pub struct MouseInterpolator{ } impl MouseInterpolator{ fn handle_instruction(&mut self,physics:&mut crate::physics::PhysicsContext,ins:TimedInstruction){ + 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 t0=self.queue[0].time; let mut iter=self.queue.iter(); //find a mouse input - let mut mouse0=None; - iter.take_while(|&ins|{ - match ins.instruction{ - InputInstruction::MoveMouse(m)=>{ - mouse0=Some(m); - false - }, - _=>{ - //TODO: 10ms{ + let physics_input=match &ins0.instruction{ + InputInstruction::MoveMouse(mouse0)=>{ + 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 + //drain and handle the elements from the front + break 'outer; + }, + _=>{ + //TODO: 10ms{ + 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:ins0.time,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, + }); }, + None=>{ + //if mouse0 is never found and the loop ends, we can drain the entire queue + //because we are not waiting for mouse events. + break 'outer; + } } - }); - if mouse0.is_none(){ - return; - } - let mut mouse1=None; - for ins in iter{ - //fill mouse0 } } }