From 749f5f12a8f04bdf8e4373e9b6d173a70a8f9ff8 Mon Sep 17 00:00:00 2001 From: eri Date: Sun, 4 Aug 2024 13:26:20 +0200 Subject: [PATCH] feat: readme and examples --- .github/workflows/ci.yaml | 3 + .rustfmt.toml | 4 +- Cargo.lock | 141 +++++++++++++- Cargo.toml | 8 +- README.md | 99 ++++++++++ examples/dvd.rs | 169 ++++++++++++++++ examples/jump.rs | 314 ++++++++++++++++++++++++++++++ src/base.rs | 2 +- src/base/data.rs | 1 - src/base/sets.rs | 62 +++++- src/components/camera/deferred.rs | 26 +-- src/lib.rs | 1 - src/prelude.rs | 7 +- src/ui.rs | 2 +- src/ui/navigation.rs | 1 - 15 files changed, 806 insertions(+), 34 deletions(-) create mode 100644 README.md create mode 100644 examples/dvd.rs create mode 100644 examples/jump.rs diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 04aa607..5cb049a 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -30,6 +30,9 @@ jobs: - name: Cargo clippy run: cargo clippy --workspace --all-targets --all-features -- -Dwarnings --no-deps + - name: Cargo clippy (examples) + run: cargo clippy --examples -- -Dwarnings --no-deps + - name: Cargo fmt run: cargo fmt --all -- --check diff --git a/.rustfmt.toml b/.rustfmt.toml index 430a04f..2a9b31e 100644 --- a/.rustfmt.toml +++ b/.rustfmt.toml @@ -10,8 +10,8 @@ normalize_comments = true normalize_doc_attributes = true overflow_delimited_expr = true reorder_impl_items = true -single_line_if_else_max_width = 80 # 50 -single_line_let_else_max_width = 80 # 50 +single_line_if_else_max_width = 80 # 50 +single_line_let_else_max_width = 80 # 50 unstable_features = true use_field_init_shorthand = true use_try_shorthand = true diff --git a/Cargo.lock b/Cargo.lock index 668c68e..7fb8900 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -90,7 +90,7 @@ dependencies = [ "getrandom", "once_cell", "version_check", - "zerocopy", + "zerocopy 0.7.35", ] [[package]] @@ -867,8 +867,10 @@ dependencies = [ "bevy_app", "bevy_ecs", "bevy_utils", + "tracing-error", "tracing-log", "tracing-subscriber", + "tracing-tracy", "tracing-wasm", ] @@ -1088,6 +1090,7 @@ dependencies = [ "naga", "naga_oil", "nonmax", + "profiling", "ruzstd", "send_wrapper", "serde", @@ -2203,11 +2206,26 @@ dependencies = [ "log", "macro_rules_attribute", "macros", + "rand", "serde", "toml", "web-sys", ] +[[package]] +name = "generator" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "186014d53bc231d0090ef8d6f03e0920c54d85a5ed22f4f2f74315ec56cf83fb" +dependencies = [ + "cc", + "cfg-if", + "libc", + "log", + "rustversion", + "windows 0.54.0", +] + [[package]] name = "gethostname" version = "0.4.3" @@ -2848,6 +2866,19 @@ version = "0.4.22" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24" +[[package]] +name = "loom" +version = "0.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "419e0dc8046cb947daa77eb95ae174acfbddb7673b4151f56d1eed8e93fbfaca" +dependencies = [ + "cfg-if", + "generator", + "scoped-tls", + "tracing", + "tracing-subscriber", +] + [[package]] name = "mach2" version = "0.4.2" @@ -3587,6 +3618,15 @@ dependencies = [ "unicode-xid", ] +[[package]] +name = "ppv-lite86" +version = "0.2.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dee4364d9f3b902ef14fab8a1ddffb783a1cb6b4bba3bfc1fa3922732c7de97f" +dependencies = [ + "zerocopy 0.6.6", +] + [[package]] name = "presser" version = "0.3.1" @@ -3622,6 +3662,20 @@ name = "profiling" version = "1.0.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "43d84d1d7a6ac92673717f9f6d1518374ef257669c24ebc5ac25d5033828be58" +dependencies = [ + "profiling-procmacros", + "tracing", +] + +[[package]] +name = "profiling-procmacros" +version = "1.0.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8021cf59c8ec9c432cfc2526ac6b8aa508ecaf29cd415f271b8406c1b851c3fd" +dependencies = [ + "quote", + "syn 2.0.72", +] [[package]] name = "quick-xml" @@ -3653,6 +3707,18 @@ version = "0.8.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" dependencies = [ + "libc", + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", "rand_core", ] @@ -3661,6 +3727,9 @@ name = "rand_core" version = "0.6.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom", +] [[package]] name = "range-alloc" @@ -3790,6 +3859,12 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "rustversion" +version = "1.0.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "955d28af4278de8121b7ebeb796b6a45735dc01436d898801014aced2773a3d6" + [[package]] name = "ruzstd" version = "0.7.0" @@ -4222,6 +4297,16 @@ dependencies = [ "valuable", ] +[[package]] +name = "tracing-error" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d686ec1c0f384b1277f097b2f279a2ecc11afe8c133c1aabf036a27cb4cd206e" +dependencies = [ + "tracing", + "tracing-subscriber", +] + [[package]] name = "tracing-log" version = "0.2.0" @@ -4251,6 +4336,17 @@ dependencies = [ "tracing-log", ] +[[package]] +name = "tracing-tracy" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9be7f8874d6438e4263f9874c84eded5095bda795d9c7da6ea0192e1750d3ffe" +dependencies = [ + "tracing-core", + "tracing-subscriber", + "tracy-client", +] + [[package]] name = "tracing-wasm" version = "0.2.1" @@ -4262,6 +4358,26 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "tracy-client" +version = "0.17.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "63de1e1d4115534008d8fd5788b39324d6f58fc707849090533828619351d855" +dependencies = [ + "loom", + "once_cell", + "tracy-client-sys", +] + +[[package]] +name = "tracy-client-sys" +version = "0.23.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "98b98232a2447ce0a58f9a0bfb5f5e39647b5c597c994b63945fcccd1306fafb" +dependencies = [ + "cc", +] + [[package]] name = "ttf-parser" version = "0.24.0" @@ -5232,13 +5348,34 @@ version = "0.8.20" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "791978798f0597cfc70478424c2b4fdc2b7a8024aaff78497ef00f24ef674193" +[[package]] +name = "zerocopy" +version = "0.6.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "854e949ac82d619ee9a14c66a1b674ac730422372ccb759ce0c39cabcf2bf8e6" +dependencies = [ + "byteorder", + "zerocopy-derive 0.6.6", +] + [[package]] name = "zerocopy" version = "0.7.35" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1b9b4fd18abc82b8136838da5d50bae7bdea537c574d8dc1a34ed098d6c166f0" dependencies = [ - "zerocopy-derive", + "zerocopy-derive 0.7.35", +] + +[[package]] +name = "zerocopy-derive" +version = "0.6.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "125139de3f6b9d625c39e2efdd73d41bdac468ccd556556440e322be0e1bbd91" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.72", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index a169c0a..cb46bb9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,10 +21,11 @@ dev = [ release = ["common", "embedded"] common = [] # Individual features -embedded = ["include_dir"] -pixel_perfect = ["deferred"] deferred = [] +embedded = ["include_dir"] inspector = ["bevy-inspector-egui"] +pixel_perfect = ["deferred"] +trace = ["release", "bevy/trace_tracy"] [dependencies] # Bevy and plugins @@ -39,15 +40,16 @@ bevy-inspector-egui = { version = "0.25", optional = true } macros = { path = "macros" } # Other dependencies +anyhow = { version = "1.0" } include_dir = { version = "0.7", optional = true } log = { version = "*", features = [ "max_level_debug", "release_max_level_warn", ] } macro_rules_attribute = { version = "0.2" } +rand = { version = "0.8" } serde = { version = "1.0", features = ["derive"] } toml = { version = "0.8" } -anyhow = { version = "1.0" } [target.'cfg(target_arch = "wasm32")'.dependencies] web-sys = { version = "0.3", features = ["Storage"] } diff --git a/README.md b/README.md new file mode 100644 index 0000000..699bef2 --- /dev/null +++ b/README.md @@ -0,0 +1,99 @@ +# hello bevy ðŸĶ† + +an opinionated [bevy](https://github.com/bevyengine/bevy) template for my projects. + +

