1 个不稳定版本

0.1.0 2023年11月27日

#1380Rust 模式

每月 27 次下载

自定义许可证

20KB
408

描述

wave_function_collapse 是一个用于生成随机网格(例如,游戏)的 Rust 包。

用法

// First create tiles like so
use wave_function_collapse::prelude::*;
use std::rc::Rc;

#[derive(Debug, Clone)]
pub struct Water;
#[derive(Debug, Clone)]
pub struct Beach;
#[derive(Debug, Clone)]
pub struct Land;

// And define rules for them using `create_tile!` macro

create_tile_unit!(Water, Water, Beach;);
create_tile_unit!(Beach, Beach, Water Land;);
create_tile_unit!(Land, Land, Beach;);

// Now create `GridBuilder` for defining your grids

let size = (25, 25);
let tiles: Vec<Rc<dyn Tile>> = create_tiles_expr!(
  Water,
  Beach,
  Land,
);
let grid_builder = GridBuilder::new().with_size(size).with_tiles(tiles).seal();

// One thing to note is that you need to specify type for variable that holds your tiles.
// Create simple grid

let grid = grid_builder.build().gen();

// This will create and generate grid that we want. For debugging purposes lets print want we get.

for (id, tile) in grid.iter().enumerate() {
  if id % size.0 == 0 {
    println!();
  }
  if tile.as_any().downcast_ref::<Water>().is_some() {
    print!("W");
  } else if tile.as_any().downcast_ref::<Beach>().is_some() {
    print!("B");
  } else if tile.as_any().downcast_ref::<Land>().is_some() {
    print!("L");
  }
}

有关更多信息,请参阅文档!

依赖项

~250–630KB
~13K SLoC