Browse Source

Update chapter 3 to 0.3.0 and WASM.

Herbert Wolverson 4 years ago
parent
commit
4a39d855aa
5 changed files with 31 additions and 14 deletions
  1. 2 0
      Cargo.lock
  2. 12 7
      book/src/chapter_3.md
  3. 4 0
      chapter-03-walkmap/Cargo.toml
  4. 12 7
      chapter-03-walkmap/src/main.rs
  5. 1 0
      wasmbuild.bat

+ 2 - 0
Cargo.lock

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

+ 12 - 7
book/src/chapter_3.md

@@ -187,6 +187,8 @@ use specs::prelude::*;
 #[macro_use]
 extern crate specs_derive;
 
+rltk::add_wasm_support!();
+
 #[derive(Component)]
 struct Position {
     x: i32,
@@ -209,8 +211,7 @@ enum TileType {
 }
 
 struct State {
-    ecs: World,
-    systems: Dispatcher<'static, 'static>
+    ecs: World
 }
 
 pub fn xy_idx(x: i32, y: i32) -> usize {
@@ -307,7 +308,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);
@@ -321,12 +322,16 @@ impl GameState for State {
     }
 }
 
+impl State {
+    fn run_systems(&mut self) {
+        self.ecs.maintain();
+    }
+}
+
 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>();

+ 4 - 0
chapter-03-walkmap/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-03-walkmap/src/main.rs

@@ -3,6 +3,8 @@ use specs::prelude::*;
 #[macro_use]
 extern crate specs_derive;
 
+rltk::add_wasm_support!();
+
 #[derive(Component)]
 struct Position {
     x: i32,
@@ -25,8 +27,7 @@ enum TileType {
 }
 
 struct State {
-    ecs: World,
-    systems: Dispatcher<'static, 'static>
+    ecs: World
 }
 
 pub fn xy_idx(x: i32, y: i32) -> usize {
@@ -123,7 +124,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);
@@ -137,12 +138,16 @@ impl GameState for State {
     }
 }
 
+impl State {
+    fn run_systems(&mut self) {
+        self.ecs.maintain();
+    }
+}
+
 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

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