diff --git a/src/body.rs b/src/body.rs index b47a380..dec612e 100644 --- a/src/body.rs +++ b/src/body.rs @@ -80,36 +80,25 @@ impl crate::event::EventTrait for PhysicsState { //this little next event function can cache its return value and invalidate the cached value by watching the State. fn next_event(&self) -> Option { //JUST POLLING!!! NO MUTATION - let mut best_event: Option = None; - let collect_event = |test_event:Option|{ - match test_event { - Some(unwrap_test_event) => match best_event { - Some(unwrap_best_event) => if unwrap_test_event.time best_event=test_event, - }, - None => (), - } - }; + let mut best = crate::event::EventCollector::new(); //autohop (already pressing spacebar; the signal to begin trying to jump is different) if self.grounded&&self.jump_trying { //scroll will be implemented with InputEvent::Jump(true) but it blocks setting self.jump_trying=true - collect_event(Some(EventStruct{ + best.collect(Some(EventStruct{ time:self.time, event:crate::event::EventEnum::Jump })); } //check for collision stop events with curent contacts for collision_data in self.contacts.iter() { - collect_event(self.predict_collision(collision_data.model)); + best.collect(self.predict_collision(collision_data.model)); } //check for collision start events (against every part in the game with no optimization!!) for &model in self.world.models { - collect_event(self.predict_collision(&model)); + best.collect(self.predict_collision(&model)); } //check to see when the next strafe tick is - collect_event(self.next_strafe_event()); - best_event + best.collect(self.next_strafe_event()); + best.event() } } diff --git a/src/event.rs b/src/event.rs index 6f45098..d226031 100644 --- a/src/event.rs +++ b/src/event.rs @@ -12,4 +12,30 @@ pub enum EventEnum { pub trait EventTrait { fn next_event(&self) -> Option; +} + +//PROPER PRIVATE FIELDS!!! +pub struct EventCollector { + event: Option, +} +impl EventCollector { + pub fn new() -> Self { + Self{event:None} + } + + pub fn collect(&mut self,test_event:Option){ + match &test_event { + Some(unwrap_test_event) => match &self.event { + Some(unwrap_best_event) => if unwrap_test_event.time self.event=test_event, + }, + None => (), + } + } + pub fn event(self) -> Option { + //STEAL EVENT AND DESTROY EVENTCOLLECTOR + return self.event + } } \ No newline at end of file