generic events

This commit is contained in:
Quaternions 2023-09-08 17:00:50 -07:00
parent d2d62007ba
commit 86df619eb9
2 changed files with 24 additions and 24 deletions

View File

@ -1,5 +1,12 @@
use crate::event::EventStruct; use crate::event::EventStruct;
pub enum PhysicsEvent {
CollisionStart(RelativeCollision),
CollisionEnd(RelativeCollision),
StrafeTick,
Jump,
}
pub struct Body { pub struct Body {
pub position: glam::Vec3,//I64 where 2^32 = 1 u pub position: glam::Vec3,//I64 where 2^32 = 1 u
pub velocity: glam::Vec3,//I64 where 2^32 = 1 u/s pub velocity: glam::Vec3,//I64 where 2^32 = 1 u/s
@ -206,28 +213,28 @@ impl PhysicsState {
self.body.position+self.body.velocity*(dt as f32)+self.gravity*((0.5*dt*dt) as f32) self.body.position+self.body.velocity*(dt as f32)+self.gravity*((0.5*dt*dt) as f32)
} }
fn next_strafe_event(&self) -> Option<EventStruct> { fn next_strafe_event(&self) -> Option<EventStruct<PhysicsEvent>> {
return Some(EventStruct{ return Some(EventStruct{
time:(self.time*self.strafe_tick_num/self.strafe_tick_den+1)*self.strafe_tick_den/self.strafe_tick_num, time:(self.time*self.strafe_tick_num/self.strafe_tick_den+1)*self.strafe_tick_den/self.strafe_tick_num,
event:crate::event::EventEnum::StrafeTick event:PhysicsEvent::StrafeTick
}); });
} }
fn next_walk_event(&self) -> Option<EventStruct> { fn next_walk_event(&self) -> Option<EventStruct<PhysicsEvent>> {
//check if you are accelerating towards a walk target velocity and create an event //check if you are accelerating towards a walk target velocity and create an event
return None; return None;
} }
fn predict_collision_end(&self,model:&Model) -> Option<EventStruct> { fn predict_collision_end(&self,model:&Model) -> Option<EventStruct<PhysicsEvent>> {
None None
} }
fn predict_collision_start(&self,model:&Model) -> Option<EventStruct> { fn predict_collision_start(&self,model:&Model) -> Option<EventStruct<PhysicsEvent>> {
None None
} }
} }
impl crate::event::EventTrait for PhysicsState { impl crate::event::EventEmitter<PhysicsEvent> for PhysicsState {
//this little next event function can cache its return value and invalidate the cached value by watching the State. //this little next event function can cache its return value and invalidate the cached value by watching the State.
fn next_event(&self) -> Option<EventStruct> { fn next_event(&self) -> Option<EventStruct<PhysicsEvent>> {
//JUST POLLING!!! NO MUTATION //JUST POLLING!!! NO MUTATION
let mut best = crate::event::EventCollector::new(); let mut best = crate::event::EventCollector::new();
//autohop (already pressing spacebar; the signal to begin trying to jump is different) //autohop (already pressing spacebar; the signal to begin trying to jump is different)
@ -235,7 +242,7 @@ impl crate::event::EventTrait for PhysicsState {
//scroll will be implemented with InputEvent::Jump(true) but it blocks setting self.jump_trying=true //scroll will be implemented with InputEvent::Jump(true) but it blocks setting self.jump_trying=true
best.collect(Some(EventStruct{ best.collect(Some(EventStruct{
time:self.time, time:self.time,
event:crate::event::EventEnum::Jump event:PhysicsEvent::Jump
})); }));
} }
//check for collision stop events with curent contacts //check for collision stop events with curent contacts

View File

@ -1,29 +1,22 @@
pub struct EventStruct { pub struct EventStruct<E> {
pub time: crate::body::TIME, pub time: crate::body::TIME,
pub event: EventEnum, pub event: E,
} }
pub enum EventEnum { pub trait EventEmitter<E> {
CollisionStart(crate::body::RelativeCollision),//Body::CollisionStart fn next_event(&self) -> Option<EventStruct<E>>;
CollisionEnd(crate::body::RelativeCollision),//Body::CollisionEnd
StrafeTick,
Jump,
}
pub trait EventTrait {
fn next_event(&self) -> Option<EventStruct>;
} }
//PROPER PRIVATE FIELDS!!! //PROPER PRIVATE FIELDS!!!
pub struct EventCollector { pub struct EventCollector<E> {
event: Option<EventStruct>, event: Option<EventStruct<E>>,
} }
impl EventCollector { impl<E> EventCollector<E> {
pub fn new() -> Self { pub fn new() -> Self {
Self{event:None} Self{event:None}
} }
pub fn collect(&mut self,test_event:Option<EventStruct>){ pub fn collect(&mut self,test_event:Option<EventStruct<E>>){
match &test_event { match &test_event {
Some(unwrap_test_event) => match &self.event { Some(unwrap_test_event) => match &self.event {
Some(unwrap_best_event) => if unwrap_test_event.time<unwrap_best_event.time { Some(unwrap_best_event) => if unwrap_test_event.time<unwrap_best_event.time {
@ -34,7 +27,7 @@ impl EventCollector {
None => (), None => (),
} }
} }
pub fn event(self) -> Option<EventStruct> { pub fn event(self) -> Option<EventStruct<E>> {
//STEAL EVENT AND DESTROY EVENTCOLLECTOR //STEAL EVENT AND DESTROY EVENTCOLLECTOR
return self.event return self.event
} }