rust/hwphysics/src/time.rs
changeset 15275 66c987015f2d
parent 15274 42b710b0f883
child 15282 478d5372eb4a
equal deleted inserted replaced
15274:42b710b0f883 15275:66c987015f2d
     1 use crate::common::{GearDataProcessor, GearId};
     1 use crate::common::{GearDataProcessor, GearId, Millis};
     2 use fpnum::{fp, FPNum};
       
     3 use std::{
     2 use std::{
     4     cmp::{Eq, Ord, Ordering, PartialEq, PartialOrd},
     3     cmp::{Eq, Ord, Ordering, PartialEq, PartialOrd},
     5     collections::BinaryHeap,
     4     collections::BinaryHeap,
     6 };
     5 };
     7 
     6 
     8 pub type EventId = u16;
     7 pub type EventId = u16;
     9 
     8 
    10 struct TimeEvent {
     9 struct TimeEvent {
    11     time: FPNum,
    10     time: Millis,
    12     gear_id: GearId,
    11     gear_id: GearId,
    13     event_id: EventId,
    12     event_id: EventId,
    14 }
    13 }
    15 
    14 
    16 impl PartialOrd for TimeEvent {
    15 impl PartialOrd for TimeEvent {
    50     }
    49     }
    51 }
    50 }
    52 
    51 
    53 pub struct TimeProcessor {
    52 pub struct TimeProcessor {
    54     current_event_id: EventId,
    53     current_event_id: EventId,
    55     current_time: FPNum,
    54     current_time: Millis,
    56     events: BinaryHeap<TimeEvent>,
    55     events: BinaryHeap<TimeEvent>,
    57     timeouts: OccurredEvents,
    56     timeouts: OccurredEvents,
    58 }
    57 }
    59 
    58 
    60 impl TimeProcessor {
    59 impl TimeProcessor {
    61     pub fn new() -> Self {
    60     pub fn new() -> Self {
    62         Self {
    61         Self {
    63             current_event_id: 0,
    62             current_event_id: 0,
    64             current_time: fp!(0),
    63             current_time: Millis::new(0),
    65             events: BinaryHeap::with_capacity(1024),
    64             events: BinaryHeap::with_capacity(1024),
    66             timeouts: OccurredEvents::new(),
    65             timeouts: OccurredEvents::new(),
    67         }
    66         }
    68     }
    67     }
    69 
    68 
    70     pub fn register(&mut self, gear_id: GearId, timeout: FPNum) -> EventId {
    69     pub fn register(&mut self, gear_id: GearId, timeout: Millis) -> EventId {
    71         let event_id = self.current_event_id;
    70         let event_id = self.current_event_id;
    72         self.current_event_id.wrapping_add(1);
    71         self.current_event_id.wrapping_add(1);
    73         let event = TimeEvent {
    72         let event = TimeEvent {
    74             time: self.current_time + timeout,
    73             time: self.current_time + timeout,
    75             gear_id,
    74             gear_id,
    79         event_id
    78         event_id
    80     }
    79     }
    81 
    80 
    82     pub fn cancel(&mut self, gear_id: GearId) {}
    81     pub fn cancel(&mut self, gear_id: GearId) {}
    83 
    82 
    84     pub fn process(&mut self, time_step: FPNum) -> &OccurredEvents {
    83     pub fn process(&mut self, time_step: Millis) -> &OccurredEvents {
    85         self.timeouts.clear();
    84         self.timeouts.clear();
    86         self.current_time += time_step;
    85         self.current_time = self.current_time + time_step;
    87         while self
    86         while self
    88             .events
    87             .events
    89             .peek()
    88             .peek()
    90             .filter(|e| e.time <= self.current_time)
    89             .filter(|e| e.time <= self.current_time)
    91             .is_some()
    90             .is_some()