2 releases

new 0.5.1 Jan 31, 2025
0.5.0 Jan 31, 2025

#147 in Algorithms

39 downloads per month

Apache-2.0

305KB
3K SLoC

Latest Version License:Apache Build Status Coverage Status

Bevy_Knossos

Bevy_Knossos is a fork from knossos Rust library and CLI for maze generation, but with focus on Bevy development. It is complete with fundamental functions for rendering and saving mazes to files.

Reference for Knossos Library

In Greek mythology, King Minos dwelt in a palace at Knossos. He hired the Athenian architect, mathematician, and inventor Daedalus to design his palace and so cleverly was it constructed that no one who entered could find their way back out without a guide. In other versions of this same story it was not the palace itself which was designed in this way but the labyrinth within the palace which was built to house the half-man/half-bull the Minotaur. In order to keep Daedalus from telling the secrets of the palace, Minos locked him and his son Icarus in a high tower at Knossos and kept them prisoner. Daedalus fashioned wings made of wax and bird's feathers for himself and his son, however, and escaped their prison but Icarus, flying too close to the sun, melted his wings and fell to his death.

Source: https://www.worldhistory.org/knossos

Overview

Bevy_Knossos currently supports only one type of mazes: orthogonal, which is a standard maze layout of rectangular passages. If you only want a Rust Maze library, checkout Knossos Library.

The library supports the following generation algorithms:

Knossos Library supports the following output types:

  • ASCII With the ASCII output option, you can effortlessly display a maze on the console or save it to a file to visualize its appearance.

  • Game map If you are looking to create your own game featuring pseudo 3D graphics or testing your ray casting algorithm implementation, you can transform a maze into a game map using this formatter. It offers various configuration options, including the span value for specifying the distance between opposing walls, the characters wall and passage for map construction, and the ability to randomly place start S and goal G points along the borders.

  • Image Utilizing the Image output feature, you have the capability to render a maze into PNG or JPG formats (simply utilize the appropriate filename extension). This output type offers extensive customization options, enabling you to define custom margins, wall and passage widths, as well as background and foreground colors.

Installation

Run the following Cargo command in your project directory:

cargo add bevy_knossos

Or add the following line to your Cargo.toml:

[dependencies]
bevy_knossos = "0.5.0"

Usage

For information on knossos CLI usage, check the original repository #Cli or #Usage.

Crates may have diverged

Bevy Usage

Bevy support table

bevy bevy_knossos
0.15 0.5

Bevy Mini-maze under Kenney.nl license and APACHE:

Resulting Maze with Bevy

Resulting Maze with Bevy

use bevy::{prelude::*, utils::HashMap};
use bevy_knossos::{maze::*, Coords, CoordsComponent};

fn main() {
    let maze = OrthogonalMazeBuilder::new()
        .algorithm(Box::new(RecursiveBacktracking))
        .width(5)
        .height(5)
        .build()
        .unwrap();

    App::new()
        .insert_resource(maze)
        .add_plugins(DefaultPlugins)
        .add_systems(Startup, load_assets)
        .add_systems(PostStartup, setup.after(load_assets))
        .run();
}

#[derive(Clone, Debug, Reflect, Resource, Default)]
pub struct TilesHandles {
    map: HashMap<String, Handle<bevy::image::Image>>
}

fn load_assets(mut commands: Commands, asset_server: Res<AssetServer>) {
    let mut tiles = TilesHandles::default();
    let images = vec![
        "tile_0000.png",
        "tile_0001.png",
        "tile_0010.png",
        "tile_0011.png",
        "tile_0100.png",
        "tile_0101.png",
        "tile_0110.png",
        "tile_0111.png",
        "tile_1000.png",
        "tile_1001.png",
        "tile_1010.png",
        "tile_1011.png",
        "tile_1100.png",
        "tile_1101.png",
        "tile_1110.png",
        "tile_1111.png",
    ];

    for image in images {
        let handle = asset_server.load(image);
        tiles.map.insert(image.to_string(), handle);
    }
    commands.insert_resource(tiles);
}

fn setup(mut commands: Commands, maze: Res<OrthogonalMaze>, tiles: Res<TilesHandles>) {

    commands.spawn((
        Camera2d,
        Name::new("Camera"),
    ));

    for (coords, cell) in maze.iter() {
        let bundle = load_image(&coords, cell, &tiles);
        commands.spawn(bundle);
    }
}

fn load_image(coords: &Coords, cell: &Cell, tiles: &Res<TilesHandles>) -> (
    CoordsComponent,
    Cell,
    Sprite,
    Name,
    Transform
) {
    let cell_sprite = format!("tile_{}.png", cell.to_bits_str());
    let sprite = Sprite::from_image(tiles.map.get(&cell_sprite).expect("All tiles should have been registered").clone());
    let name = Name::new(format!("({},{}): {}", coords.0, coords.1, cell));
    let position = Transform::from_xyz(coords.0 as f32 * 45., (5 - coords.1) as f32 * 45., 0.)
        .with_scale(Vec3::from_slice(&[5., 5., 0.1]));

    (coords.to_owned().into(), *cell, sprite, name, position)
}
kenny.nl CC0 Minimap Pack License Minimap Pack (1.0)

Created/distributed by Kenney (www.kenney.nl) Creation date: 05-11-2024


License: (Creative Commons Zero, CC0) http://creativecommons.org/publicdomain/zero/1.0/

This content is free to use in personal, educational and commercial projects.

Support us by crediting Kenney or www.kenney.nl (this is not mandatory)


Donate: http://support.kenney.nl Patreon: http://patreon.com/kenney/

Follow on Twitter for updates: http://twitter.com/KenneyNL

Dependencies

~25–36MB
~600K SLoC