Crates.io | ldtk_rust |
lib.rs | ldtk_rust |
version | 0.6.0 |
source | src |
created_at | 2020-12-22 16:05:31.420774 |
updated_at | 2022-05-09 17:38:03.789983 |
description | Use the LDtk 2D level editor to build games in Rust. |
homepage | https://github.com/estivate/ldtk_rust |
repository | https://github.com/estivate/ldtk_rust |
max_upload_size | |
id | 326068 |
size | 2,309,940 |
ldtk_rust enables access to LDtk data for use in Rust. LDtk is a 2D level editor for games that supports multiple tile layers, powerful auto-tiling rules, entity placement and more.
This library works with LDtk version 1.1.3
and supports the optional external
level files. LDtk updates save files automatically, so there's no reason to be
on an older version, but if you are (or if you get a new version before this
crate is updated) you can follow the process below to
generate code against whatever LDtk version you want to use. Or if you'd like to
not use ANY library and embed the code directly in your game, take a look at
this process.
Calling the new() method on the LdtkFile struct with the path to a LDtk file will populate a struct that closely resembles the LDtk JSON format.
use ldtk_rust::Project;
fn main() {
let ldtk = Project::new( "assets/test_game.ldtk");
println!("First level pixel height is {}!", ldtk.levels[0].px_hei);
}
Your editor's auto-complete should help you visualize your options, or you can generate API docs with "cargo doc --open" or view them here.
You can run the programs in the example folder using cargo:
> cargo run --example basic
Example dependencies do not load when compiling the library for production.
An example running in Bevy Engine is included in the examples directory. There are lots of comments, and the focus of the example is on the process, not the Bevy-specific code. If you are using another game engine the example will hopefully still be understandable and useful.
Use Project::new()
to load all data, including any external level data. Use
this if you want to load all your data at startup and you don't want to worry about
whether level data is in separate files.
If you want to load one level at a time, see examples/single_level.rs
. Essentially
you will call Project::load_project()
followed by Level::new()
as you load each
level.
The JSON deserialization is handled by serde using Rust code that is auto-generated from the LDtk JSON schema. In general this code matches the LDtk documentation except CamelCase names preferred in JSON are changed to snake_case names preferred in Rust. JSON types of String, Int and Float become Rust types of String, i64 and f64.
Fields that allow null values are wrapped in a Rust Option<T>
ldtk-rs auto generates the entire crate from the JSON schema specified.
The LDtk project publishes QuickType loaders for a variety of languages. These are auto generated, so they might need adjusting a bit. Last time I tested the Rust version, I needed to tweak the serde line at the top.
Embed the JSON conversion in your game (removing any dependencies) by following
the instructions below and reviewing the lib.rs
file.
LDtk includes a JSON schema that can be used to auto-generate RUST code to unmarshal the JSON.
To use this library with an older (or newer) version of the LDtk Schema:
docs/
directory..rs
version files to see this.mod
and pub use
lines at the top of lib.rs
(in the same
directory you're working in already) to include your new file instead.You'll need to adjust your Cargo.toml file to use your project instead of this one (or contribute your change back here).
Take a look at lib.rs
and decide if you really even want this
project wrapping the autogenerated code, or if you want to just include it
in your own project directly. As LDtk nears 1.0 the JSON Schema is getting
better, and using this process has become easier. To do this:
lib.rs
file in this project to see how it's included and used.