# HG changeset patch # User alfadur # Date 1564084922 -10800 # Node ID 66c987015f2d7f23f734ab7d96274422f535b875 # Parent 42b710b0f883d76c597de5a286ec361db660580a replace time with milliseconds diff -r 42b710b0f883 -r 66c987015f2d rust/hwphysics/src/common.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 { fn add(&mut self, gear_id: GearId, gear_data: T); fn remove(&mut self, gear_id: GearId); diff -r 42b710b0f883 -r 66c987015f2d rust/hwphysics/src/lib.rs --- 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) { + pub fn step(&mut self, time_step: Millis, land: &Land2D) { let updates = self.physics.process(time_step); let collision = self.collision.process(land, &updates); let events = self.time.process(time_step); diff -r 42b710b0f883 -r 66c987015f2d rust/hwphysics/src/physics.rs --- 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 { diff -r 42b710b0f883 -r 66c987015f2d rust/hwphysics/src/time.rs --- 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, 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() diff -r 42b710b0f883 -r 66c987015f2d rust/lib-hedgewars-engine/src/world.rs --- 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); } } }