Browse Source

Chapter 9 is now WASM friendly.

Herbert Wolverson 4 years ago
parent
commit
a14550867f
7 changed files with 48 additions and 22 deletions
  1. 2 0
      Cargo.lock
  2. 3 0
      Cargo.toml
  3. 8 5
      book/src/chapter_9.md
  4. 0 1
      chapter-08-ui/Cargo.toml
  5. 4 0
      chapter-09-items/Cargo.toml
  6. 30 16
      chapter-09-items/src/main.rs
  7. 1 0
      wasmbuild.bat

+ 2 - 0
Cargo.lock

@@ -243,6 +243,8 @@ dependencies = [
  "rltk 0.3.1 (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]]

+ 3 - 0
Cargo.toml

@@ -38,3 +38,6 @@ members = [
     "chapter-20-magicmapping",
     "chapter-21-rexmenu"
     ]
+
+[profile.dev]
+opt-level = 1

+ 8 - 5
book/src/chapter_9.md

@@ -326,7 +326,8 @@ impl<'a> System<'a> for ItemCollectionSystem {
 This iterates the requests to pick up an item, removes their position component, and adds an `InBackpack` component assigned to the collector. Don't forget to add it to the systems list in `main.rs`:
 
 ```rust
-.with(ItemCollectionSystem{}, "pickup", &["melee_combat"])
+let mut pickup = ItemCollectionSystem{};
+pickup.run_now(&self.ecs);
 ```
 
 The next step is to add an input command to pick up an item. `g` is a popular key for this, so we'll go with that (we can always change it!). In `player.rs`, in the ever-growing `match` statement of inputs, we add:
@@ -551,7 +552,8 @@ impl<'a> System<'a> for PotionUseSystem {
 
 And register it in the list of systems to run:
 ```rust
-.with(PotionUseSystem{}, "potions", &["melee_combat"])
+let mut potions = PotionUseSystem{};
+potions.run_now(&self.ecs);
 ```
 
 Like other systems we've looked at, this iterates all of the `WantsToDrinkPotion` intent objects. It then heals up the drinker by the amount set in the `Potion` component, and deletes the potion. Since all of the placement information is attached to the potion itself, there's no need to chase around making sure it is removed from the appropriate backpack: the entity ceases to exist, and takes its components with it.
@@ -560,12 +562,12 @@ Testing this with `cargo run` gives a surprise: the potion isn't deleted after u
 
 ```rust
 RunState::PlayerTurn => {
-    self.systems.dispatch(&self.ecs);
+    self.run_systems();
     self.ecs.maintain();
     newrunstate = RunState::MonsterTurn;
 }
 RunState::MonsterTurn => {
-    self.systems.dispatch(&self.ecs);
+    self.run_systems();
     self.ecs.maintain();
     newrunstate = RunState::AwaitingInput;
 }
@@ -627,7 +629,8 @@ impl<'a> System<'a> for ItemDropSystem {
 
 Register it in the dispatch builder in `main.rs`:
 ```rust
-.with(ItemDropSystem{}, "drop_items", &["melee_combat"])
+let mut drop_items = ItemDropSystem{};
+drop_items.run_now(&self.ecs);
 ```
 
 We'll add a new `RunState` in `main.rs`:

+ 0 - 1
chapter-08-ui/Cargo.toml

@@ -11,7 +11,6 @@ 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"

+ 4 - 0
chapter-09-items/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"

+ 30 - 16
chapter-09-items/src/main.rs

@@ -28,12 +28,36 @@ mod spawner;
 mod inventory_system;
 use inventory_system::{ ItemCollectionSystem, PotionUseSystem, ItemDropSystem };
 
+rltk::add_wasm_support!();
+
 #[derive(PartialEq, Copy, Clone)]
 pub enum RunState { AwaitingInput, PreRun, PlayerTurn, MonsterTurn, ShowInventory, ShowDropItem }
 
 pub struct State {
-    pub ecs: World,
-    pub systems: Dispatcher<'static, 'static>
+    pub ecs: World
+}
+
+impl State {
+    fn run_systems(&mut self) {
+        let mut mapindex = MapIndexingSystem{};
+        mapindex.run_now(&self.ecs);
+        let mut vis = VisibilitySystem{};
+        vis.run_now(&self.ecs);
+        let mut mob = MonsterAI{};
+        mob.run_now(&self.ecs);
+        let mut melee = MeleeCombatSystem{};
+        melee.run_now(&self.ecs);
+        let mut damage = DamageSystem{};
+        damage.run_now(&self.ecs);
+        let mut pickup = ItemCollectionSystem{};
+        pickup.run_now(&self.ecs);
+        let mut potions = PotionUseSystem{};
+        potions.run_now(&self.ecs);
+        let mut drop_items = ItemDropSystem{};
+        drop_items.run_now(&self.ecs);
+
+        self.ecs.maintain();
+    }
 }
 
 impl GameState for State {
@@ -65,7 +89,7 @@ impl GameState for State {
         
         match newrunstate {
             RunState::PreRun => {
-                self.systems.dispatch(&self.ecs);
+                self.run_systems();
                 self.ecs.maintain();
                 newrunstate = RunState::AwaitingInput;
             }
@@ -73,12 +97,12 @@ impl GameState for State {
                 newrunstate = player_input(self, ctx);
             }
             RunState::PlayerTurn => {
-                self.systems.dispatch(&self.ecs);
+                self.run_systems();
                 self.ecs.maintain();
                 newrunstate = RunState::MonsterTurn;
             }
             RunState::MonsterTurn => {
-                self.systems.dispatch(&self.ecs);
+                self.run_systems();
                 self.ecs.maintain();
                 newrunstate = RunState::AwaitingInput;
             }
@@ -119,20 +143,10 @@ impl GameState for State {
 }
 
 fn main() {
-    let mut context = Rltk::init_simple8x8(80, 50, "Hello Rust World", "../resources");
+    let mut context = Rltk::init_simple8x8(80, 50, "Hello Rust World", "resources");
     context.with_post_scanlines(true);
     let mut gs = State {
         ecs: World::new(),
-        systems : DispatcherBuilder::new()
-            .with(MapIndexingSystem{}, "map_indexing_system", &[])
-            .with(VisibilitySystem{}, "visibility_system", &[])
-            .with(MonsterAI{}, "monster_ai", &["visibility_system", "map_indexing_system"])
-            .with(MeleeCombatSystem{}, "melee_combat", &["monster_ai"])
-            .with(DamageSystem{}, "damage", &["melee_combat"])
-            .with(ItemCollectionSystem{}, "pickup", &["melee_combat"])
-            .with(PotionUseSystem{}, "potions", &["melee_combat"])
-            .with(ItemDropSystem{}, "drop_items", &["melee_combat"])
-            .build(),
     };
     gs.ecs.register::<Position>();
     gs.ecs.register::<Renderable>();

+ 1 - 0
wasmbuild.bat

@@ -8,6 +8,7 @@ CALL :Stage chapter-05-fov
 CALL :Stage chapter-06-monsters
 CALL :Stage chapter-07-damage
 CALL :Stage chapter-08-ui
+CALL :Stage chapter-09-items
 
 REM Publish or perish
 cd book\book\wasm