2020-09-01 12:46:31 +00:00
|
|
|
//! Map structure contains information about tiles and other elements on the map.
|
|
|
|
//!
|
|
|
|
//! Map is created with generators and then can by modified with MapModifiers.
|
|
|
|
//!
|
|
|
|
//! This structure is not intented to be your map in the game (But can be used as one).
|
|
|
|
//! Rather the information from this map will be copied to the structures required by
|
|
|
|
//! specific game.
|
|
|
|
//!
|
|
|
|
|
|
|
|
/// Position on the map
|
|
|
|
#[derive(PartialEq, Copy, Clone, Debug, Eq, Hash)]
|
2020-09-02 10:01:16 +00:00
|
|
|
pub struct Point {
|
2020-09-01 12:46:31 +00:00
|
|
|
x: usize,
|
|
|
|
y: usize
|
|
|
|
}
|
|
|
|
|
2020-09-02 10:01:16 +00:00
|
|
|
impl Point {
|
|
|
|
/// Create new point
|
|
|
|
pub fn new(x: usize, y: usize) -> Point {
|
|
|
|
Point {x, y}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Euclidean distance to a given point
|
|
|
|
pub fn distance_to(self, point: &Point) -> f32 {
|
|
|
|
let a = (self.x as f32 - point.x as f32).powf(2.0);
|
|
|
|
let b = (self.y as f32 - point.y as f32).powf(2.0);
|
|
|
|
(a + b).sqrt()
|
2020-09-01 12:46:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Possible tile type on the map
|
|
|
|
#[derive(PartialEq, Copy, Clone, Debug, Eq, Hash)]
|
2020-08-31 09:45:59 +00:00
|
|
|
pub enum TileType {
|
|
|
|
Wall, Floor
|
|
|
|
}
|
|
|
|
|
2020-09-01 12:46:31 +00:00
|
|
|
/// Map data
|
2020-08-31 09:45:59 +00:00
|
|
|
#[derive(Default, Clone)]
|
|
|
|
pub struct Map {
|
|
|
|
pub tiles : Vec<TileType>,
|
2020-08-31 12:13:52 +00:00
|
|
|
pub width : usize,
|
|
|
|
pub height : usize,
|
2020-09-02 10:01:16 +00:00
|
|
|
pub starting_point: Option<Point>,
|
|
|
|
pub exit_point: Option<Point>
|
2020-08-31 09:45:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Map {
|
|
|
|
|
|
|
|
/// Generates an empty map, consisting entirely of solid walls
|
2020-08-31 12:13:52 +00:00
|
|
|
pub fn new(width: usize, height: usize) -> Map {
|
|
|
|
let map_tile_count = width*height;
|
2020-08-31 09:45:59 +00:00
|
|
|
Map{
|
|
|
|
tiles : vec![TileType::Wall; map_tile_count],
|
|
|
|
width,
|
2020-09-01 12:46:31 +00:00
|
|
|
height,
|
|
|
|
starting_point: None,
|
|
|
|
exit_point: None,
|
2020-08-31 09:45:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-31 20:03:48 +00:00
|
|
|
/// Get TileType at the given location
|
2020-08-31 12:13:52 +00:00
|
|
|
pub fn at(&self, x: usize, y: usize) -> TileType {
|
|
|
|
let idx = y * self.width + x;
|
2020-08-31 09:45:59 +00:00
|
|
|
self.tiles[idx]
|
|
|
|
}
|
|
|
|
|
2020-08-31 20:03:48 +00:00
|
|
|
/// Modify tile at the given location
|
|
|
|
pub fn set_tile(&mut self, x: usize, y: usize, tile: TileType) {
|
|
|
|
let idx = y * self.width + x;
|
|
|
|
self.tiles[idx] = tile;
|
|
|
|
}
|
2020-08-31 09:45:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// ------------------------------------------------------------------------------------------------
|
|
|
|
/// Module unit tests
|
|
|
|
/// ------------------------------------------------------------------------------------------------
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
2020-09-02 10:01:16 +00:00
|
|
|
#[test]
|
|
|
|
fn test_distance() {
|
|
|
|
let p1 = Point::new(10, 10);
|
|
|
|
let p2 = Point::new(14, 7);
|
|
|
|
let distance = p1.distance_to(&p2);
|
|
|
|
assert_eq!(distance, 5.0);
|
|
|
|
}
|
|
|
|
|
2020-08-31 09:45:59 +00:00
|
|
|
#[test]
|
|
|
|
fn test_new_map() {
|
|
|
|
let map = Map::new(10, 10);
|
|
|
|
for i in 0..10 {
|
|
|
|
for j in 0..10 {
|
|
|
|
assert_eq!(map.at(i, j), TileType::Wall);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|