aboutsummaryrefslogtreecommitdiff
path: root/src/tiling.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/tiling.rs')
-rw-r--r--src/tiling.rs87
1 files changed, 60 insertions, 27 deletions
diff --git a/src/tiling.rs b/src/tiling.rs
index f1630d6..c79d6ef 100644
--- a/src/tiling.rs
+++ b/src/tiling.rs
@@ -1,5 +1,45 @@
+use std::convert::From;
+
+#[derive(Clone)]
+pub enum TileType {
+ Empty,
+ Wall,
+ Floor,
+ StairsUp,
+ StairsDown,
+ Character(&'static str),
+ Player,
+}
+
+#[derive(Clone)]
+pub struct Tile {
+ tile_type: TileType,
+ visible: bool,
+}
+
+impl Tile {
+ pub fn new(tile_type: TileType, visible: bool) -> Self {
+ Tile { tile_type, visible }
+ }
+
+ pub fn get_type(&self) -> &TileType {
+ &self.tile_type
+ }
+}
+
+impl From<TileType> for Tile {
+ fn from(tile_type: TileType) -> Self {
+ Tile {
+ tile_type,
+ visible: true, // <--- TODO: this set the default beaviour
+ // - true: all tiles of world and entities will be drawn
+ // - false: only draw tiles visible for the player
+ }
+ }
+}
+
pub struct TileGrid {
- grid: Vec<Vec<TileType>>,
+ grid: Vec<Vec<Tile>>,
xsize: usize,
ysize: usize,
}
@@ -15,7 +55,7 @@ impl TileGrid {
for _ in 0..ysize {
let mut subvec = Vec::with_capacity(xsize);
for _ in 0..xsize {
- subvec.push(TileType::Empty);
+ subvec.push(Tile::new(TileType::Empty, true));
}
grid.grid.push(subvec);
}
@@ -23,27 +63,27 @@ impl TileGrid {
grid
}
- pub fn set_tile(&mut self, x: usize, y: usize, tile: TileType) {
+ pub fn set_tile(&mut self, x: usize, y: usize, tile: Tile) {
self.grid[y][x] = tile;
}
/// Sets a tile if nothing lies underneath it.
- pub fn set_empty_tile(&mut self, x: usize, y: usize, tile: TileType) {
+ pub fn set_empty_tile(&mut self, x: usize, y: usize, tile: Tile) {
self.set_tile(
x,
y,
- match self.grid[y][x] {
+ match self.grid[y][x].tile_type {
TileType::Empty => tile,
_ => self.grid[y][x].clone(),
},
)
}
- pub fn raw_data(&self) -> &Vec<Vec<TileType>> {
+ pub fn raw_data(&self) -> &Vec<Vec<Tile>> {
&self.grid
}
- pub fn block_at(&self, x: usize, y: usize) -> &TileType {
+ pub fn block_at(&self, x: usize, y: usize) -> &Tile {
&self.grid[y + 1][x]
}
@@ -56,29 +96,22 @@ impl TileGrid {
}
}
-pub fn tile_to_str(tile: &TileType) -> &str {
- match tile {
- TileType::Floor => ".",
- TileType::Wall => "#",
- TileType::Empty => " ",
- TileType::StairsDown => ">",
- TileType::StairsUp => "<",
- TileType::Player => "@",
- _ => "?",
+pub fn tile_to_str(tile: &Tile) -> &str {
+ if tile.visible {
+ match tile.tile_type {
+ TileType::Floor => ".",
+ TileType::Wall => "#",
+ TileType::Empty => " ",
+ TileType::StairsDown => ">",
+ TileType::StairsUp => "<",
+ TileType::Player => "@",
+ TileType::Character(t) => t,
+ }
+ } else {
+ " "
}
}
pub trait Tileable {
fn tile(&self, grid: &mut TileGrid) -> Result<(), String>;
}
-
-#[derive(Clone)]
-pub enum TileType {
- Empty,
- Wall,
- Floor,
- StairsUp,
- StairsDown,
- Character,
- Player,
-}