+ + + +

+ +### features ðŸŒŋ + +- uses bevy 0.14 +- minimal and curated plugin support +- fully featured accesible menu with keyboard, mouse and gamepad navigation +- ci that checks errors and lint +- creates binaries for web, linux, mac and windows when releasing a tag +- deploy to itch automatically +- has a nix flake development shell for easy building + +### how to use it âœĻ + +- use this template in a new project (on github, a green button on the top right) + +### runing locally 🌚 + +this project is configured to use dynamic linking for debug builds and fast recompiling by default. +in order to have the fastest compile, you may install [mold](https://github.com/rui314/mold) and use rust nightly (`rustup default nightly`). +if you don't want some of these features, go to [.cargo/config](.cargo/config) and follow the instructions, or remove it to disable optimizations all together. + +to run a debug build use cargo: + +```sh +cargo run +``` + +you can also play around with some of the included examples with `cargo run --example `. and if you want to get started quickly, copy any example to `src/main.rs`! + +if you have nix installed, running `nix develop` you get a shell with all the dependencies already installed. + +### release ðŸŒŧ + +in order to create a release build with binaries for all platforms you have two options: either you trigger it manually on the actions page or you add a tag like '[anything]0.1' with the version you want. + +```sh +git tag -a "0.1" -m "test release" +git push --tags +``` + +if you want to also deploy this build to itch, go to the repository settings > secrets > actions and add: + +``` +ITCH_API_KEY = [your api key] +``` + +to run a release build locally: + +```sh +cargo run --release --no-default-features --features release +``` + +### profiling 📈 + +bevy has built in support for the [tracy](https://github.com/wolfpld/tracy) profiler. you can profile your game easily: + +```sh +tracy-capture -o capture.tracy & +cargo run --release --no-default-features --features trace +``` + +and then view the result with: + +```sh +tracy capture.tracy +``` + +### other projects 💖 + +this is heavily based on [TheBevyFlock/bevy_quickstart](https://github.com/TheBevyFlock/bevy_quickstart) and [NiklasEi/bevy_game_template](https://github.com/NiklasEi/bevy_game_template). please use these more general templates that are robust and have community support. hello bevy is hardly tested and very tailored to my preferences. + +### plugins ðŸŠī + +this template intends to use as little external dependencies as possible to facilitate version updates and avoid bloat. that said, there are a few awesome community plugins that make everything as easy as possible. + +- [leafwing-input-manager](https://github.com/Leafwing-Studios/leafwing-input-manager): an awesome way of handling input from multiple sources and create simple bindings +- [bevy_mod_picking](https://github.com/aevyrie/bevy_mod_picking): used to select things on the screen. only the ui picker is enabled by default, used for mouse navigation +- [bevy-inspector-egui](https://github.com/jakobhellermann/bevy-inspector-egui): optional and only enabled when using the `inspector` feature. it provides a very useful world inspector + +### license 📝 + +this project is dual licensed under MIT and Apache 2.0, do what you want with it! + +the files under assets may come from other sources and have different licenses: + +- `icons/bevy.png` and `icons/pixelbevy.png` from [cart](https://github.com/bevyengine/bevy_github_ci_template/issues/45#issue-2022210264), **not** open +- `sounds/boing.ogg`, sound effect from [bigsoundbank.com](https://bigsoundbank.com/high-pitched-tom-1-s2329.html), [CC0](https://creativecommons.org/publicdomain/zero/1.0/) +- `music/rain.ogg`, sound effect from [bigsoundbank.com](https://bigsoundbank.com/summer-rain-on-terrace-s1019.html), [CC0](https://creativecommons.org/publicdomain/zero/1.0/) +- `fonts/pixel.ttf`, public pixel font from [ggbot](https://ggbot.itch.io/public-pixel-font), [CC0](https://creativecommons.org/publicdomain/zero/1.0/) +- `fonts/sans.tff`, outfit font from [google](https://fonts.google.com/specimen/Outfit), [OFL](https://scripts.sil.org/cms/scripts/page.php?site_id=nrsi&id=OFL) diff --git a/examples/dvd.rs b/examples/dvd.rs new file mode 100644 index 0000000..c31316a --- /dev/null +++ b/examples/dvd.rs @@ -0,0 +1,169 @@ +use game::prelude::*; +use rand::{distributions::Standard, prelude::*}; + +fn main() { + App::new().add_plugins((GamePlugin, plugin)).run(); +} + +fn plugin(app: &mut App) { + app.add_event::() + .add_systems(OnEnter(GameState::Play), init.run_if(run_once())) + .add_systems( + Update, + ( + update_velocity.in_set(PlaySet::Update), + on_collision + .in_set(PlaySet::ReadEvents) + .run_if(on_event::()), + ), + ); +} + +// Components +// --- + +/// Applies a velocity to an entity every frame. +#[derive(Component)] +struct Velocity(Vec2); + +/// Marker for the score counter entity. +#[derive(Component)] +struct Counter(u32); + +// Events +// --- + +/// Event that is triggered when an entity collides with the screen border. +#[derive(Event)] +struct CollisionEvent; + +// Systems +// --- + +/// Spawn the initial objects. +fn init( + mut cmd: Commands, + options: Res, + meta_assets: Res>, + font_assets: Res>, +) { + // Moving balls + for velocity in [ + Vec2::new(300., 250.), + Vec2::new(-150., 400.), + Vec2::new(200., -350.), + ] { + cmd.spawn(( + SpriteBundle { + texture: meta_assets.get(&MetaAssetKey::BevyLogo).clone_weak(), + sprite: Sprite { + custom_size: Some(Vec2::splat(96.)), + ..default() + }, + transform: Transform::from_translation(Vec3::new(0., 0., 1.)), + ..default() + }, + Velocity(velocity), + )); + } + + // Counter text + cmd.spawn(( + Text2dBundle { + text: Text::from_section("0", TextStyle { + font: font_assets.get(&FontAssetKey::Main).clone_weak(), + font_size: 192., + color: options.palette.light, + }), + ..default() + }, + Counter(0), + )); +} + +/// Update the position of the objects with the `Velocity` component and check +/// for collisions with the window border +fn update_velocity( + time: Res