diff --git a/src/body.rs b/src/body.rs index 210bcbc4..78117183 100644 --- a/src/body.rs +++ b/src/body.rs @@ -6,6 +6,7 @@ pub struct Body { pub struct PhysicsState { pub body: Body, + //pub contacts: Vec, pub time: TIME, pub strafe_tick_period: TIME, pub tick: u32, @@ -14,6 +15,7 @@ pub struct PhysicsState { pub mv: f32, pub grounded: bool, pub walkspeed: f32, + pub jump_trying: bool, } pub type TIME = i64; @@ -62,4 +64,49 @@ impl PhysicsState { let dt=(time-self.body.time) as f64/1_000_000_000f64; self.body.position+self.body.velocity*(dt as f32)+self.gravity*((0.5*dt*dt) as f32) } + + fn next_strafe_event(&self) -> Option { + return Some(crate::event::EventStruct{ + time:(self.time/self.strafe_tick_period+1)*self.strafe_tick_period, + event:crate::event::EventEnum::StrafeTick + }); + } +} + +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 => (), + } + }; + //check to see if yee need to jump (this is not the way lol) + if self.grounded&&self.jump_trying { + //scroll will be implemented with InputEvent::InstantJump rather than InputEvent::Jump(true) + collect_event(Some(crate::event::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.model.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.model.predict_collision(&model)); + } + //check to see when the next strafe tick is + collect_event(self.next_strafe_event()); + best_event + } } diff --git a/src/event.rs b/src/event.rs new file mode 100644 index 00000000..6f45098e --- /dev/null +++ b/src/event.rs @@ -0,0 +1,15 @@ +pub struct EventStruct { + pub time: crate::body::TIME, + pub event: EventEnum, +} + +pub enum EventEnum { + CollisionStart,//(Collideable),//Body::CollisionStart + CollisionEnd,//(Collideable),//Body::CollisionEnd + StrafeTick, + Jump, +} + +pub trait EventTrait { + fn next_event(&self) -> Option; +} \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 2aaeac4a..b229c530 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,2 +1,3 @@ pub mod framework; pub mod body; +pub mod event; diff --git a/src/main.rs b/src/main.rs index f862e1e7..657e9578 100644 --- a/src/main.rs +++ b/src/main.rs @@ -304,6 +304,7 @@ impl strafe_client::framework::Example for Skybox { friction: 90.0, mv: 2.7, grounded: true, + jump_trying: false, walkspeed: 18.0, };