add hwphysics to engine
authoralfadur
Mon, 26 Nov 2018 02:50:54 +0300
changeset 14300 fd8e0e52d5bd
parent 14299 21be7838a127
child 14301 0855344f54a7
add hwphysics to engine
rust/lib-hedgewars-engine/Cargo.toml
rust/lib-hedgewars-engine/src/lib.rs
rust/lib-hedgewars-engine/src/world.rs
--- a/rust/lib-hedgewars-engine/Cargo.toml	Sun Nov 25 22:32:49 2018 +0100
+++ b/rust/lib-hedgewars-engine/Cargo.toml	Mon Nov 26 02:50:54 2018 +0300
@@ -9,11 +9,13 @@
 gfx_device_gl = "0.15"
 netbuf = "0.4"
 
+fpnum = { path = "../fpnum" }
 land2d = { path = "../land2d" }
 lfprng = { path = "../lfprng" }
 integral-geometry = { path = "../integral-geometry" }
 landgen = { path = "../landgen" }
 hedgewars-engine-messages = { path = "../hedgewars-engine-messages" }
+hwphysics = { path = "../hwphysics" }
 
 [lib]
 name = "hedgewars_engine"
--- a/rust/lib-hedgewars-engine/src/lib.rs	Sun Nov 25 22:32:49 2018 +0100
+++ b/rust/lib-hedgewars-engine/src/lib.rs	Mon Nov 26 02:50:54 2018 +0300
@@ -112,7 +112,8 @@
 
 #[no_mangle]
 pub extern "C" fn advance_simulation(engine_state: &mut EngineInstance, ticks: u32) -> bool {
-    unimplemented!()
+    engine_state.world.step();
+    true
 }
 #[no_mangle]
 pub extern "C" fn cleanup(engine_state: *mut EngineInstance) {
--- a/rust/lib-hedgewars-engine/src/world.rs	Sun Nov 25 22:32:49 2018 +0100
+++ b/rust/lib-hedgewars-engine/src/world.rs	Mon Nov 26 02:50:54 2018 +0300
@@ -1,14 +1,33 @@
+use fpnum::{FPNum, fp};
 use integral_geometry::{Point, Rect, Size};
 use land2d::Land2D;
 use landgen::{
-    outline_template::OutlineTemplate, template_based::TemplatedLandGenerator,
-    LandGenerationParameters, LandGenerator,
+    outline_template::OutlineTemplate,
+    template_based::TemplatedLandGenerator,
+    LandGenerationParameters,
+    LandGenerator,
 };
 use lfprng::LaggedFibonacciPRNG;
+use hwphysics as hwp;
+
+struct GameState {
+    land: Land2D<u32>,
+    physics: hwp::World,
+}
+
+impl GameState {
+    fn new(land: Land2D<u32>, physics: hwp::World) -> Self {
+        Self {
+            land,
+            physics,
+        }
+    }
+}
 
 pub struct World {
     random_numbers_gen: LaggedFibonacciPRNG,
     preview: Land2D<u8>,
+    game_state: Option<GameState>,
 }
 
 impl World {
@@ -16,6 +35,7 @@
         Self {
             random_numbers_gen: LaggedFibonacciPRNG::new(&[]),
             preview: Land2D::new(Size::new(0, 0), 0),
+            game_state: None,
         }
     }
 
@@ -41,8 +61,24 @@
             template
         }
 
-        let params = LandGenerationParameters::new(0 as u8, 255, 5, false, false);
+        let params = LandGenerationParameters::new(0u8, u8::max_value(), 5, false, false);
         let landgen = TemplatedLandGenerator::new(template());
         self.preview = landgen.generate_land(&params, &mut self.random_numbers_gen);
     }
+
+    pub fn init(&mut self, template: OutlineTemplate) {
+        let physics = hwp::World::new(template.size);
+
+        let params = LandGenerationParameters::new(0u32, u32::max_value(), 5, false, false);
+        let landgen = TemplatedLandGenerator::new(template);
+        let land = landgen.generate_land(&params, &mut self.random_numbers_gen);
+
+        self.game_state = Some(GameState::new(land, physics));
+    }
+
+    pub fn step(&mut self) {
+        if let Some(ref mut state) = self.game_state {
+            state.physics.step(fp!(1), &state.land);
+        }
+    }
 }