Browse Source

Chapter 4 is WASM/0.3.0 friendly.

Herbert Wolverson 4 years ago
parent
commit
aff5204ddf
5 changed files with 20 additions and 10 deletions
  1. 2 0
      Cargo.lock
  2. 1 3
      book/src/chapter_4.md
  3. 4 0
      chapter-04-newmap/Cargo.toml
  4. 12 7
      chapter-04-newmap/src/main.rs
  5. 1 0
      wasmbuild.bat

+ 2 - 0
Cargo.lock

@@ -188,6 +188,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]]

+ 1 - 3
book/src/chapter_4.md

@@ -231,9 +231,7 @@ Our `main.rs` file also required adjustment, to get accept the new format. We ch
 fn main() {
     let context = Rltk::init_simple8x8(80, 50, "Hello Rust World", "../resources");
     let mut gs = State {
-        ecs: World::new(),
-        systems : DispatcherBuilder::new()
-            .build()
+        ecs: World::new()
     };
     gs.ecs.register::<Position>();
     gs.ecs.register::<Renderable>();

+ 4 - 0
chapter-04-newmap/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"

+ 12 - 7
chapter-04-newmap/src/main.rs

@@ -11,9 +11,16 @@ use player::*;
 mod rect;
 pub use rect::Rect;
 
+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) {
+        self.ecs.maintain();
+    }
 }
 
 impl GameState for State {
@@ -21,7 +28,7 @@ impl GameState for State {
         ctx.cls();
 
         player_input(self, ctx);
-        self.systems.dispatch(&self.ecs);
+        self.run_systems();
 
         let map = self.ecs.fetch::<Vec<TileType>>();
         draw_map(&map, ctx);
@@ -36,11 +43,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()
-            .build()
+        ecs: World::new()
     };
     gs.ecs.register::<Position>();
     gs.ecs.register::<Renderable>();

+ 1 - 0
wasmbuild.bat

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