2 releases
Uses new Rust 2024
| 0.1.1 | Oct 7, 2025 |
|---|---|
| 0.1.0 | Oct 5, 2025 |
#837 in Data structures
Used in conduit-cli-game
24KB
192 lines
cli-tilemap
Rust crate that provides basic functionality 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 HashMap<Cell, V>.
Note:
- Crate is in the "work in progress" state, so the public API may change in the future. Feel free to contribute!
Examples:
use cli_tilemap::{Tile, TileMap, Formatting};
use crossterm::style::{Stylize, StyledContent};
use grid_math::Cell;
use std::io::stdout;
// declare Entity enum
#[derive(Default, Debug)]
enum Entity {
Enemy,
Hero,
#[default]
Air,
}
// represent Entity as tile
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(),
}
}
}
fn main() {
// create 5x5 tilemap:
let mut map: TileMap<Entity> = TileMap::new(5, 5);
// modify formatting rules for map:
map.formatting.tile_spacing = 1; // already 1 by default
map.formatting.row_spacing = 1; // already 1 by default
// 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 draw!");
// or transform tilemap into string:
//let map_str = map.to_string();
//println!("{map_str}");
}
This will create new TileMap<Entity>, fill it with some instances of Entity, and then draw it to the stdout:
Note:
- `TileMap` type is based on the `GridMap` type, provided by the `grid-math` crate,
so to get more detailed explanation about `Cell`, `Grid`, and `GridMap` types, visit it's documentation.
- Also, to get more information about `StyledContent` type, `Stylize` trait, visit `crossterm` documentation.
For more examples, visit cli-tilemap documentation on https://docs.rs/cli-tilemap/latest/cli_tilemap/ Crab Crab! 🦀🦀
Dependencies
~3–7MB
~157K SLoC