rust/hwphysics/src/lib.rs
changeset 15401 6e3e5be8b2e2
parent 15374 35c331f9308e
child 15402 52844baced17
--- a/rust/hwphysics/src/lib.rs	Wed Aug 28 22:53:40 2019 +0300
+++ b/rust/hwphysics/src/lib.rs	Wed Aug 28 23:06:34 2019 +0300
@@ -10,41 +10,28 @@
 
 use crate::{
     collision::{CollisionData, CollisionProcessor, ContactData},
-    common::{GearAllocator, GearData, GearDataAggregator, GearDataProcessor, GearId, Millis},
-    physics::{PhysicsData, PhysicsProcessor},
+    common::{GearAllocator, GearId, Millis},
+    data::GearDataManager,
+    physics::PhysicsProcessor,
     time::TimeProcessor,
 };
 
-pub struct JoinedData {
-    gear_id: GearId,
-    physics: PhysicsData,
-    collision: CollisionData,
-    contact: ContactData,
-}
-
 pub struct World {
     allocator: GearAllocator,
+    data: GearDataManager,
     physics: PhysicsProcessor,
     collision: CollisionProcessor,
     time: TimeProcessor,
 }
 
-macro_rules! processor_map {
-    ( $data_type: ident => $field: ident ) => {
-        impl GearDataAggregator<$data_type> for World {
-            fn find_processor(&mut self) -> &mut GearDataProcessor<$data_type> {
-                &mut self.$field
-            }
-        }
-    };
-}
-
-processor_map!(PhysicsData => physics);
-processor_map!(CollisionData => collision);
-
 impl World {
     pub fn new(world_size: Size) -> Self {
+        let mut data = GearDataManager::new();
+        PhysicsProcessor::register_components(&mut data);
+        CollisionProcessor::register_components(&mut data);
+
         Self {
+            data,
             allocator: GearAllocator::new(),
             physics: PhysicsProcessor::new(),
             collision: CollisionProcessor::new(world_size),
@@ -59,24 +46,21 @@
 
     #[inline]
     pub fn delete_gear(&mut self, gear_id: GearId) {
-        self.physics.remove(gear_id);
+        self.data.remove_all(gear_id);
         self.collision.remove(gear_id);
         self.time.cancel(gear_id);
         self.allocator.free(gear_id)
     }
 
     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 updates = self.physics.process(&mut self.data, time_step);
+        let collisions = self.collision.process(land, &updates);
         let events = self.time.process(time_step);
     }
 
-    pub fn add_gear_data<T>(&mut self, gear_id: GearId, data: T)
-    where
-        T: GearData,
-        Self: GearDataAggregator<T>,
-    {
-        self.find_processor().add(gear_id, data);
+    #[inline]
+    pub fn add_gear_data<T: Clone + 'static>(&mut self, gear_id: GearId, data: &T) {
+        self.data.add(gear_id, data);
     }
 }
 
@@ -85,7 +69,7 @@
     use crate::{
         collision::{CircleBounds, CollisionData},
         common::Millis,
-        physics::PhysicsData,
+        physics::{PositionData, VelocityData},
         World,
     };
     use fpnum::{fp, FPNum, FPPoint};
@@ -97,19 +81,14 @@
         let world_size = Size::new(2048, 2048);
 
         let mut world = World::new(world_size);
-        let gear_id = std::num::NonZeroU16::new(46631).unwrap();
+        let gear_id = world.new_gear().unwrap();
+
+        world.add_gear_data(gear_id, &PositionData(FPPoint::zero()));
+        world.add_gear_data(gear_id, &VelocityData(FPPoint::unit_y()));
 
         world.add_gear_data(
             gear_id,
-            PhysicsData {
-                position: FPPoint::zero(),
-                velocity: FPPoint::unit_y(),
-            },
-        );
-
-        world.add_gear_data(
-            gear_id,
-            CollisionData {
+            &CollisionData {
                 bounds: CircleBounds {
                     center: FPPoint::zero(),
                     radius: fp!(10),