move time out of physics
authoralfadur
Thu, 07 Jan 2021 17:13:32 +0300
changeset 15797 95402fa4e191
parent 15796 9588bd852202
child 15798 ec85fdf82942
move time out of physics
rust/hwphysics/src/lib.rs
rust/hwphysics/src/time.rs
rust/lib-hedgewars-engine/src/lib.rs
rust/lib-hedgewars-engine/src/time.rs
--- a/rust/hwphysics/src/lib.rs	Sun Dec 13 22:26:39 2020 +0100
+++ b/rust/hwphysics/src/lib.rs	Thu Jan 07 17:13:32 2021 +0300
@@ -3,7 +3,6 @@
 mod data;
 mod grid;
 pub mod physics;
-pub mod time;
 
 use integral_geometry::Size;
 use land2d::Land2D;
@@ -13,7 +12,6 @@
     common::{GearAllocator, GearId, Millis},
     data::{DataIterator, GearDataManager, TypeIter},
     physics::PhysicsProcessor,
-    time::TimeProcessor,
 };
 
 pub struct World {
@@ -21,7 +19,6 @@
     data: GearDataManager,
     physics: PhysicsProcessor,
     collision: CollisionProcessor,
-    time: TimeProcessor,
 }
 
 impl World {
@@ -35,7 +32,6 @@
             allocator: GearAllocator::new(),
             physics: PhysicsProcessor::new(),
             collision: CollisionProcessor::new(world_size),
-            time: TimeProcessor::new(),
         }
     }
 
@@ -48,7 +44,6 @@
     pub fn delete_gear(&mut self, gear_id: GearId) {
         self.data.remove_all(gear_id);
         self.collision.remove(gear_id);
-        self.time.cancel_all(gear_id);
         self.allocator.free(gear_id)
     }
 
@@ -60,7 +55,6 @@
                 .process_multiple_ticks(&mut self.data, time_step)
         };
         let collisions = self.collision.process(land, &updates);
-        let events = self.time.process(time_step);
     }
 
     #[inline]
--- a/rust/hwphysics/src/time.rs	Sun Dec 13 22:26:39 2020 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,113 +0,0 @@
-use crate::common::{GearId, Millis};
-use std::{
-    cmp::{Eq, Ord, Ordering, PartialEq, PartialOrd},
-    collections::BinaryHeap,
-};
-
-pub type EventId = u16;
-
-struct TimeEvent {
-    time: Millis,
-    gear_id: GearId,
-    event_id: EventId,
-}
-
-impl PartialOrd for TimeEvent {
-    #[inline]
-    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
-        self.time.partial_cmp(&other.time)
-    }
-}
-
-impl PartialEq for TimeEvent {
-    #[inline]
-    fn eq(&self, other: &Self) -> bool {
-        self.time.eq(&other.time)
-    }
-}
-
-impl Ord for TimeEvent {
-    #[inline]
-    fn cmp(&self, other: &Self) -> Ordering {
-        self.time.cmp(&other.time)
-    }
-}
-
-impl Eq for TimeEvent {}
-
-pub struct OccurredEvents {
-    events: Vec<(GearId, EventId)>,
-}
-
-impl OccurredEvents {
-    fn new() -> Self {
-        Self { events: vec![] }
-    }
-
-    fn clear(&mut self) {
-        self.events.clear()
-    }
-}
-
-pub struct TimeProcessor {
-    current_event_id: EventId,
-    current_time: Millis,
-    events: BinaryHeap<TimeEvent>,
-    timeouts: OccurredEvents,
-}
-
-impl TimeProcessor {
-    pub fn new() -> Self {
-        Self {
-            current_event_id: 0,
-            current_time: Millis::new(0),
-            events: BinaryHeap::with_capacity(1024),
-            timeouts: OccurredEvents::new(),
-        }
-    }
-
-    pub fn register(&mut self, gear_id: GearId, timeout: Millis) -> EventId {
-        let event_id = self.current_event_id;
-        self.current_event_id = self.current_event_id.wrapping_add(1);
-        let event = TimeEvent {
-            time: self.current_time + timeout,
-            gear_id,
-            event_id,
-        };
-        self.events.push(event);
-        event_id
-    }
-
-    fn remove_events<P>(&mut self, predicate: P)
-    where
-        P: Fn(&TimeEvent) -> bool,
-    {
-        let events = self.events.drain().filter(predicate).collect::<Vec<_>>();
-        self.events.extend(events);
-    }
-
-    pub fn cancel(&mut self, event_id: EventId) {
-        //self.events.retain(|event| event.event_id != event_id)
-        self.remove_events(|event| event.event_id != event_id)
-    }
-
-    pub fn cancel_all(&mut self, gear_id: GearId) {
-        //self.events.retain(|event| event.gear_id != gear_id)
-        self.remove_events(|event| event.gear_id != gear_id)
-    }
-
-    pub fn process(&mut self, time_step: Millis) -> &OccurredEvents {
-        self.timeouts.clear();
-        self.current_time = self.current_time + time_step;
-        while self
-            .events
-            .peek()
-            .filter(|e| e.time <= self.current_time)
-            .is_some()
-        {
-            let event = self.events.pop().unwrap();
-            self.timeouts.events.push((event.gear_id, event.event_id))
-        }
-        &self.timeouts
-    }
-}
--- a/rust/lib-hedgewars-engine/src/lib.rs	Sun Dec 13 22:26:39 2020 +0100
+++ b/rust/lib-hedgewars-engine/src/lib.rs	Thu Jan 07 17:13:32 2021 +0300
@@ -1,6 +1,7 @@
 pub mod instance;
 pub mod ipc;
 mod render;
