forked from StrafesNET/strafe-client
next_instruction non-optional time_limit
This commit is contained in:
parent
0fe14749d3
commit
a5aa89064b
11
src/body.rs
11
src/body.rs
@ -1,4 +1,4 @@
|
||||
use crate::instruction::TimedInstruction;
|
||||
use crate::instruction::{InstructionEmitter, InstructionConsumer, TimedInstruction};
|
||||
|
||||
pub enum PhysicsInstruction {
|
||||
CollisionStart(RelativeCollision),
|
||||
@ -210,10 +210,7 @@ impl PhysicsState {
|
||||
//tickless gaming
|
||||
pub fn run(&mut self, time: TIME){
|
||||
//prepare is ommitted - everything is done via instructions.
|
||||
while let Some(instruction) = self.next_instruction() {//collect
|
||||
if time<instruction.time {
|
||||
break;
|
||||
}
|
||||
while let Some(instruction) = self.next_instruction(time) {//collect
|
||||
//advance
|
||||
//self.advance_time(instruction.time);
|
||||
//process
|
||||
@ -281,9 +278,9 @@ impl PhysicsState {
|
||||
|
||||
impl crate::instruction::InstructionEmitter<PhysicsInstruction> for PhysicsState {
|
||||
//this little next instruction function can cache its return value and invalidate the cached value by watching the State.
|
||||
fn next_instruction(&self) -> Option<TimedInstruction<PhysicsInstruction>> {
|
||||
fn next_instruction(&self,time_limit:TIME) -> Option<TimedInstruction<PhysicsInstruction>> {
|
||||
//JUST POLLING!!! NO MUTATION
|
||||
let mut collector = crate::instruction::InstructionCollector::new();
|
||||
let mut collector = crate::instruction::InstructionCollector::new(time_limit);
|
||||
//autohop (already pressing spacebar; the signal to begin trying to jump is different)
|
||||
if self.grounded&&self.jump_trying {
|
||||
//scroll will be implemented with InputInstruction::Jump(true) but it blocks setting self.jump_trying=true
|
||||
|
@ -4,7 +4,7 @@ pub struct TimedInstruction<I> {
|
||||
}
|
||||
|
||||
pub trait InstructionEmitter<I> {
|
||||
fn next_instruction(&self) -> Option<TimedInstruction<I>>;
|
||||
fn next_instruction(&self, time:crate::body::TIME) -> Option<TimedInstruction<I>>;
|
||||
}
|
||||
pub trait InstructionConsumer<I> {
|
||||
fn process_instruction(&mut self, instruction:TimedInstruction<I>);
|
||||
@ -12,26 +12,36 @@ pub trait InstructionConsumer<I> {
|
||||
|
||||
//PROPER PRIVATE FIELDS!!!
|
||||
pub struct InstructionCollector<I> {
|
||||
instruction: Option<TimedInstruction<I>>,
|
||||
time: crate::body::TIME,
|
||||
instruction: Option<I>,
|
||||
}
|
||||
impl<I> InstructionCollector<I> {
|
||||
pub fn new() -> Self {
|
||||
Self{instruction:None}
|
||||
pub fn new(time:crate::body::TIME) -> Self {
|
||||
Self{
|
||||
time,
|
||||
instruction:None
|
||||
}
|
||||
}
|
||||
|
||||
pub fn collect(&mut self,instruction:Option<TimedInstruction<I>>){
|
||||
match &instruction {
|
||||
Some(unwrap_instruction) => match &self.instruction {
|
||||
Some(unwrap_best_instruction) => if unwrap_instruction.time<unwrap_best_instruction.time {
|
||||
self.instruction=instruction;
|
||||
},
|
||||
None => self.instruction=instruction,
|
||||
match instruction {
|
||||
Some(unwrap_instruction) => {
|
||||
if unwrap_instruction.time<self.time {
|
||||
self.time=unwrap_instruction.time;
|
||||
self.instruction=Some(unwrap_instruction.instruction);
|
||||
}
|
||||
},
|
||||
None => (),
|
||||
}
|
||||
}
|
||||
pub fn instruction(self) -> Option<TimedInstruction<I>> {
|
||||
//STEAL INSTRUCTION AND DESTROY INSTRUCTIONCOLLECTOR
|
||||
return self.instruction
|
||||
match self.instruction {
|
||||
Some(instruction)=>Some(TimedInstruction{
|
||||
time:self.time,
|
||||
instruction
|
||||
}),
|
||||
None => None,
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user