replace time with milliseconds
authoralfadur
Thu, 25 Jul 2019 23:02:02 +0300
changeset 15296 66c987015f2d
parent 15295 42b710b0f883
child 15297 d111a0d9d499
replace time with milliseconds
rust/hwphysics/src/common.rs
rust/hwphysics/src/lib.rs
rust/hwphysics/src/physics.rs
rust/hwphysics/src/time.rs
rust/lib-hedgewars-engine/src/world.rs
--- a/rust/hwphysics/src/common.rs	Thu Jul 25 22:31:24 2019 +0300
+++ b/rust/hwphysics/src/common.rs	Thu Jul 25 23:02:02 2019 +0300
@@ -1,8 +1,37 @@
-use std::{collections::BinaryHeap, num::NonZeroU16};
+use fpnum::{fp, FPNum};
+use std::{collections::BinaryHeap, num::NonZeroU16, ops::Add};
 
 pub type GearId = NonZeroU16;
 pub trait GearData {}
 
+#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Debug)]
+pub struct Millis(u32);
+
+impl Millis {
+    #[inline]
+    pub fn new(value: u32) -> Self {
+        Self(value)
+    }
+
+    #[inline]
+    pub fn get(self) -> u32 {
+        self.0
+    }
+
+    #[inline]
+    pub fn to_fixed(self) -> FPNum {
+        FPNum::new(self.0 as i32, 1000)
+    }
+}
+
+impl Add for Millis {
+    type Output = Self;
+
+    fn add(self, rhs: Self) -> Self::Output {
+        Self(self.0 + rhs.0)
+    }
+}
+
 pub trait GearDataProcessor<T: GearData> {
     fn add(&mut self, gear_id: GearId, gear_data: T);
     fn remove(&mut self, gear_id: GearId);
--- a/rust/hwphysics/src/lib.rs	Thu Jul 25 22:31:24 2019 +0300
+++ b/rust/hwphysics/src/lib.rs	Thu Jul 25 23:02:02 2019 +0300
@@ -10,7 +10,7 @@
 
 use crate::{
     collision::{CollisionData, CollisionProcessor, ContactData},
-    common::{GearAllocator, GearData, GearDataAggregator, GearDataProcessor, GearId},
+    common::{GearAllocator, GearData, GearDataAggregator, GearDataProcessor, GearId, Millis},
     physics::{PhysicsData, PhysicsProcessor},
     time::TimeProcessor,
 };
@@ -65,7 +65,7 @@
         self.allocator.free(gear_id)
     }
 
-    pub fn step(&mut self, time_step: FPNum, land: &Land2D<u32>) {
+    pub fn step(&mut self, time_step: Millis, land: &Land2D<u32>) {
         let updates = self.physics.process(time_step);
         let collision = self.collision.process(land, &updates);
         let events = self.time.process(time_step);
--- a/rust/hwphysics/src/physics.rs	Thu Jul 25 22:31:24 2019 +0300
+++ b/rust/hwphysics/src/physics.rs	Thu Jul 25 23:02:02 2019 +0300
@@ -1,4 +1,4 @@
-use crate::common::{GearData, GearDataProcessor, GearId};
+use crate::common::{GearData, GearDataProcessor, GearId, Millis};
 use fpnum::*;
 use integral_geometry::{GridIndex, Point, Size};
 
@@ -133,11 +133,12 @@
         }
     }
 
-    pub fn process(&mut self, time_step: FPNum) -> &PositionUpdates {
+    pub fn process(&mut self, time_step: Millis) -> &PositionUpdates {
+        let fp_step = time_step.to_fixed();
         self.position_updates.clear();
         for (gear_id, (pos, vel)) in self.dynamic_physics.iter_pos_update() {
             let old_pos = *pos;
-            *pos += *vel * time_step;
+            *pos += *vel * fp_step;
             if !vel.is_zero() {
                 self.position_updates.push(gear_id, &old_pos, pos)
             } else {
--- a/rust/hwphysics/src/time.rs	Thu Jul 25 22:31:24 2019 +0300
+++ b/rust/hwphysics/src/time.rs	Thu Jul 25 23:02:02 2019 +0300
@@ -1,5 +1,4 @@
-use crate::common::{GearDataProcessor, GearId};
-use fpnum::{fp, FPNum};
+use crate::common::{GearDataProcessor, GearId, Millis};
 use std::{
     cmp::{Eq, Ord, Ordering, PartialEq, PartialOrd},
     collections::BinaryHeap,
@@ -8,7 +7,7 @@
 pub type EventId = u16;
 
 struct TimeEvent {
-    time: FPNum,
+    time: Millis,
     gear_id: GearId,
     event_id: EventId,
 }
@@ -52,7 +51,7 @@
 
 pub struct TimeProcessor {
     current_event_id: EventId,
-    current_time: FPNum,
+    current_time: Millis,
     events: BinaryHeap<TimeEvent>,
     timeouts: OccurredEvents,
 }
@@ -61,13 +60,13 @@
     pub fn new() -> Self {
         Self {
             current_event_id: 0,
-            current_time: fp!(0),
+            current_time: Millis::new(0),
             events: BinaryHeap::with_capacity(1024),
             timeouts: OccurredEvents::new(),
         }
     }
 
-    pub fn register(&mut self, gear_id: GearId, timeout: FPNum) -> EventId {
+    pub fn register(&mut self, gear_id: GearId, timeout: Millis) -> EventId {
         let event_id = self.current_event_id;
         self.current_event_id.wrapping_add(1);
         let event = TimeEvent {
@@ -81,9 +80,9 @@
 
     pub fn cancel(&mut self, gear_id: GearId) {}
 
-    pub fn process(&mut self, time_step: FPNum) -> &OccurredEvents {
+    pub fn process(&mut self, time_step: Millis) -> &OccurredEvents {
         self.timeouts.clear();
-        self.current_time += time_step;
+        self.current_time = self.current_time + time_step;
         while self
             .events
             .peek()
--- a/rust/lib-hedgewars-engine/src/world.rs	Thu Jul 25 22:31:24 2019 +0300
+++ b/rust/lib-hedgewars-engine/src/world.rs	Thu Jul 25 23:02:02 2019 +0300
@@ -1,5 +1,8 @@
 use fpnum::{fp, FPNum, FPPoint};
-use hwphysics::{self as hwp, common::GearId};
+use hwphysics::{
+    self as hwp,
+    common::{GearId, Millis},
+};
 use integral_geometry::{Point, Rect, Size};
 use land2d::Land2D;
 use landgen::{
@@ -150,7 +153,7 @@
         }
 
         if let Some(ref mut state) = self.game_state {
-            state.physics.step(fp!(1), &state.land);
+            state.physics.step(Millis::new(1), &state.land);
         }
     }
 }