/ src / board / mod.rs
mod.rs
 1  use generator::generate_map;
 2  use mapgen::MapBuffer;
 3  
 4  use specs::{DispatcherBuilder, World};
 5  use tile::Tile;
 6  
 7  use crate::flow::{GameFlow, GameState};
 8  
 9  use self::tile::Biome;
10  
11  pub mod generator;
12  pub mod tile;
13  pub mod view;
14  
15  #[derive(Clone)]
16  pub struct WorldTileMap {
17      pub board: Vec<Vec<Tile>>,
18      pub biome: Biome,
19      pub height: usize,
20      pub width: usize,
21  }
22  
23  impl WorldTileMap {
24      fn new_empty(width: usize, height: usize) -> Self {
25          Self {
26              board: vec![vec![tile::Tile::Wall; width]; height],
27              biome: tile::Biome::Castle,
28              height,
29              width,
30          }
31      }
32  
33      pub fn set_biome(&mut self, biome: Biome) {
34          self.biome = biome
35      }
36  
37      pub fn set_map(&mut self, map: &MapBuffer) {
38          for y in 0..BOARD_HEIGHT {
39              for x in 0..BOARD_WIDTH {
40                  let tile = if map.is_walkable(x, y) {
41                      tile::Tile::Ground
42                  } else {
43                      tile::Tile::Wall
44                  };
45  
46                  self.board[y][x] = tile;
47              }
48          }
49      }
50  }
51  
52  impl Default for WorldTileMap {
53      fn default() -> Self {
54          Self::new_empty(BOARD_WIDTH, BOARD_HEIGHT)
55      }
56  }
57  
58  const BOARD_HEIGHT: usize = 50;
59  const BOARD_WIDTH: usize = 80;
60  
61  struct MapGenerationSystem;
62  
63  impl<'a> specs::System<'a> for MapGenerationSystem {
64      type SystemData = (specs::Read<'a, GameFlow>, specs::Write<'a, WorldTileMap>);
65  
66      fn run(&mut self, (game_flow, mut tile_map): Self::SystemData) {
67          let GameState::Started = game_flow.state else {
68              return;
69          };
70  
71          let map: mapgen::MapBuffer = generate_map();
72          tile_map.set_map(&map);
73          tile_map.set_biome(tile::Biome::Castle);
74      }
75  }
76  
77  pub fn register(dispatcher: &mut DispatcherBuilder, world: &mut World) -> anyhow::Result<()> {
78      let world_tile_map = WorldTileMap::new_empty(BOARD_WIDTH, BOARD_HEIGHT);
79      world.insert(world_tile_map);
80  
81      dispatcher.add(MapGenerationSystem, "map_generation_system", &[]);
82  
83      Ok(())
84  }