7 个版本
0.2.4 | 2024 年 6 月 7 日 |
---|---|
0.2.3 | 2024 年 6 月 7 日 |
0.1.1 | 2024 年 6 月 2 日 |
472 in 游戏开发
5KB
表格
一个 Rust 生成连续内存块的泛型结构。灵感来自 Richard Fabian 的《面向数据设计》一书。
安装
cargo add dod-table
用法
use dod_table::*;
// Make sure that the type used in the table implements the Default trait.
#[derive(Default)]
struct Entity {
x: f32,
y: f32,
some_state: String,
some_flag: bool,
}
// The global data for our game.
struct Data {
// Memory reserved for (in this specific example) 512 entities.
entities: Table<Entity, 512>,
// We choose a specific element in the table for the player entity.
player_id: usize,
}
// We could also derive the Default trait here like the Entity struct, but lets
// instead implement a 'new' method for our Data struct.
impl Data {
fn new() -> Self {
Self {
// Table implements the Default trait, hence that its type also needs to implement it.
entities: Table::default(),
// Let's reserve the first index as the player entity.
player_id: 0,
}
}
}
// Your game's entrypoint.
fn main() {
let data = Data::new();
// Initialize the player data.
data.entities.set(data.player_id, Entity {
x: 100.,
y: 100.,
..Default::default(),
});
loop {
let player = data.entities.get_mut(data.player_id);
// The game logic...
}
}