+mod time;
 mod world;
 
 use std::{
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rust/lib-hedgewars-engine/src/time.rs	Thu Jan 07 17:13:32 2021 +0300
@@ -0,0 +1,113 @@
+use std::{
+    cmp::{Eq, Ord, Ordering, PartialEq, PartialOrd},
+    collections::BinaryHeap,
+};
+use hwphysics::common::{GearId, Millis};
+
+pub type EventId = u16;
+
+struct TimeEvent {
+    time: Millis,
+    gear_id: GearId,
+    event_id: EventId,
+}
+
+impl PartialOrd for TimeEvent {
+    #[inline]
+    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
+        self.time.partial_cmp(&other.time)
+    }
+}
+
+impl PartialEq for TimeEvent {
+    #[inline]
+    fn eq(&self, other: &Self) -> bool {
+        self.time.eq(&other.time)
+    }
+}
+
+impl Ord for TimeEvent {
+    #[inline]
+    fn cmp(&self, other: &Self) -> Ordering {
+        self.time.cmp(&other.time)
+    }
+}
+
+impl Eq for TimeEvent {}
+
+pub struct OccurredEvents {
+    events: Vec<(GearId, EventId)>,
+}
+
+impl OccurredEvents {
+    fn new() -> Self {
+        Self { events: vec![] }
+    }
+
+    fn clear(&mut self) {
+        self.events.clear()
+    }
+}
+
+pub struct TimeProcessor {
+    current_event_id: EventId,
+    current_time: Millis,
+    events: BinaryHeap<TimeEvent>,
+    timeouts: OccurredEvents,
+}
+
+impl TimeProcessor {
+    pub fn new() -> Self {
+        Self {
+            current_event_id: 0,
+            current_time: Millis::new(0),
+            events: BinaryHeap::with_capacity(1024),
+            timeouts: OccurredEvents::new(),
+        }
+    }
+
+    pub fn register(&mut self, gear_id: GearId, timeout: Millis) -> EventId {
+        let event_id = self.current_event_id;
+        self.current_event_id = self.current_event_id.wrapping_add(1);
+        let event = TimeEvent {
+            time: self.current_time + timeout,
+            gear_id,
+            event_id,
+        };
+        self.events.push(event);
+        event_id
+    }
+
+    fn retain_events<P>(&mut self, predicate: P)
+    where
+        P: Fn(&TimeEvent) -> bool,
+    {
+        let events = self.events.drain().filter(predicate).collect::<Vec<_>>();
+        self.events.extend(events);
+    }
+
+    pub fn cancel(&mut self, event_id: EventId) {
+        //self.events.retain(|event| event.event_id != event_id)
+        self.retain_events(|event| event.event_id != event_id)
+    }
+
+    pub fn cancel_all(&mut self, gear_id: GearId) {
+        //self.events.retain(|event| event.gear_id != gear_id)
+        self.retain_events(|event| event.gear_id != gear_id)
+    }
+
+    pub fn process(&mut self, time_step: Millis) -> &OccurredEvents {
+        self.timeouts.clear();
+        self.current_time = self.current_time + time_step;
+        while self
+            .events
+            .peek()
+            .filter(|e| e.time <= self.current_time)
+            .is_some()
+        {
+            let event = self.events.pop().unwrap();
+            self.timeouts.events.push((event.gear_id, event.event_id))
+        }
+        &self.timeouts
+    }
+}