6个版本
0.1.5 | 2021年5月10日 |
---|---|
0.1.4 | 2021年5月9日 |
#894 in 游戏开发
41KB
592 行
Das Grid
Das Grid是一个2D网格库,作为基于网格概念的任何2D游戏的基本构建模块
基于2D网格概念构建的著名游戏
- 国际象棋/跳棋
- 象棋
- Scrabble
- Tetris
- 宝石迷阵
- Shinning Force(战斗时)
Das Grid提供
- 通用网格类型,您可以使用任何类型作为网格单元
- 辅助器使网格内值的移动变得简单
- 基于2D顶部/左侧到底部/右侧概念(未来可能更新)
使用Das Grid
同时,请参阅docs.rs/das-grid/0.1.4/das_grid中的文档
创建网格
// Creates a 10x10 grid with 0 as default value for each cell
let mut g = das_grid::Grid::new(10, 10, 0);
// Set the the value 1 at position x: 5 and y: 5
g.set((5, 5), &1);
自带类型
// Using &str instead of i32
let mut g: das_grid::Grid<&str> = das_grid::Grid::new(10, 10, "a");
g.get((0, 0)).unwrap(); // ouputs: "a"
extern crate das_grid;
// Your own enum, much better to track grid values
#[derive(Clone, Copy, PartialEq, Eq)]
enum Pawn {
None,
Player,
}
impl std::fmt::Display for Pawn {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match *self {
Pawn::None => write!(f, "None"),
Pawn::Player => write!(f, "Player"),
}
}
}
fn main() -> Result<(), das_grid::GridErr> {
// Initialize empty grid
let mut g: das_grid::Grid<Pawn> = das_grid::Grid::new(8, 8, Pawn::None);
// Set the Player on position 5,5
g.set((5, 5), &Pawn::Player)?;
println!("Grid initial state {:?}", g);
// Move the player to right
if let Ok(()) = g.mov_to((5, 5), das_grid::MoveDirection::Right) {
// "The pawn on 5,6 is Player"
println!("The pawn on 5,6 is {}", g.get((6, 5)).unwrap());
}
println!("Grid initial end state {:?}", g);
Ok(())
}
mov_to
函数会返回一个Result<(), OutOfGridErr>
,如果移动尝试超出网格范围
示例
要构建和运行示例
cargo run --example custom_values
# Output
# Initial state Grid { rows: 2, cols: 2, cells: [
# Player (x: 0 y: 0) None (x: 0 y: 1)
# None (x: 1 y: 0) None (x: 1 y: 1)
# ] }
#
# The pawn on 0, 1 is Player
#
# End state Grid { rows: 2, cols: 2, cells: [
# None (x: 0 y: 0) Player (x: 0 y: 1)
# None (x: 1 y: 0) None (x: 1 y: 1)
# ] }
移动单元
网格中的每个瓦片都称为单元,每个单元都是您想要的类型,因为它是二维结构,每个单元都有一个地址,由X和Y组成
// Creates a 5x5 grid with 0 as default value for each cell
let mut g = das_grid::Grid::new(5, 5, 0);
// Print with special {:?} to see the contents of the grid
println!("{:?}", g);
// outputs:
// Grid { rows: 5, cols: 5, cells: [
// 0 (x: 0 y: 0) 0 (x: 1 y: 0) 0 (x: 2 y: 0) 0 (x: 3 y: 0) 0 (x: 4 y: 0)
// 0 (x: 0 y: 1) 0 (x: 1 y: 1) 0 (x: 2 y: 1) 0 (x: 3 y: 1) 0 (x: 4 y: 1)
// 0 (x: 0 y: 2) 0 (x: 1 y: 2) 0 (x: 2 y: 2) 0 (x: 3 y: 2) 0 (x: 4 y: 2)
// 0 (x: 0 y: 3) 0 (x: 1 y: 3) 0 (x: 2 y: 3) 0 (x: 3 y: 3) 0 (x: 4 y: 3)
// 0 (x: 0 y: 4) 0 (x: 1 y: 4) 0 (x: 2 y: 4) 0 (x: 3 y: 4) 0 (x: 4 y: 4)
// ] }
如前所述,要移动单元,您可以调用函数mov_to
并传递起始点和终点作为方向
方向可以是左、右、上、下
- DasGrid::MoveDirection::Left,表示为(-1, 0)
- DasGrid::MoveDirection::Right,表示为(1, 0)
- DasGrid::MoveDirection::Top,表示为(0, -1)
- DasGrid::MoveDirection::Down,表示为(0, 1)
mov_to
函数会返回一个Result<(), OutOfGridErr>
,如果移动尝试超出网格范围
迭代器
网格实现了一些迭代器,这些迭代器在日常使用中可能非常有用
遍历扁平网格结构
let grid: DasGrid<i32> = DasGrid::new(2, 2, 0);
let mut result: Vec<i32> = vec![];
for v in &grid {
println!("Value {}", value);
}
assert!(result == [0, 0, 0, 0]);
遍历X和Y的枚举
let mut grid: DasGrid<i32> = DasGrid::new(2, 2, 0);
// Returns the X and Y as tuple
for (x, y) in grid.enumerate() {
println!("X {} Y {}", x, y);
}
许可证
MIT License
Copyright (c) 2021 Eduardo Pereira
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.