map.rs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. use rand::Rng;
  2. use specs::prelude::*;
  3. #[derive(PartialEq, Clone, Copy, Debug)]
  4. pub struct Cell {
  5. pub tile: TileType,
  6. pub seen: bool,
  7. }
  8. impl Cell {
  9. pub fn glyph(&self) -> carpet::CP437 {
  10. match self.tile {
  11. TileType::Wall => carpet::CP437::from_char('#'),
  12. TileType::Floor => carpet::CP437::from_u8(0),
  13. }
  14. }
  15. pub fn blocks_view(&self) -> bool {
  16. match self.tile {
  17. TileType::Wall => true,
  18. TileType::Floor => false,
  19. }
  20. }
  21. }
  22. #[derive(PartialEq, Clone, Copy, Debug)]
  23. pub enum TileType {
  24. Wall,
  25. Floor,
  26. }
  27. pub struct Map {
  28. pub tiles: carpet::Board<Cell>,
  29. pub rooms: Vec<carpet::Rect>,
  30. }
  31. impl Map {
  32. pub fn new() -> Map {
  33. let mut rng = rand::thread_rng();
  34. let mut map = Map {
  35. tiles: carpet::Board::new_with_default(80, 50, Cell { tile: TileType::Wall, seen: false }),
  36. rooms: Vec::new(),
  37. };
  38. const MAX_ROOMS: usize = 30;
  39. const MIN_SIZE: usize = 6;
  40. const MAX_SIZE: usize = 10;
  41. for _ in 0..MAX_ROOMS {
  42. let w = rng.gen_range(MIN_SIZE..MAX_SIZE);
  43. let h = rng.gen_range(MIN_SIZE..MAX_SIZE);
  44. let x = rng.gen_range(1..80 - w);
  45. let y = rng.gen_range(1..50 - h);
  46. let room = carpet::Rect::new([x, y], [w, h]);
  47. if map.rooms.iter().any(|r| r.overlaps(room)) {
  48. continue;
  49. }
  50. map.carve(room);
  51. if let Some(prev) = map.rooms.first() {
  52. let c1 = room.center();
  53. let c2 = prev.center();
  54. let join = if rng.gen() {
  55. carpet::Coord { x: c1.x, y: c2.y }
  56. } else {
  57. carpet::Coord { x: c2.x, y: c1.y }
  58. };
  59. map.carve(carpet::Rect::from_points(c1, join));
  60. map.carve(carpet::Rect::from_points(join, c2));
  61. }
  62. map.rooms.push(room);
  63. }
  64. map
  65. }
  66. fn carve(&mut self, rect: carpet::Rect) {
  67. let iter = self
  68. .tiles
  69. .window_iter_mut(rect)
  70. .unwrap_or_else(|| panic!("Rect {:?} of map bounds", rect));
  71. for (_, _, t) in iter {
  72. t.tile = TileType::Floor;
  73. }
  74. }
  75. pub fn passable(&self, (x, y): (usize, usize)) -> bool {
  76. if let Some(cell) = self.tiles.get(x, y) {
  77. return cell.tile == TileType::Floor;
  78. }
  79. false
  80. }
  81. }
  82. #[derive(Component)]
  83. pub struct Viewshed {
  84. pub vs: carpet::Viewshed<Cell>,
  85. pub dirty: bool,
  86. }