#path-finding #rogue-like #traits #geometry #brackets #algorithm #graphics

bracket-algorithm-traits

Traits required for the bracket-* crates. Adapt your maps to the traits with Algorithm2D, Algorithm3D and BaseMap.

6 releases

0.8.7 Oct 4, 2022
0.8.2 Feb 10, 2021
0.8.1 Apr 29, 2020
0.7.0 Feb 22, 2020
0.1.0 Feb 21, 2020

#1807 in Game dev

Download history 1131/week @ 2024-07-20 1120/week @ 2024-07-27 906/week @ 2024-08-03 1969/week @ 2024-08-10 1199/week @ 2024-08-17 1313/week @ 2024-08-24 1476/week @ 2024-08-31 1054/week @ 2024-09-07 1030/week @ 2024-09-14 1252/week @ 2024-09-21 1068/week @ 2024-09-28 433/week @ 2024-10-05 836/week @ 2024-10-12 882/week @ 2024-10-19 953/week @ 2024-10-26 1053/week @ 2024-11-02

3,759 downloads per month
Used in 10 crates (2 directly)

MIT license

88KB
2K SLoC

bracket-algorithm-traits

This crate provides traits for use in the bracket-pathfinding and bracket-geometry crates. It is part of the overall bracket-lib system.

Using a trait interface means that bracket-lib doesn't have to know or care about how you store your data, and can still provide useful geometry and path-finding functions. Defaults are provided, allowing you to get up and running quickly.

Everything is exported via bracket_algorithm_traits::prelude.

Map Indexing

A truly minimal implementation (replace TestMap with your map structure):

struct TestMap{};
impl BaseMap for TestMap {}
impl Algorithm2D for TestMap{
    fn dimensions(&self) -> Point {
        Point::new(2, 2)
    }
}

This is sufficient to provide the following services:

  • in_bounds(Point): is a point within the dimensions of the map?
  • point2d_to_index(Point) -> usize: provides an array index for a point, within the map dimensions. It assumes that your array has striding by column.
  • index_to_point2d(usize) -> Point : provides an x/y coordinate for a given array index, assuming the same striding.

If you don't like the default implementations, feel free to override them.

There is an equivalent Algorithm3D for 3D grid-based maps (substitute Point3D for Point).

Map Traversal

The BaseMap trait helps you define the map. If you want to use path-finding, you need to implement the is_opaque function:

impl BaseMap for MyMap {
    fn is_opaque(&self, _idx: usize) -> bool {
        false
    }
}

To support path-finding, you need to implement two more functions:

impl BaseMap for MyMap {
    fn get_available_exits(&self, idx: usize) -> SmallVec<[(usize, f32); 10]> {
        Vec::new()
        // Provide a list of exit indices (you can use point2d_to_index to generate them) for this
        // tile inside the array.
        // Do NOT include the current tile as an available exit.
    }

    fn get_pathing_distance(&self, _idx1: usize, _idx2: usize) -> f32 {
        1.0
        // This should be a distance, using whatever heuristic you prefer.
    }

Dependencies

~1.5MB
~33K SLoC