Crate cli_tilemap

Crate cli_tilemap 

Source
Expand description

Basic implementation of TileMap for CLI-based games!

This module contains the Tile trait, allowing to represent other data types as tile, or more specifically as StyledContent<&'static str>, provided by the crossterm crate, and the TileMap<T> type, representing a tilemap of T, where T: Tile + Default, which is based on the GridMap<V> from [grid-math] crate, which is a wrapper around the std::collections::HashMap.

TileMap<T> implements the Deref and the DerefMut traits to deref to the inner HashMap, so we can work with it in the same way as with the std::collections::HashMap.

§Note

  • Crate is in the “work in progress” state, so the public API may change in the future. Feel free to contribute!

§Examples

TileMap for custom data type:

use cli_tilemap::{Tile, TileMap, Formatting};
use crossterm::style::{Stylize, StyledContent};
use grid_math::Cell;
use std::io::stdout;


#[derive(Default, Debug)]
enum Entity {
    Enemy,
    Hero,
    #[default]
    Air,
}

impl Tile for Entity {
    fn tile(&self) -> StyledContent<&'static str> {
        match self {
            Self::Air => "[-]".dark_grey().bold(),
            Self::Hero => "[&]".green().bold(),
            Self::Enemy => "[@]".red().bold(),
        }
    }
}

// new 5x5 tilemap:
let mut map: TileMap<Entity> = TileMap::new(5, 5);
// insert entities:
map.insert(Cell::new(3, 3), Entity::Enemy);
map.insert(Cell::new(1, 0), Entity::Hero);
// draw map to the raw stdout:
map.draw(&mut stdout()).expect("should be able to draw to the stdout!");
// change row and tile spacing:
map.formatting.row_spacing = 2;
map.formatting.tile_spacing = 4;
// format as a string and print:
let map_string = map.to_string();
println!("{map_string}");

For more documentation about the Grid, GridMap and Cell types, visit https://crates.io/crates/grid-math

Structs§

Formatting
Formatting represents instructions for TileMap<T> on how to draw tilemap to the terminal
TileMap
TileMap<T>, represents a tilemap over the type T, where T is Tile + Default

Traits§

Tile
Tile allows to represent any other data type as tile, or more specifically as StyledContent<&'static str>