Browse Source

Chapter 5 is now 0.3.0/WASM friendly.

Herbert Wolverson 4 years ago
parent
commit
d97e8bfcd0
5 changed files with 29 additions and 15 deletions
  1. 2 0
      Cargo.lock
  2. 8 7
      book/src/chapter_5.md
  3. 4 0
      chapter-05-fov/Cargo.toml
  4. 14 8
      chapter-05-fov/src/main.rs
  5. 1 0
      wasmbuild.bat

+ 2 - 0
Cargo.lock

@@ -199,6 +199,8 @@ dependencies = [
  "rltk 0.3.0 (git+https://github.com/thebracket/rltk_rs)",
  "specs 0.15.1 (registry+https://github.com/rust-lang/crates.io-index)",
  "specs-derive 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "wasm-bindgen 0.2.50 (registry+https://github.com/rust-lang/crates.io-index)",
+ "web-sys 0.3.27 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
 [[package]]

+ 8 - 7
book/src/chapter_5.md

@@ -178,15 +178,16 @@ impl<'a> System<'a> for VisibilitySystem {
 }
 ```
 
-We adjust the systems registration in `main.rs`:
+Now we have to adjust `run_systems` in `main.rs` to actually call the system:
 
 ```rust
-let mut gs = State {
-    ecs: World::new(),
-    systems : DispatcherBuilder::new()
-        .with(VisibilitySystem{}, "visibility_system", &[])
-        .build()
-};
+impl State {
+    fn run_systems(&mut self) {
+        let mut vis = VisibilitySystem{};
+        vis.run_now(&self.ecs);
+        self.ecs.maintain();
+    }
+}
 ```
 
 We also have to tell `main.rs` to use the new module:

+ 4 - 0
chapter-05-fov/Cargo.toml

@@ -10,3 +10,7 @@ edition = "2018"
 rltk = { git = "https://github.com/thebracket/rltk_rs" }
 specs = "0.15.0"
 specs-derive = "0.4.0"
+
+[target.'cfg(any(target_arch = "wasm32"))'.dependencies]
+web-sys = { version = "0.3", features=["console"] }
+wasm-bindgen = "0.2"

+ 14 - 8
chapter-05-fov/src/main.rs

@@ -15,9 +15,18 @@ pub use rect::Rect;
 mod visibility_system;
 use visibility_system::VisibilitySystem;
 
+rltk::add_wasm_support!();
+
 pub struct State {
-    pub ecs: World,
-    pub systems: Dispatcher<'static, 'static>
+    pub ecs: World
+}
+
+impl State {
+    fn run_systems(&mut self) {
+        let mut vis = VisibilitySystem{};
+        vis.run_now(&self.ecs);
+        self.ecs.maintain();
+    }
 }
 
 impl GameState for State {
@@ -25,7 +34,7 @@ impl GameState for State {
         ctx.cls();
 
         player_input(self, ctx);
-        self.systems.dispatch(&self.ecs);
+        self.run_systems();
 
         draw_map(&self.ecs, ctx);
 
@@ -39,12 +48,9 @@ impl GameState for State {
 }
 
 fn main() {
-    let context = Rltk::init_simple8x8(80, 50, "Hello Rust World", "../resources");
+    let context = Rltk::init_simple8x8(80, 50, "Hello Rust World", "resources");
     let mut gs = State {
-        ecs: World::new(),
-        systems : DispatcherBuilder::new()
-            .with(VisibilitySystem{}, "visibility_system", &[])
-            .build()
+        ecs: World::new()
     };
     gs.ecs.register::<Position>();
     gs.ecs.register::<Renderable>();

+ 1 - 0
wasmbuild.bat

@@ -4,6 +4,7 @@ CALL :Stage chapter-01-hellorust
 CALL :Stage chapter-02-helloecs
 CALL :Stage chapter-03-walkmap
 CALL :Stage chapter-04-newmap
+CALL :Stage chapter-05-fov
 
 REM Publish or perish
 cd book\book\wasm