map.rs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. extern crate rltk;
  2. use rltk::{ RGB, Rltk, Console, RandomNumberGenerator, BaseMap, Algorithm2D, Point };
  3. use super::{Rect};
  4. use std::cmp::{max, min};
  5. extern crate specs;
  6. use specs::prelude::*;
  7. #[derive(PartialEq, Copy, Clone)]
  8. pub enum TileType {
  9. Wall, Floor
  10. }
  11. #[derive(Default)]
  12. pub struct Map {
  13. pub tiles : Vec<TileType>,
  14. pub rooms : Vec<Rect>,
  15. pub width : i32,
  16. pub height : i32,
  17. pub revealed_tiles : Vec<bool>,
  18. pub visible_tiles : Vec<bool>
  19. }
  20. impl Map {
  21. pub fn xy_idx(&self, x: i32, y: i32) -> usize {
  22. (y as usize * self.width as usize) + x as usize
  23. }
  24. fn apply_room_to_map(&mut self, room : &Rect) {
  25. for y in room.y1 +1 ..= room.y2 {
  26. for x in room.x1 + 1 ..= room.x2 {
  27. let idx = self.xy_idx(x, y);
  28. self.tiles[idx] = TileType::Floor;
  29. }
  30. }
  31. }
  32. fn apply_horizontal_tunnel(&mut self, x1:i32, x2:i32, y:i32) {
  33. for x in min(x1,x2) ..= max(x1,x2) {
  34. let idx = self.xy_idx(x, y);
  35. if idx > 0 && idx < self.width as usize * self.height as usize {
  36. self.tiles[idx as usize] = TileType::Floor;
  37. }
  38. }
  39. }
  40. fn apply_vertical_tunnel(&mut self, y1:i32, y2:i32, x:i32) {
  41. for y in min(y1,y2) ..= max(y1,y2) {
  42. let idx = self.xy_idx(x, y);
  43. if idx > 0 && idx < self.width as usize * self.height as usize {
  44. self.tiles[idx as usize] = TileType::Floor;
  45. }
  46. }
  47. }
  48. /// Makes a new map using the algorithm from http://rogueliketutorials.com/tutorials/tcod/part-3/
  49. /// This gives a handful of random rooms and corridors joining them together.
  50. pub fn new_map_rooms_and_corridors() -> Map {
  51. let mut map = Map{
  52. tiles : vec![TileType::Wall; 80*50],
  53. rooms : Vec::new(),
  54. width : 80,
  55. height: 50,
  56. revealed_tiles : vec![false; 80*50],
  57. visible_tiles : vec![false; 80*50]
  58. };
  59. const MAX_ROOMS : i32 = 30;
  60. const MIN_SIZE : i32 = 6;
  61. const MAX_SIZE : i32 = 10;
  62. let mut rng = RandomNumberGenerator::new();
  63. for _i in 0..MAX_ROOMS {
  64. let w = rng.range(MIN_SIZE, MAX_SIZE);
  65. let h = rng.range(MIN_SIZE, MAX_SIZE);
  66. let x = rng.roll_dice(1, map.width - w - 1) - 1;
  67. let y = rng.roll_dice(1, map.height - h - 1) - 1;
  68. let new_room = Rect::new(x, y, w, h);
  69. let mut ok = true;
  70. for other_room in map.rooms.iter() {
  71. if new_room.intersect(other_room) { ok = false }
  72. }
  73. if ok {
  74. map.apply_room_to_map(&new_room);
  75. if !map.rooms.is_empty() {
  76. let (new_x, new_y) = new_room.center();
  77. let (prev_x, prev_y) = map.rooms[map.rooms.len()-1].center();
  78. if rng.range(0,1) == 1 {
  79. map.apply_horizontal_tunnel(prev_x, new_x, prev_y);
  80. map.apply_vertical_tunnel(prev_y, new_y, new_x);
  81. } else {
  82. map.apply_vertical_tunnel(prev_y, new_y, prev_x);
  83. map.apply_horizontal_tunnel(prev_x, new_x, new_y);
  84. }
  85. }
  86. map.rooms.push(new_room);
  87. }
  88. }
  89. map
  90. }
  91. }
  92. impl BaseMap for Map {
  93. fn is_opaque(&self, idx:i32) -> bool {
  94. self.tiles[idx as usize] == TileType::Wall
  95. }
  96. fn get_available_exits(&self, _idx:i32) -> Vec<(i32, f32)> {
  97. Vec::new()
  98. }
  99. fn get_pathing_distance(&self, idx1:i32, idx2:i32) -> f32 {
  100. let p1 = Point::new(idx1 % self.width, idx1 / self.width);
  101. let p2 = Point::new(idx2 % self.width, idx2 / self.width);
  102. rltk::DistanceAlg::Pythagoras.distance2d(p1, p2)
  103. }
  104. }
  105. impl Algorithm2D for Map {
  106. fn point2d_to_index(&self, pt: Point) -> i32 {
  107. (pt.y * self.width) + pt.x
  108. }
  109. fn index_to_point2d(&self, idx:i32) -> Point {
  110. Point{ x: idx % self.width, y: idx / self.width }
  111. }
  112. }
  113. pub fn draw_map(ecs: &World, ctx : &mut Rltk) {
  114. let map = ecs.fetch::<Map>();
  115. let mut y = 0;
  116. let mut x = 0;
  117. for (idx,tile) in map.tiles.iter().enumerate() {
  118. // Render a tile depending upon the tile type
  119. if map.revealed_tiles[idx] {
  120. let glyph;
  121. let mut fg;
  122. match tile {
  123. TileType::Floor => {
  124. glyph = rltk::to_cp437('.');
  125. fg = RGB::from_f32(0.0, 0.5, 0.5);
  126. }
  127. TileType::Wall => {
  128. glyph = rltk::to_cp437('#');
  129. fg = RGB::from_f32(0., 1.0, 0.);
  130. }
  131. }
  132. if !map.visible_tiles[idx] { fg = fg.to_greyscale() }
  133. ctx.set(x, y, fg, RGB::from_f32(0., 0., 0.), glyph);
  134. }
  135. // Move the coordinates
  136. x += 1;
  137. if x > 79 {
  138. x = 0;
  139. y += 1;
  140. }
  141. }
  142. }