rust/hwphysics/src/lib.rs
changeset 15295 42b710b0f883
parent 15291 7446258fab98
child 15296 66c987015f2d
--- a/rust/hwphysics/src/lib.rs	Thu Jul 25 21:59:20 2019 +0300
+++ b/rust/hwphysics/src/lib.rs	Thu Jul 25 22:31:24 2019 +0300
@@ -10,7 +10,7 @@
 
 use crate::{
     collision::{CollisionData, CollisionProcessor, ContactData},
-    common::{GearData, GearDataAggregator, GearDataProcessor, GearId},
+    common::{GearAllocator, GearData, GearDataAggregator, GearDataProcessor, GearId},
     physics::{PhysicsData, PhysicsProcessor},
     time::TimeProcessor,
 };
@@ -23,6 +23,7 @@
 }
 
 pub struct World {
+    allocator: GearAllocator,
     physics: PhysicsProcessor,
     collision: CollisionProcessor,
     time: TimeProcessor,
@@ -44,12 +45,26 @@
 impl World {
     pub fn new(world_size: Size) -> Self {
         Self {
+            allocator: GearAllocator::new(),
             physics: PhysicsProcessor::new(),
             collision: CollisionProcessor::new(world_size),
             time: TimeProcessor::new(),
         }
     }
 
+    #[inline]
+    pub fn new_gear(&mut self) -> Option<GearId> {
+        self.allocator.alloc()
+    }
+
+    #[inline]
+    pub fn delete_gear(&mut self, gear_id: GearId) {
+        self.physics.remove(gear_id);
+        self.collision.remove(gear_id);
+        self.time.cancel(gear_id);
+        self.allocator.free(gear_id)
+    }
+
     pub fn step(&mut self, time_step: FPNum, land: &Land2D<u32>) {
         let updates = self.physics.process(time_step);
         let collision = self.collision.process(land, &updates);