2023-10-19 00:17:21 +00:00
|
|
|
use crate::physics::{MouseState,PhysicsInputInstruction};
|
2024-01-30 00:19:41 +00:00
|
|
|
use strafesnet_common::integer::Time;
|
|
|
|
use strafesnet_common::instruction::{TimedInstruction,InstructionConsumer};
|
2023-10-19 00:17:21 +00:00
|
|
|
#[derive(Debug)]
|
2024-01-30 06:37:05 +00:00
|
|
|
pub enum InputInstruction{
|
2023-10-19 00:17:21 +00:00
|
|
|
MoveMouse(glam::IVec2),
|
|
|
|
MoveRight(bool),
|
|
|
|
MoveUp(bool),
|
|
|
|
MoveBack(bool),
|
|
|
|
MoveLeft(bool),
|
|
|
|
MoveDown(bool),
|
|
|
|
MoveForward(bool),
|
|
|
|
Jump(bool),
|
|
|
|
Zoom(bool),
|
|
|
|
Reset,
|
|
|
|
}
|
|
|
|
pub enum Instruction{
|
|
|
|
Input(InputInstruction),
|
|
|
|
Render,
|
|
|
|
Resize(winit::dpi::PhysicalSize<u32>,crate::settings::UserSettings),
|
2024-02-14 05:56:11 +00:00
|
|
|
GenerateModels(strafesnet_common::map::CompleteMap,strafesnet_deferred_loader::texture::Textures),
|
2023-10-19 00:17:21 +00:00
|
|
|
ClearModels,
|
|
|
|
//Graphics(crate::graphics_worker::Instruction),
|
|
|
|
}
|
|
|
|
|
2024-01-30 06:37:05 +00:00
|
|
|
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>>{
|
2023-10-19 00:17:21 +00:00
|
|
|
let mut mouse_blocking=true;
|
2024-01-30 06:37:05 +00:00
|
|
|
let mut last_mouse_time=physics.state.next_mouse.time;
|
2023-10-19 00:17:21 +00:00
|
|
|
let mut timeline=std::collections::VecDeque::new();
|
|
|
|
crate::compat_worker::QNWorker::new(move |ins:TimedInstruction<Instruction>|{
|
|
|
|
if if let Some(phys_input)=match &ins.instruction{
|
|
|
|
Instruction::Input(input_instruction)=>match input_instruction{
|
|
|
|
&InputInstruction::MoveMouse(m)=>{
|
|
|
|
if mouse_blocking{
|
|
|
|
//tell the game state which is living in the past about its future
|
|
|
|
timeline.push_front(TimedInstruction{
|
|
|
|
time: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
|
|
|
|
timeline.push_front(TimedInstruction{
|
|
|
|
time:last_mouse_time,
|
|
|
|
instruction:PhysicsInputInstruction::ReplaceMouse(
|
2024-01-30 06:37:05 +00:00
|
|
|
MouseState{time:last_mouse_time,pos:physics.state.next_mouse.pos},
|
2023-10-19 00:17:21 +00:00
|
|
|
MouseState{time:ins.time,pos:m}
|
|
|
|
),
|
|
|
|
});
|
|
|
|
//delay physics execution until we have an interpolation target
|
|
|
|
mouse_blocking=true;
|
|
|
|
}
|
|
|
|
last_mouse_time=ins.time;
|
|
|
|
None
|
|
|
|
},
|
|
|
|
&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),
|
|
|
|
},
|
2024-01-30 06:37:05 +00:00
|
|
|
Instruction::GenerateModels(_,_)=>Some(PhysicsInputInstruction::Idle),
|
2023-10-19 00:17:21 +00:00
|
|
|
Instruction::ClearModels=>Some(PhysicsInputInstruction::Idle),
|
|
|
|
Instruction::Resize(_,_)=>Some(PhysicsInputInstruction::Idle),
|
|
|
|
Instruction::Render=>Some(PhysicsInputInstruction::Idle),
|
|
|
|
}{
|
|
|
|
//non-mouse event
|
|
|
|
timeline.push_back(TimedInstruction{
|
|
|
|
time:ins.time,
|
|
|
|
instruction:phys_input,
|
|
|
|
});
|
|
|
|
|
|
|
|
if 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 :(
|
2024-01-30 06:37:05 +00:00
|
|
|
if Time::from_millis(10)<ins.time-physics.state.next_mouse.time{
|
2023-10-19 00:17:21 +00:00
|
|
|
//push an event to extrapolate no movement from
|
|
|
|
timeline.push_front(TimedInstruction{
|
|
|
|
time:last_mouse_time,
|
2024-01-30 06:37:05 +00:00
|
|
|
instruction:PhysicsInputInstruction::SetNextMouse(MouseState{time:ins.time,pos:physics.state.next_mouse.pos}),
|
2023-10-19 00:17:21 +00:00
|
|
|
});
|
|
|
|
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.
|
|
|
|
mouse_blocking=false;
|
|
|
|
true
|
|
|
|
}else{
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}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
|
|
|
|
last_mouse_time=ins.time;
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
//mouse event
|
|
|
|
true
|
|
|
|
}{
|
|
|
|
//empty queue
|
|
|
|
while let Some(instruction)=timeline.pop_front(){
|
|
|
|
physics.run(instruction.time);
|
|
|
|
physics.process_instruction(TimedInstruction{
|
|
|
|
time:instruction.time,
|
|
|
|
instruction:crate::physics::PhysicsInstruction::Input(instruction.instruction),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
match ins.instruction{
|
|
|
|
Instruction::Render=>{
|
2024-01-30 06:37:05 +00:00
|
|
|
graphics_worker.send(crate::graphics_worker::Instruction::Render(physics.state.output(),ins.time,physics.state.next_mouse.pos)).unwrap();
|
2023-10-19 00:17:21 +00:00
|
|
|
},
|
|
|
|
Instruction::Resize(size,user_settings)=>{
|
|
|
|
graphics_worker.send(crate::graphics_worker::Instruction::Resize(size,user_settings)).unwrap();
|
|
|
|
},
|
2024-01-30 06:37:05 +00:00
|
|
|
Instruction::GenerateModels(map,textures)=>{
|
|
|
|
physics.generate_models(&map);
|
|
|
|
physics.spawn();
|
|
|
|
graphics_worker.send(crate::graphics_worker::Instruction::GenerateModels(map,textures)).unwrap();
|
2023-10-19 00:17:21 +00:00
|
|
|
},
|
|
|
|
Instruction::ClearModels=>{
|
2024-01-30 06:37:05 +00:00
|
|
|
physics.state.clear();
|
2023-10-19 00:17:21 +00:00
|
|
|
graphics_worker.send(crate::graphics_worker::Instruction::ClearModels).unwrap();
|
|
|
|
},
|
|
|
|
_=>(),
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|