map.rs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. use rltk::{RandomNumberGenerator, RGB, Rltk, Algorithm2D, Point, BaseMap};
  2. use super::{Rect};
  3. use std::cmp::{max, min};
  4. use specs::prelude::*;
  5. #[derive(PartialEq, Copy, Clone)]
  6. pub enum TileType {
  7. Wall,
  8. Floor
  9. }
  10. pub struct Map {
  11. pub tiles: Vec<TileType>,
  12. pub rooms: Vec<Rect>,
  13. pub width: i32,
  14. pub height: i32,
  15. pub revealed_tiles : Vec<bool>,
  16. pub visible_tiles: Vec<bool>
  17. }
  18. impl Map {
  19. pub fn xy_idx(&self, x: i32, y: i32) -> usize {
  20. (y as usize * self.width as usize) + x as usize
  21. }
  22. fn apply_room_to_map(&mut self, room: &Rect) {
  23. for y in room.y1 +1 ..= room.y2 {
  24. for x in room.x1 + 1 ..= room.x2 {
  25. let idx = self.xy_idx(x,y);
  26. self.tiles[idx] = TileType::Floor;
  27. }
  28. }
  29. }
  30. fn apply_horizontal_tunnel(&mut self, x1:i32, x2:i32, y:i32) {
  31. for x in min(x1,x2) ..= max(x1,x2) {
  32. let idx = self.xy_idx(x, y);
  33. if idx > 0 && idx < self.width as usize * self.height as usize {
  34. self.tiles[idx as usize] = TileType::Floor;
  35. }
  36. }
  37. }
  38. fn apply_vertical_tunnel(&mut self, y1:i32, y2:i32, x:i32) {
  39. for y in min(y1,y2) ..= max(y1,y2) {
  40. let idx = self.xy_idx(x, y);
  41. if idx > 0 && idx < self.width as usize * self.height as usize {
  42. self.tiles[idx as usize] = TileType::Floor;
  43. }
  44. }
  45. }
  46. pub fn new_map_rooms_and_corridors() -> Map {
  47. let mut map = Map{
  48. tiles : vec![TileType::Wall; 80*50],
  49. rooms : Vec::new(),
  50. width : 80,
  51. height: 50,
  52. revealed_tiles : vec![false; 80*50],
  53. visible_tiles : vec![false; 80*50],
  54. };
  55. const MAX_ROOMS : i32 = 30;
  56. const MIN_SIZE : i32 = 6;
  57. const MAX_SIZE : i32 = 10;
  58. let mut rng = RandomNumberGenerator::new();
  59. for _ in 0..MAX_ROOMS {
  60. let w = rng.range(MIN_SIZE, MAX_SIZE);
  61. let h = rng.range(MIN_SIZE, MAX_SIZE);
  62. let x = rng.roll_dice(1, map.width - w - 1) - 1;
  63. let y = rng.roll_dice(1, map.height - h - 1) - 1;
  64. let new_room = Rect::new(x, y, w, h);
  65. let mut ok = true;
  66. for other_room in map.rooms.iter() {
  67. if new_room.intersect(other_room) { ok = false }
  68. }
  69. if ok {
  70. map.apply_room_to_map(&new_room);
  71. if !map.rooms.is_empty() {
  72. let (new_x, new_y) = new_room.center();
  73. let (prev_x, prev_y) = map.rooms[map.rooms.len()-1].center();
  74. if rng.range(0,2) == 1 {
  75. map.apply_horizontal_tunnel(prev_x, new_x, prev_y);
  76. map.apply_vertical_tunnel(prev_y, new_y, new_x);
  77. } else {
  78. map.apply_vertical_tunnel(prev_y, new_y, prev_x);
  79. map.apply_horizontal_tunnel(prev_x, new_x, new_y);
  80. }
  81. }
  82. map.rooms.push(new_room);
  83. }
  84. }
  85. map
  86. }
  87. }
  88. impl Algorithm2D for Map {
  89. fn dimensions(&self) -> Point {
  90. Point::new(self.width, self.height)
  91. }
  92. }
  93. impl BaseMap for Map {
  94. fn is_opaque(&self, idx:usize) -> bool {
  95. self.tiles[idx as usize] == TileType::Wall
  96. }
  97. }
  98. // /// Makes a map with solid boundaries and 400 randomly placed walls. No guarantees that it won't
  99. // /// look awful.
  100. // pub fn new_map_test() -> Vec<TileType> {
  101. // let mut map = vec![TileType::Floor; 80*50];
  102. // // Make the boundaries walls
  103. // for x in 0..80 {
  104. // map[xy_idx(x, 0)] = TileType::Wall;
  105. // map[xy_idx(x, 49)] = TileType::Wall;
  106. // }
  107. // for y in 0..50 {
  108. // map[xy_idx(0, y)] = TileType::Wall;
  109. // map[xy_idx(79, y)] = TileType::Wall;
  110. // }
  111. // // Now we'll randomly splat a bunch of walls. It won't be pretty, but it's a decent illustration.
  112. // // First, obtain the thread-local RNG:
  113. // let mut rng = rltk::RandomNumberGenerator::new();
  114. // for _i in 0..400 {
  115. // let x = rng.roll_dice(1, 79);
  116. // let y = rng.roll_dice(1, 49);
  117. // let idx = xy_idx(x, y);
  118. // if idx != xy_idx(40, 25) {
  119. // map[idx] = TileType::Wall;
  120. // }
  121. // }
  122. // return map
  123. // }
  124. pub fn draw_map(ecs: &World, ctx : &mut Rltk) {
  125. let map = ecs.fetch::<Map>();
  126. let mut y = 0;
  127. let mut x = 0;
  128. for (idx,tile) in map.tiles.iter().enumerate() {
  129. // Render a tile depending upon the tile type
  130. if map.revealed_tiles[idx] {
  131. let glyph;
  132. let mut fg;
  133. match tile {
  134. TileType::Floor => {
  135. glyph = rltk::to_cp437('.');
  136. fg = RGB::from_f32(0.0, 0.5, 0.5);
  137. }
  138. TileType::Wall => {
  139. glyph = rltk::to_cp437('#');
  140. fg = RGB::from_f32(0., 1.0, 0.);
  141. }
  142. }
  143. if !map.visible_tiles[idx] { fg = fg.to_greyscale() }
  144. ctx.set(x, y, fg, RGB::from_f32(0., 0., 0.), glyph);
  145. }
  146. // Move the coordinates
  147. x += 1;
  148. if x > 79 {
  149. x = 0;
  150. y += 1;
  151. }
  152. }
  153. }